चूंकि यह एक बहुत ही सामान्य प्रश्न है, इसलिए मैंने यह लेख लिखा है , जिस पर यह उत्तर आधारित है।
चलो हमारे आवेदन निम्नलिखित का उपयोग करता है मान लेते हैं Post, PostComment, PostDetails, और Tagसंस्थाओं, जो एक एक-से-कई रूप है, एक-से-एक, और कई-से-अनेक तालिका रिश्तों :

जेपीए क्राइटेरिया मेटामोडेल कैसे उत्पन्न करें
hibernate-jpamodelgenहाइबरनेट ORM द्वारा प्रदान की उपकरण परियोजना संस्थाओं को स्कैन और जेपीए मानदंड Metamodel उत्पन्न करने के लिए इस्तेमाल किया जा सकता। तुम सब करने की ज़रूरत है निम्नलिखित जोड़ने annotationProcessorPathके लिए maven-compiler-pluginMaven में pom.xmlविन्यास फाइल:
<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>
अब, जब परियोजना संकलित की जाती है, तो आप देख सकते हैं कि targetफ़ोल्डर में, निम्नलिखित जावा कक्षाएं उत्पन्न होती हैं:
> tree target/generated-sources/
target/generated-sources/
└── annotations
└── com
└── vladmihalcea
└── book
└── hpjp
└── hibernate
├── forum
│ ├── PostComment_.java
│ ├── PostDetails_.java
│ ├── Post_.java
│ └── Tag_.java
टैग इकाई मेटामॉडल
यदि Tagइकाई निम्नानुसार मैप की गई है:
@Entity
@Table(name = "tag")
public class Tag {
@Id
private Long id;
private String name;
//Getters and setters omitted for brevity
}
Tag_Metamodel वर्ग इस तरह उत्पन्न होता है:
@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";
}
SingularAttributeबुनियादी के लिए प्रयोग किया जाता है idऔर name Tagजेपीए इकाई गुण।
पोस्ट इकाई मेटामॉडल
Postइकाई इस तरह मैप किया गया है:
@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इकाई दो बुनियादी गुण होते हैं, idऔरtitle एक एक-से-कई, commentsसंग्रह, एक एक-से-एक detailsसंघ है, और एक बहुत-से-अनेक tagsसंग्रह।
Post_Metamodel वर्ग इस प्रकार उत्पन्न होता है:
@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";
}
बुनियादी idऔर titleविशेषताओं, साथ ही साथ एक-से-एक detailsएसोसिएशन, द्वारा प्रतिनिधित्व किया जाता हैSingularAttribute थोड़ी देरcomments और tagsसंग्रह को जेपीए द्वारा दर्शाया जाता है ListAttribute।
PostDetails इकाई Metamodel
PostDetailsइकाई इस तरह मैप किया गया है:
@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
}
SingularAttributeसंबंधित PostDetails_मेटामोडेल वर्ग में जेपीए द्वारा सभी इकाई विशेषताओं का प्रतिनिधित्व किया जा रहा है :
@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";
}
पोस्टकॉममेंट इकाई मेटामॉडल
PostCommentइस प्रकार मैप किया गया है:
@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
}
और, सभी इकाई विशेषताओं को SingularAttributeसंबंधित PostComments_मेटामोडेल वर्ग में जेपीए द्वारा दर्शाया गया है :
@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";
}
जेपीए मानदंड मेटामोडेल का उपयोग करना
जेपीए मेटामॉडल के बिना, एक मानदंड एपीआई क्वेरी जिसे PostCommentउनके संबंधित Postशीर्षक द्वारा फ़िल्टर की गई संस्थाओं को प्राप्त करने की आवश्यकता होती है :
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();
ध्यान दें कि हमने उदाहरण postबनाते समय स्ट्रिंग शाब्दिक का उपयोग किया था Join, और हमने इसका उपयोग कियाtitle संदर्भित करते समय स्ट्रिंग शाब्दिक काPost title ।
जेपीए मेटामॉडल हमें हार्ड-कोडिंग इकाई विशेषताओं से बचने की अनुमति देता है, जैसा कि निम्नलिखित उदाहरण द्वारा चित्रित किया गया है:
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();
यदि आप कोडकोटा जैसे कोड पूरा करने वाले टूल का उपयोग कर रहे हैं, तो जेपीए मानदंड एपीआई प्रश्न लिखना बहुत आसान है। की जाँच करें इस लेख Codota आईडीई प्लगइन के बारे में अधिक जानकारी के लिए।
या, मान लें कि हम और फ़िल्टर करते समय डीटीओ प्रोजेक्शन लाना चाहते हैंPost titlePostDetails createdOn विशेषताओं ।
हम मेटामोडेल का उपयोग ज्वाइन की विशेषताओं को बनाते समय कर सकते हैं, साथ ही डीटीओ प्रोजेक्शन कॉलम एलियास का निर्माण करते समय या इकाई विशेषताओं को संदर्भित करते समय हमें फ़िल्टर करने की आवश्यकता होती है:
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();
बिल्कुल सटीक?