hibernate-enhance-maven-plugin在添加新实体时生成insert + update而不是single insert

时间:2018-03-21 11:55:28

标签: hibernate

我有一个简单的实体,类似的东西:

@Entity
@Data
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;

@NotEmpty
private String name;

@NotNull
@Basic(fetch = LAZY)
@Column(columnDefinition = "BYTEA")
private byte[] content;

}

当我使用spring-data-jpa存储库保存此实体时,我得到这样的单个插入:

insert 
into
    user
    (content, name, id) 
values
    (?, ?, ?)

但是我需要懒洋洋地加载内容字段,所以我添加了maven插件:

<plugin>
    <groupId>org.hibernate.orm.tooling</groupId>
    <artifactId>hibernate-enhance-maven-plugin</artifactId>
    <version>5.0.1.Final</version>
    <executions>
        <execution>
             <configuration>
                 <failOnError>true</failOnError>                   <enableLazyInitialization>true</enableLazyInitialization>
             </configuration>
             <goals>
                 <goal>enhance</goal>
             </goals>
        </execution>
    </executions>
</plugin>

现在我有2个SQL语句而不是1:

Hibernate: 
insert 
into
    user
    (content, name, id) 
values
    (?, ?, ?)
Hibernate: 
update
    user
set
    content=?,
    name=?,
where
    id=?

有人可以解释为什么会这样吗?

更新:在提交flush事件之前抛出。不同之处在于DefaultFlushEntityEventListener.mightBeDirty方法。

启用插件时dirtyProperties = persister.resolveAttributeIndexes(((SelfDirtinessTracker)实体)。$$ _ hibernate_getDirtyAttributes());返回所有属性,当插件被禁用时,所有属性都为null,在第一种情况下,hibernate决定执行update语句,在第二种情况下无需更新。

方法$$ _ hibernate_getDirtyAttributes()在EntityEnhancer中创建,如下所示:

public boolean $$_hibernate_getDirtyAttributes() { 
    return ($$_hibernate_tracker != null && !$$_hibernate_tracker.isEmpty()) || $$_hibernate_areCollectionFieldsDirty;" 
}

$$ _ hibernate_tracker字段包含所有字段中的实体字段,因此它不为null或为空。

看起来这些属性应该在持久或添加之后清理。

没有调用$$ _ hibernate_trackChange,这意味着在持久化后会留下这些属性。我想应该清理一下。

仅在postUpdate中清理这些属性。

1 个答案:

答案 0 :(得分:1)

在我的高性能java持久性GitHub存储库中,您可以找到LazyAttributeWithMultipleEntitiesTest

启用enableLazyInitialization Hibernate增强属性并持久保存这些实体后:

Attachment book = new Attachment();
book.setName("High-Performance Java Persistence");
book.setMediaType(MediaType.PDF);
book.setContent(Files.readAllBytes(bookFilePath));
entityManager.persist(book);

Attachment video = new Attachment();
video.setName("High-Performance Hibernate");
video.setMediaType(MediaType.MPEG_VIDEO);
video.setContent(Files.readAllBytes(videoFilePath));
entityManager.persist(video);

Hibernate生成以下INSERT语句:

Query:["select next_val as id_val from hibernate_sequence for update"], Params:[()]
Query:["update hibernate_sequence set next_val= ? where next_val=?"], Params:[(2, 1)]
Query:["select next_val as id_val from hibernate_sequence for update"], Params:[()]
Query:["update hibernate_sequence set next_val= ? where next_val=?"], Params:[(3, 2)]

Query:["insert into attachment (media_type, name, content, id) values (?, ?, ?, ?)"], Params:[(20, High-Performance Java Persistence, [60, 63, 120, 109, 108, 32, 118, 101, 114, 115, 105, 111, 110, 61, 34, 49, 46, 48, 34, 32, 101, 110, 99, 111, 100, 105, 110, 103, 61, 34, 85, 84, 70, 45, 56, 34, 63, 62, 10, 60, 101, 104, 99, 97, 99, 104, 101, 32, 120, 109, 108, 110, 115, 58, 120, 115, 105, 61, 34, 104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 119, 51, 46, 111, 114, 103, 47, 50, 48, 48, 49, 47, 88, 77, 76, 83, 99, 104, 101, 109, 97, 45, 105, 110, 115, 116, 97, 110, 99, 101, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 120, 115, 105, 58, 110, 111, 78, 97, 109, 101, 115, 112, 97, 99, 101, 83, 99, 104, 101, 109, 97, 76, 111, 99, 97, 116, 105, 111, 110, 61, 34, 101, 104, 99, 97, 99, 104, 101, 46, 120, 115, 100, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 117, 112, 100, 97, 116, 101, 67, 104, 101, 99, 107, 61, 34, 116, 114, 117, 101, 34, 32, 109, 111, 110, 105, 116, 111, 114, 105, 110, 103, 61, 34, 97, 117, 116, 111, 100, 101, 116, 101, 99, 116, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 100, 121, 110, 97, 109, 105, 99, 67, 111, 110, 102, 105, 103, 61, 34, 116, 114, 117, 101, 34, 62, 10, 10, 32, 32, 32, 32, 60, 100, 101, 102, 97, 117, 108, 116, 67, 97, 99, 104, 101, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 109, 97, 120, 69, 108, 101, 109, 101, 110, 116, 115, 73, 110, 77, 101, 109, 111, 114, 121, 61, 34, 49, 48, 48, 48, 48, 48, 34, 10, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 101, 116, 101, 114, 110, 97, 108, 61, 34, 116, 114, 117, 101, 34, 62, 10, 32, 32, 32, 32, 32, 32, 32, 32, 60, 112, 101, 114, 115, 105, 115, 116, 101, 110, 99, 101, 32, 115, 116, 114, 97, 116, 101, 103, 121, 61, 34, 110, 111, 110, 101, 34, 47, 62, 10, 32, 32, 32, 32, 60, 47, 100, 101, 102, 97, 117, 108, 116, 67, 97, 99, 104, 101, 62, 10, 60, 47, 101, 104, 99, 97, 99, 104, 101, 62, 10], 1)]
Query:["insert into attachment (media_type, name, content, id) values (?, ?, ?, ?)"], Params:[(19, High-Performance Hibernate, [35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 35, 32, 80, 54, 83, 112, 121, 32, 79, 112, 116, 105, 111, 110, 115, 32, 70, 105, 108, 101, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 83, 101, 101, 32, 100, 111, 99, 117, 109, 101, 110, 116, 97, 116, 105, 111, 110, 32, 102, 111, 114, 32, 100, 101, 116, 97, 105, 108, 101, 100, 32, 105, 110, 115, 116, 114, 117, 99, 116, 105, 111, 110, 115, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 104, 116, 116, 112, 58, 47, 47, 112, 54, 115, 112, 121, 46, 103, 105, 116, 104, 117, 98, 46, 105, 111, 47, 112, 54, 115, 112, 121, 47, 50, 46, 48, 47, 99, 111, 110, 102, 105, 103, 97, 110, 100, 117, 115, 97, 103, 101, 46, 104, 116, 109, 108, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 13, 10, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 35, 32, 77, 79, 68, 85, 76, 69, 83, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 77, 111, 100, 117, 108, 101, 32, 108, 105, 115, 116, 32, 97, 100, 97, 112, 116, 115, 32, 116, 104, 101, 32, 109, 111, 100, 117, 108, 97, 114, 32, 102, 117, 110, 99, 116, 105, 111, 110, 97, 108, 105, 116, 121, 32, 111, 102, 32, 80, 54, 83, 112, 121, 46, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 79, 110, 108, 121, 32, 109, 111, 100, 117, 108, 101, 115, 32, 108, 105, 115, 116, 101, 100, 32, 97, 114, 101, 32, 97, 99, 116, 105, 118, 101, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 40, 100, 101, 102, 97, 117, 108, 116, 32, 105, 115, 32, 99, 111, 109, 46, 112, 54, 115, 112, 121, 46, 101, 110, 103, 105, 110, 101, 46, 108, 111, 103, 103, 105, 110, 103, 46, 80, 54, 76, 111, 103, 70, 97, 99, 116, 111, 114, 121, 32, 97, 110, 100, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 99, 111, 109, 46, 112, 54, 115, 112, 121, 46, 101, 110, 103, 105, 110, 101, 46, 115, 112, 121, 46, 80, 54, 83, 112, 121, 70, 97, 99, 116, 111, 114, 121, 41, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 80, 108, 101, 97, 115, 101, 32, 110, 111, 116, 101, 32, 116, 104, 97, 116, 32, 116, 104, 101, 32, 99, 111, 114, 101, 32, 109, 111, 100, 117, 108, 101, 32, 40, 80, 54, 83, 112, 121, 70, 97, 99, 116, 111, 114, 121, 41, 32, 99, 97, 110, 39, 116, 32, 98, 101, 32, 32, 32, 35, 13, 10, 35, 32, 100, 101, 97, 99, 116, 105, 118, 97, 116, 101, 100, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 85, 110, 108, 105, 107, 101, 32, 116, 104, 101, 32, 111, 116, 104, 101, 114, 32, 112, 114, 111, 112, 101, 114, 116, 105, 101, 115, 44, 32, 97, 99, 116, 105, 118, 97, 116, 105, 111, 110, 32, 111, 102, 32, 116, 104, 101, 32, 99, 104, 97, 110, 103, 101, 115, 32, 111, 110, 32, 32, 32, 32, 32, 35, 13, 10, 35, 32, 116, 104, 105, 115, 32, 111, 110, 101, 32, 114, 101, 113, 117, 105, 114, 101, 115, 32, 114, 101, 108, 111, 97, 100, 46, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 109, 111, 100, 117, 108, 101, 108, 105, 115, 116, 61, 99, 111, 109, 46, 112, 54, 115, 112, 121, 46, 101, 110, 103, 105, 110, 101, 46, 115, 112, 121, 46, 80, 54, 83, 112, 121, 70, 97, 99, 116, 111, 114, 121, 44, 99, 111, 109, 46, 112, 54, 115, 112, 121, 46, 101, 110, 103, 105, 110, 101, 46, 108, 111, 103, 103, 105, 110, 103, 46, 80, 54, 76, 111, 103, 70, 97, 99, 116, 111, 114, 121, 44, 99, 111, 109, 46, 112, 54, 115, 112, 121, 46, 101, 110, 103, 105, 110, 101, 46, 111, 117, 116, 97, 103, 101, 46, 80, 54, 79, 117, 116, 97, 103, 101, 70, 97, 99, 116, 111, 114, 121, 13, 10, 13, 10, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 35, 32, 67, 79, 82, 69, 32, 40, 80, 54, 83, 80, 89, 41, 32, 80, 82, 79, 80, 69, 82, 84, 73, 69, 83, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 32, 35, 13, 10, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 35, 13, 10, 13, 10, 35, 32, 65, 32, 99, 111, 109, 109, 97, 32, 115, 101, 112, 97, 114, 97, 116, 101, 100, 32, 108, 105, 115, 116, 32, 111, 102, 32, 74, 68, 66, 67, 32, 100, 114, 105, 118, 101, 114, 115, 32, 116, 111, 32, 108, 111, 97, 100, 32, 97, 110, 100, 32, 114, 101, 103, 105, 115, 116, 101, 114, 46, 13, 10, 35, 32, 40, 100, 101, 102, 97, 117, 108, 116, 32, 105, 115, 32, 101, 109, 112, 116, 121, 41, 13, 10, 35, 13, 10, 35, 32, 78, 111, 116, 101, 58, 32, 84, 104, 105, 115, 32, 105, 115, 32, 110, 111, 114, 109, 97, 108, 108, 121, 32, 111, 110, 108, 121, 32, 110, 101, 101, 100, 101, 100, 32, 119, 104, 101, 110, 32, 117, 115, 105, 110, 103, 32, 80, 54, 83, 112, 121, 32, 105, 110, 32, 97, 110, 13, 10, 35, 32, 97, 112, 112, 108, 105, 99, 97, 116, 105, 111, 110, 32, 115, 101, 114, 118, 101, 114, 32, 101, 110, 118, 105, 114, 111, 110, 109, 101, 110, 116, 32, 119, 105, 116, 104, 32, 97, 32, 74, 78, 68, 73, 32, 100, 97, 116, 97, 32, 115, 111, 117, 114, 99, 101, 32, 111, 114, 32, 119, 104, 101, 110, 13, 10, 35, 32, 117, 115, 105, 110, 103, 32, 97, 32, 74, 68, 66, 67, 32, 100, 114, 105, 118, 101, 114, 32, 116, 104, 97, 116, 32, 100, 111, 101, 115, 32, 110, 111, 116, 32, 105, 109, 112, 108, 101, 109, 101, 110, 116, 32, 116, 104, 101, 32, 74, 68, 66, 67, 32, 52, 46, 48, 32, 65, 80, 73, 13, 10, 35, 32, 40, 115, 112, 101, 99, 105, 102, 105, 99, 97, 108, 108, 121, 32, 97, 117, 116, 111, 109, 97, 116, 105, 99, 32, 114, 101, 103, 105, 115, 116, 114, 97, 116, 105, 111, 110, 41, 46, 13, 10, 35, 100, 114, 105, 118, 101, 114, 108, 105, 115, 116, 61, 13, 10, 13, 10, 35, 32, 102, 111, 114, 32, 102, 108, 117, 115, 104, 105], 2)]

因此,不会生成UPDATE。

您可以运行我的测试用例,亲自查看并与您的测试结果进行比较。尝试调试并查看生成UPDATE的原因。也许它不是来自你坚持的同一个实体,而是来自你默默修改的其他实体。