Hibernate6.6 集成构建工具——Bytecode Enhancement
字节码增强工具
Hibernate performs bytecode enhancement through its org.hibernate.bytecode.enhance.spi.Enhancer contract. These build time tools provide a way to incorporate configuration and execution of the enhancer into a build.
当前版本只对增加了注解的类进行增强。
它是JPA的一个标准的部分。(原话:The Metamodel Generator is a standard part of JPA.)。所以它很重要。
运行时字节码增强
Hibernate can also perform run-time bytecode enhancement when used in Jakarta EE compliant containers through jakarta.persistence.spi.ClassTransformer. See the documentation of your container for any additional details. Run-time enhancement is controlled through 3 true/false settings (all of which default to false):
hibernate.enhancer.enableDirtyTracking
Whether to enhance the model for dirty-tracking. This setting is deprecated for removal without a replacement.
hibernate.enhancer.enableLazyInitialization
Whether to enhance the model for lazy loading at the attribute level. This allows even basic types to be fetched lazily. It also allows definition of fetch groups (LazyGroup). This setting is deprecated for removal without a replacement.
hibernate.enhancer.enableAssociationManagement
Whether to automatically synchronize a bidirectional association when only one side is changed.
静态元模型类生成 (Static Meatamode Generator)
Jakarta Persistence 定义了一套安全类型的Criteria Api(规准接口),它允许通过强类型的方式结合所谓的静态元模型类构造Criteria查询。Hibernate的静态元模型生成器,可以在org.hibernate.orm:hibernate-jpamodelgen支持下,使用注解驱使来生成。
Hibernate的静态元模型生成器拥有很多其他的功能。参阅:Hibernate Static Metamodel Generator
生成器的使用,通过运行javac -processorpath 的方式。
maven集成生成器工具
currentHibernateVersion替换成你的版本,我的是6.6.4
properties 点击查看代码
<hibernate.version>6.6.4.Final</hibernate.version>
dependency 点击查看代码
<!--自动生成元模型等功能支持 b-->
<dependency>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</dependency>
plugin 点击查看代码
<build>
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>...</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.orm</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>$currentHibernateVersion</version>
<!-- Optionally exclude transitive dependencies -->
<exclusions>
<exclusion>
<groupId>org.sample</groupId>
<artifactId>sample-dependency</artifactId>
</exclusion>
</exclusions>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
[...]
</plugins>
</build>
Post.java点击查看代码
@Entity
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
@OneToMany(
mappedBy = "post",
cascade = CascadeType.ALL,
orphanRemoval = true
)
private List<PostComment> comments = new ArrayList<>();
@OneToOne(
mappedBy = "post",
cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
@LazyToOne(LazyToOneOption.NO_PROXY)
private PostDetails details;
@ManyToMany
@JoinTable(
name = "post_tag",
joinColumns = @JoinColumn(name = "post_id"),
inverseJoinColumns = @JoinColumn(name = "tag_id")
)
private List<Tag> tags = new ArrayList<>();
//Getters and setters omitted for brevity
}
编译----生成的------》
Post_.java
在项目 target/generated-sources/annotations里面
Post_.java点击查看代码
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Post.class)
public abstract class Post_ {
public static volatile ListAttribute<Post, PostComment> comments;
public static volatile SingularAttribute<Post, PostDetails> details;
public static volatile SingularAttribute<Post, Long> id;
public static volatile SingularAttribute<Post, String> title;
public static volatile ListAttribute<Post, Tag> tags;
public static final String COMMENTS = "comments";
public static final String DETAILS = "details";
public static final String ID = "id";
public static final String TITLE = "title";
public static final String TAGS = "tags";
}
浙公网安备 33010602011771号