Hibernate的dynamic-insert和dynamic-update的使用
Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update
Hibernate在初始化的时候,默认按照配置为表预定义insert,delete,update,select(by id)的SQL语句放在session中,其中insert,update,select操作都是对表的所有字段操作.如果在一个表有很多字段的时候,在做初次inser的时候有比较多的字段为空值,或者经常update某少部分字段,应该在配置文件的
如有这样一张表:create table hbtest(id int,val1 varchar2(100),val2 varchar2(100));
1,在 dynamic-insert没有设置的时候
tbo.setId(new Integer(2));
tbo.setVal1("val1");
sessionFactory.getCurrentSession().save(tbo);
某些字段为空做insert,hibernate会用全字段的insert sql语句,如下:
insert into HBTEST(VAL1, VAL2,ID) values(?, ?,?)
2,将dynamic-insert设置为true,同样的保存,hibernate会动态生成SQL语句,没有值的字段不会出现在insert语句中.
insert into HBTEST(VAL1, ID) values(?, ?)
3,在 dynamic-update没有设置的时候
Hbtest tbo = (Hbtest) sessionFactory.getCurrentSession().load(Hbtest.class,new Integer(1));
tbo.setVal1("valXX");
tx.commit();
只更新部分字段,hibernate仍然对所有字段做更新:
update HBTEST set VAL1=?,VAL2=? where ID=?
4,同样的操作如果把设置为true的话,sql语句只包含更新的字段:update HBTEST set VAL1=? where ID=?
5,Hibernate这种动态SQLupdate的特性是利用在对象从数据库加载到hibernate session的时候保存了一份快照,做更新的时候与这个快照做比较,只更新改动过的值.
所以下面这种情况就会用全部字段进行更新:,不设值的字段会被更新成null.
Hbtest tbo = new Hbtest();
tbo.setId(new Integer(1));
tbo.setVal1("val1ZZZ");
sessionFactory.getCurrentSession().update(tbo);
这种情况应该利用Hibernate提供的对SQL的支持,用SQL做更新操作.

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

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

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.

Hibernate is an open source ORM framework that binds the data mapping between relational databases and Java programs to each other, making it easier for developers to access data in the database. Using the Hibernate framework can greatly reduce the work of writing SQL statements and improve the development efficiency and reusability of applications. Let's introduce the Hibernate framework from the following aspects. 1. Advantages of the Hibernate framework: object-relational mapping, hiding database access details, making development

In this article, we will see how to perform bulk insert/update in Hibernate. Whenever we execute a sql statement, we do it by making a network call to the database. Now, if we have to insert 10 entries into the database table, then we have to make 10 network calls. Instead, we can optimize network calls by using batch processing. Batch processing allows us to execute a set of SQL statements in a single network call. To understand and implement this, let us define our entity − @EntitypublicclassParent{@Id@GeneratedValue(strategy=GenerationType.AUTO)

Caching helps reduce database network calls when executing queries. Level 1 cache and session linking. It is implemented implicitly. The first level cache exists until the session object exists. Once the session object is terminated/closed, there will be no cached objects. Second level cache works for multiple session objects. It is linked with the session factory. Second level cache objects are available to all sessions using a single session factory. These cache objects will be terminated when a specific session factory is closed. To implement the second level cache we need to add the following dependencies to use the second level cache. <!--https://mvnrepository.com/artifact/net.sf.ehcache/ehcache--><de

1. hibernate mapping configures the class tag, which is used to establish the relationship between the class and the table. name: class name, table: table name id tag, the corresponding relationship between the attribute being established and the primary key in the table property, and the establishment of ordinary attributes in the class. The corresponding relationship with the fields of the table (1) First of all, we must learn how to write the mapping configuration file. Everyone must know that the written mapping configuration file should be in the same package as the entity class, and the name should be class name.hbm.xml. So we need to create a Customer.hbm.xml file under the com.meimeixia.hibernate.demo01 package, but how should its constraints be written? Available in Hiberna
