Hibernate dynamic-insert attriburte example

摘要: This tutorial shows how to use the dynamic-insert attribute or @DynamicInsert annotation used by Hibernate. This attribute/annotation tells Hibernate whether to include null properties in the SQL INSERT statement.

This tutorial shows how to use the dynamic-insert attribute or @DynamicInsert annotation used by Hibernate. This attribute/annotation tells Hibernate whether to include null properties in the SQL INSERT statement.

Dynamic-insert=false

By default, dynamic-insert is set to false. Which means all properties are included in the SQL INSERT statement.

For example, we have an Event object which only contains a name.

Event event = new Event();
event.setName("Hibernate dynamic-insert - @DynamicInsert - example");
event.setLocation(null);
event.setDate(null);
em.persist(event);

Hibernate executes the following query.

Hibernate: 
    insert 
    into
        Event
        (date, location, name) 
    values
        (?, ?, ?)

Note: Hibernate includes all properties in the SQL Statement.

Dynamic-insert=true

When we set the dynamic-insert attribute to true, Hibernate will generate a SQL INSERT without null values.

For example, we have an Event object which only contains a name.

Event event = new Event();
event.setName("Hibernate dynamic-insert - @DynamicInsert - example");
event.setLocation(null);
event.setDate(null);
em.persist(event);

Hibernate executes the following query.

Hibernate: 
    insert 
    into
        Event
        (name) 
    values
        (?)

Note: Hibernate includes only not-null properties in the SQL Statement.

What about performance?

Let’s talk about performance, because you must be thinking that dynamic-insert is always the way to go, right? This is not the case.

Hibernate caches the actual SELECT, INSERT and UPDATE SQL strings for each entity. This results in not having to re-create these statements every time you want to find, persist or update an entity.

However, when using dynamic-insert, Hibernate has to generate the corresponding SQL strings each time. This results in a performance cost on the Hibernate side. In other words, there is a trade-off between overhead on the database side and on the Hibernate side.

Configuration

You can configure the dynamic-insert on a – per entity basis – using either the @DynamicInsert annotation or the dynamic-insert XML Mapping attribute.

Annotation Mapping

import org.hibernate.annotations.DynamicInsert;

@Entity
@DynamicInsert
public class Event implements Serializable {
    ...
}

XML Mapping

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
    <class name="com.memorynotfound.hibernate.Event" dynamic-insert="true">

        ...

    </class>
</hibernate-mapping>

Download

上一篇: Hibernate Dynamic-Update Attriburte Example
下一篇: Hibernate @Embeddable and @Embedded Annotation Example
 评论 ( What Do You Think )
名称
邮箱
网址
评论
验证
   
 

 


  • 微信公众号

  • 我的微信

站点声明:

1、一号门博客CMS,由Python, MySQL, Nginx, Wsgi 强力驱动

2、部分文章或者资源来源于互联网, 有时候很难判断是否侵权, 若有侵权, 请联系邮箱:summer@yihaomen.com, 同时欢迎大家注册用户,主动发布无版权争议的 文章/资源.

3、鄂ICP备14001754号-3, 鄂公网安备 42280202422812号