Java 如何生成JPA实体元模型?
本着与JPA2.0相关的类型安全精神,JPA2.0还具有支持实体表示的API 是否有人知道此API的完整功能实现(生成元模型而不是手动创建元模型类)?如果有人也知道在Eclipse中设置此功能的步骤,那就太棒了(我假设它与设置注释处理器一样简单,但您永远不知道) 编辑: 只是偶然发现。但问题仍然存在,因为我找不到jar的任何下载链接 编辑2: 我问这个问题已经有一段时间了,但我想我会回来添加一个链接到 如果有人也知道在Eclipse中设置此功能的步骤,那就太棒了(我认为这就像设置注释处理器一样简单,但你永远不知道) 是的。以下是各种JPA 2.0实现的实现和说明: 日食Java 如何生成JPA实体元模型?,java,hibernate,jpa,annotation-processing,metamodel,Java,Hibernate,Jpa,Annotation Processing,Metamodel,本着与JPA2.0相关的类型安全精神,JPA2.0还具有支持实体表示的API 是否有人知道此API的完整功能实现(生成元模型而不是手动创建元模型类)?如果有人也知道在Eclipse中设置此功能的步骤,那就太棒了(我假设它与设置注释处理器一样简单,但您永远不知道) 编辑: 只是偶然发现。但问题仍然存在,因为我找不到jar的任何下载链接 编辑2: 我问这个问题已经有一段时间了,但我想我会回来添加一个链接到 如果有人也知道在Eclipse中设置此功能的步骤,那就太棒了(我认为这就像设置注释处理器一样简
org.hibernate.jpamodelgen.jpametamodelenticyprocessor
org.apache.openjpa.persistence.meta.AnnotationProcessor6
org.datanucleus.jpa.JPACriteriaProcessor
最新的Hibernate实现可从以下站点获得:
- 我们需要
org.hibernate.org:hibernatejpamodelgen
- 处理器类是org.hibernate.jpamodelgen.jpametamodelenticyprocessor
org.hibernate.orm
冬眠
${version.hibernate jpamodelgen}
假如
作为处理器休眠
org.bsc.maven
maven处理器插件
过程
生成源
-AaddGeneratedAnnotation=false
org.hibernate.jpamodelgen.jpametamodelenticyprocessor
org.hibernate.orm
冬眠
${version.hibernate jpamodelgen}
OpenJPA
- 我们需要
org.apache.openjpa:openjpa
- 处理器类是org.apache.openjpa.persistence.meta.AnnotationProcessor6
- OpenJPA似乎需要额外的元素
true
org.apache.openjpa
openjpa
假如
org.apache.maven.plugins
maven编译器插件
-Aopenjpa.metamodel=true
作为处理器的OpenJPA
org.bsc.maven
maven处理器插件
过程
过程
生成源
org.apache.openjpa.persistence.meta.AnnotationProcessor6
真的
org.apache.openjpa
openjpa
${version.openjpa}
日食
- 我们需要
org.eclipse.persistence:org.eclipse.persistence.jpa.modelgen.processor
- 处理器类是org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
- EclipseLink需要
persistence.xml
org.eclipse.persistence
org.eclipse.persistence.jpa.modelgen.processor
假如
EclipseLink作为处理器
org.bsc.maven
maven处理器插件
过程
生成源
org.eclipse.persistence.internal.jpa.modelgen.CanonicalModelProcessor
-Aeclipselink.persistencexml=src/main/resources-${environment.id}/META-INF/persistence.xml
org.eclipse.persistence
org.eclipse.persistence.jpa.modelgen.processor
${version.eclipselink}
数据核
- 我们需要
org.datanucleus:datanucleus jpa查询
- 处理器类是org.datanucleus.jpa.query.JPACriteriaProcessor
org.datanucleus
datanucleus jpa查询
假如
DataNucleus作为处理器
org.bsc.maven
maven处理器插件
过程
过程
生成源
org.datanucleus.jpa.query.jpacriteria处理器
org.datanucleus
datanucleus jpa查询
${version.datanucleus}
Eclipse通过Dali提供的JPA2.0支持(包含在“EclipseIDEforJEE开发人员”中)有自己的元模型生成器与Eclipse集成
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.5.1</version>
<scope>provided</scope>
</dependency>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-jpamodelgen</artifactId>
<version>${hibernate.version}</version>
</annotationProcessorPath>
</annotationProcessorPaths>
</configuration>
</plugin>
> tree target/generated-sources/
target/generated-sources/
└── annotations
└── com
└── vladmihalcea
└── book
└── hpjp
└── hibernate
├── forum
│ ├── PostComment_.java
│ ├── PostDetails_.java
│ ├── Post_.java
│ └── Tag_.java
@Entity
@Table(name = "tag")
public class Tag {
@Id
private Long id;
private String name;
//Getters and setters omitted for brevity
}
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(Tag.class)
public abstract class Tag_ {
public static volatile SingularAttribute<Tag, String> name;
public static volatile SingularAttribute<Tag, Long> id;
public static final String NAME = "name";
public static final String ID = "id";
}
@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
}
@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";
}
@Entity
@Table(name = "post_details")
public class PostDetails {
@Id
@GeneratedValue
private Long id;
@Column(name = "created_on")
private Date createdOn;
@Column(name = "created_by")
private String createdBy;
@OneToOne(fetch = FetchType.LAZY)
@MapsId
@JoinColumn(name = "id")
private Post post;
//Getters and setters omitted for brevity
}
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(PostDetails.class)
public abstract class PostDetails_ {
public static volatile SingularAttribute<PostDetails, Post> post;
public static volatile SingularAttribute<PostDetails, String> createdBy;
public static volatile SingularAttribute<PostDetails, Long> id;
public static volatile SingularAttribute<PostDetails, Date> createdOn;
public static final String POST = "post";
public static final String CREATED_BY = "createdBy";
public static final String ID = "id";
public static final String CREATED_ON = "createdOn";
}
@Entity
@Table(name = "post_comment")
public class PostComment {
@Id
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
private Post post;
private String review;
//Getters and setters omitted for brevity
}
@Generated(value = "org.hibernate.jpamodelgen.JPAMetaModelEntityProcessor")
@StaticMetamodel(PostComment.class)
public abstract class PostComment_ {
public static volatile SingularAttribute<PostComment, Post> post;
public static volatile SingularAttribute<PostComment, String> review;
public static volatile SingularAttribute<PostComment, Long> id;
public static final String POST = "post";
public static final String REVIEW = "review";
public static final String ID = "id";
}
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<PostComment> query = builder.createQuery(PostComment.class);
Root<PostComment> postComment = query.from(PostComment.class);
Join<PostComment, Post> post = postComment.join("post");
query.where(
builder.equal(
post.get("title"),
"High-Performance Java Persistence"
)
);
List<PostComment> comments = entityManager
.createQuery(query)
.getResultList();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<PostComment> query = builder.createQuery(PostComment.class);
Root<PostComment> postComment = query.from(PostComment.class);
Join<PostComment, Post> post = postComment.join(PostComment_.post);
query.where(
builder.equal(
post.get(Post_.title),
"High-Performance Java Persistence"
)
);
List<PostComment> comments = entityManager
.createQuery(query)
.getResultList();
CriteriaBuilder builder = entityManager.getCriteriaBuilder();
CriteriaQuery<Object[]> query = builder.createQuery(Object[].class);
Root<PostComment> postComment = query.from(PostComment.class);
Join<PostComment, Post> post = postComment.join(PostComment_.post);
query.multiselect(
postComment.get(PostComment_.id).alias(PostComment_.ID),
postComment.get(PostComment_.review).alias(PostComment_.REVIEW),
post.get(Post_.title).alias(Post_.TITLE)
);
query.where(
builder.and(
builder.like(
post.get(Post_.title),
"%Java Persistence%"
),
builder.equal(
post.get(Post_.details).get(PostDetails_.CREATED_BY),
"Vlad Mihalcea"
)
)
);
List<PostCommentSummary> comments = entityManager
.createQuery(query)
.unwrap(Query.class)
.setResultTransformer(Transformers.aliasToBean(PostCommentSummary.class))
.getResultList();
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<annotationProcessorPaths>
<annotationProcessorPath>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.jpa.modelgen.processor</artifactId>
<version>2.7.7</version>
</annotationProcessorPath>
</annotationProcessorPaths>
<compilerArgs>
<arg>-Aeclipselink.persistencexml=src/main/resources/META-INF/persistence.xml</arg>
</compilerArgs>
</configuration>
</plugin>