Hibernate配置---检索策略
Hibernate是一个持久层的框架,不依赖任何容器,支持对象到数据库表的映射,ORM。支持开发中的增删改查操作,将基本的JDBC操作进
Hibernate是一个持久层的框架,不依赖任何容器,支持对象到数据库表的映射,ORM。
支持开发中的增删改查操作,将基本的JDBC操作进行封装,可以通过配置hibernate.cfg.xml来方便的切换数据库。做到了系统和数据库的解偶。
支持数据库连接池,防止高并发导致系统响应慢和系统崩溃。当多用户同时访问应用时,每次请求都会开辟一个连接对象,当开启和关闭时都会有时间的消耗,如果没有访问数据库的限制,那么将导致系统响应越来越慢,内存如果没有及时回收导致系统崩溃。hibernate常用的为c3p0连接池,通过在hibernate.cfg.xml中配置。
检索策略:
多对一配置策略:
在many-to-one元素中可以指定outer-join="true" lazy="false",采用迫切做外连接的方式检索,,使用一条语句查询出与之相关的一端,默认为立即检索,会分别发出语句查询相关联的表。lazy只可以取值:proxy,no-proxy,false。如果要延迟加载一的一端,那么需要在many-to-one中配置outer-join="proxy" ,在one-to-many端的类属性上配置lazy=true
one-to-many元素中可以在set上指定outer-join="true" lazy="false",采用迫切左外连接方式检索,默认立即检索。
如果将lazy设置为true,那么无论outer-join为什么值,都将采用延迟加载。
一对一关联延迟加载配置:
one-to-one
在一端Seal类:
cascade="save-update" lazy="proxy" constrained="true"/>
在另一端SealData:
select-before-update="false" lazy="true">
看红色部分配置,这样可以在查询seal的时候延迟加载sealData。
延迟加载主要可以用在:
Hibernate3.x,lazy默认是打开的
Hibernate Lazy策略使用:标签,可以取值true/false/extra
load支持lazy,get不支持,无论持久化类设置与否,都会立即检索。
(1)class标签
默认为true
class上的Lazy,只对普通属性起作用,对集合和
即:如果一个类里有Set集合成员变量和需要用
如果在使用load加载了某个对象,但是没有使用,即没有发出SQL语句,就把session给关了,此时,如果再使用这个对象就会抛出LazyInitalizationException。因为对象并没有加载上来,因为使用了lazy策略,只是加载了类的一个代理子类。
(2)集合
默认为true
默认为true情况下,只要使用到了Set对象,就会把整个set全部查询出来。
false情况下,不使用Lazy,查询Lazy所属的对象时,set就会被查询上来。需要类增强工具。
extra情况下,比较智能,根据查询的内容,生成不同的SQL语句。效率会高一些。
(3)单端关联
默认为proxy
false:不使用Lazy。
noproxy:需要类增强工具
(4)property
默认false
true:较少使用,如果某个字段是内容量比较大,可以设置为true,在真正使用的时候再加载。可以提高效率。,但是需要hibernate的工具包,org.hibernate.tool.instrument.cglib借助Ant来对编译完成的class文件做一个增强,实际上是在class文件中加入一些cglib代码,这样hibernate可以在运行时判断属性是否需要立即加载,否则延迟加载。为了达到这个目的需要每次都编译增强,不利于项目维护。采用的方法为:将大字段抽取到一张表里面,然后和基础表建立一对一关联关系,具体是业务来定,当查询的时候就可以借助Hibernate的延迟策略来达到懒加载目的。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Integrating Hibernate in SpringBoot Project Preface Hibernate is a popular ORM (Object Relational Mapping) framework that can map Java objects to database tables to facilitate persistence operations. In the SpringBoot project, integrating Hibernate can help us perform database operations more easily. This article will introduce how to integrate Hibernate in the SpringBoot project and provide corresponding examples. 1.Introduce dependenciesIntroduce the following dependencies in the pom.xml file: org.springframework.bootspring-boot-starter-data-jpam

Java is an object-oriented programming language that is widely used in the field of software development. Hibernate is a popular Java persistence framework that provides a simple and efficient way to manage the persistence of Java objects. However, Hibernate errors are often encountered during the development process, and these errors may cause the program to terminate abnormally or become unstable. How to handle and avoid Hibernate errors has become a skill that Java developers must master. This article will introduce some common Hib

The differences between hibernate and mybatis: 1. Implementation method; 2. Performance; 3. Comparison of object management; 4. Caching mechanism. Detailed introduction: 1. Implementation method, Hibernate is a complete object/relational mapping solution that maps objects to database tables, while MyBatis requires developers to manually write SQL statements and ResultMap; 2. Performance, Hibernate is possible in terms of development speed Faster than MyBatis because Hibernate simplifies the DAO layer and so on.

In 2025, global digital virtual currency trading platforms are fiercely competitive. This article authoritatively releases the top ten digital virtual currency trading platforms in the world in 2025 based on indicators such as transaction volume, security, and user experience. OKX ranks first with its strong technical strength and global operation strategy, and Binance follows closely with high liquidity and low fees. Platforms such as Gate.io, Coinbase, and Kraken are at the forefront with their respective advantages. The list covers trading platforms such as Huobi, KuCoin, Bitfinex, Crypto.com and Gemini, each with its own characteristics, but investment should be cautious. To choose a platform, you need to consider factors such as security, liquidity, fees, user experience, currency selection and regulatory compliance, and invest rationally

PrimeFactor−Innumbertheory,theprimefactorsofapositiveintegeraretheprimenumbersthatdividethatintegerexactly.Theprocessoffindingthesenumbersiscalledintegerfactorization,orprimefactorization.Example−Primefactorsof288are:288=2x2x2x2x2

Hibernate's one-to-many and many-to-many Hibernate is an excellent ORM framework that simplifies data access between Java applications and relational databases. In Hibernate, we can use one-to-many and many-to-many relationships to handle complex data models. Hibernate's one-to-many In Hibernate, a one-to-many relationship means that one entity class corresponds to multiple other entity classes. For example, an order can correspond to multiple order items (OrderItem), and a user (User) can correspond to multiple orders (Order). To implement a one-to-many relationship in Hibernate, you need to define a collection attribute in the entity class to store

Ranking of the top ten virtual currency trading platforms (latest in 2025): Binance: Global leader, high liquidity, and regulation has attracted attention. OKX: Large user base, supports multiple currencies, and provides leveraged trading. Gate.io: A senior exchange, with a variety of fiat currency payment methods, providing a variety of trading pairs and investment products. Bitget: Derivatives Exchange, high liquidity, low fees. Huobi: An old exchange that supports a variety of currencies and trading pairs. Coinbase: A well-known American exchange, strictly regulated. Phemex and so on.

Top 10 digital currency trading platforms: 1. OKX, 2. Binance, 3. Gate.io, 4. Huobi Global, 5. Kraken, 6. Coinbase, 7. KuCoin, 8. Bitfinex, 9. Crypto.com, 10. Gemini, these exchanges have their own characteristics, and users can choose the platform that suits them based on factors such as security, fees, currency selection, user interface and customer support.
