是否有任何jpa 1.0流利的api /接口用於查詢構建?我使用的是openjpa 1.x,所以我被JPA1困住了。JPA 1.0中是否有任何JPA流利的API/Critera API?我正在使用OpenJPA
我發現QueryByProxy,但其maven回購工作不正常。
是否有任何jpa 1.0流利的api /接口用於查詢構建?我使用的是openjpa 1.x,所以我被JPA1困住了。JPA 1.0中是否有任何JPA流利的API/Critera API?我正在使用OpenJPA
我發現QueryByProxy,但其maven回購工作不正常。
如果您遇到JPA 1.0,請考慮使用Querydsl,它在JPA之上提供流暢的類型安全API。您必須使用1.6.0之前的版本,即1.5.4(他們在1.6.0中轉換爲JPA 2.0)。這是IMO最好的選擇。
Querydsl還支持JDO,Java集合,SQL和Lucene。 (有偏見的評論,因爲我是Querydsl開發者) – 2010-08-08 09:42:29
簡短的回答是沒有。但是,這取決於您使用的提供商。例如,如果您使用的是Hibernate,那麼您總是可以從休眠狀態獲取Criteria API。但是在JPA 1.0中不支持。但是在JPA 2.0中。
您可以在JPA和Hibernate中使用Fluent接口模式。我寫了an article which explains this topic in great detail。
要總結一下,如果你正在使用Hibernate,你可以修改制定者返回實體:
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
public Post() {}
public Post(String title) {
this.title = title;
}
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
mappedBy = "post"
)
private List<PostComment> comments = new ArrayList<>();
public Long getId() {
return id;
}
public Post setId(Long id) {
this.id = id;
return this;
}
public String getTitle() {
return title;
}
public Post setTitle(String title) {
this.title = title;
return this;
}
public List<PostComment> getComments() {
return comments;
}
public Post addComment(PostComment comment) {
comment.setPost(this);
comments.add(comment);
return this;
}
}
@Entity(name = "PostComment")
@Table(name = "post_comment")
public class PostComment {
@Id
@GeneratedValue
private Long id;
private String review;
private Date createdOn;
@ManyToOne
private Post post;
public Long getId() {
return id;
}
public PostComment setId(Long id) {
this.id = id;
return this;
}
public String getReview() {
return review;
}
public PostComment setReview(String review) {
this.review = review;
return this;
}
public Date getCreatedOn() {
return createdOn;
}
public PostComment setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
return this;
}
public Post getPost() {
return post;
}
public PostComment setPost(Post post) {
this.post = post;
return this;
}
}
這種方式,你可以建立家長和孩子的實體是這樣的:
doInJPA(entityManager -> {
Post post = new Post()
.setId(1L)
.setTitle("High-Performance Java Persistence")
.addComment(
new PostComment()
.setReview("Awesome book")
.setCreatedOn(Timestamp.from(
LocalDateTime.now().minusDays(1).toInstant(ZoneOffset.UTC))
)
)
.addComment(
new PostComment()
.setReview("High-Performance Rocks!")
.setCreatedOn(Timestamp.from(
LocalDateTime.now().minusDays(2).toInstant(ZoneOffset.UTC))
)
)
.addComment(
new PostComment()
.setReview("Database essentials to the rescue!")
.setCreatedOn(Timestamp.from(
LocalDateTime.now().minusDays(3).toInstant(ZoneOffset.UTC))
)
);
entityManager.persist(post);
});
如果你關心JPA的便攜性,那麼你可能不想違反Java Bean的規範,在這種情況下,你需要添加流利的接口方法一起定期制定者:
@Entity(name = "Post")
@Table(name = "post")
public class Post {
@Id
private Long id;
private String title;
public Post() {}
public Post(String title) {
this.title = title;
}
@OneToMany(
cascade = CascadeType.ALL,
orphanRemoval = true,
mappedBy = "post"
)
private List<PostComment> comments = new ArrayList<>();
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Post id(Long id) {
this.id = id;
return this;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Post title(String title) {
this.title = title;
return this;
}
public List<PostComment> getComments() {
return comments;
}
public Post addComment(PostComment comment) {
comments.add(comment.post(this));
return this;
}
}
@Entity(name = "PostComment")
@Table(name = "post_comment")
public class PostComment {
@Id
@GeneratedValue
private Long id;
private String review;
private Date createdOn;
@ManyToOne
private Post post;
public Long getId() {
return id;
}
public PostComment setId(Long id) {
this.id = id;
return this;
}
public String getReview() {
return review;
}
public void setReview(String review) {
this.review = review;
}
public PostComment review(String review) {
this.review = review;
return this;
}
public Date getCreatedOn() {
return createdOn;
}
public void setCreatedOn(Date createdOn) {
this.createdOn = createdOn;
}
public PostComment createdOn(Date createdOn) {
this.createdOn = createdOn;
return this;
}
public Post getPost() {
return post;
}
public void setPost(Post post) {
this.post = post;
}
public PostComment post(Post post) {
this.post = post;
return this;
}
}
實體建築幾乎等同於前一個:
doInJPA(entityManager -> {
Post post = new Post()
.id(1L)
.title("High-Performance Java Persistence")
.addComment(new PostComment()
.review("Awesome book")
.createdOn(Timestamp.from(
LocalDateTime.now().minusDays(1).toInstant(ZoneOffset.UTC))
)
)
.addComment(new PostComment()
.review("High-Performance Rocks!")
.createdOn(Timestamp.from(
LocalDateTime.now().minusDays(2).toInstant(ZoneOffset.UTC))
)
)
.addComment(new PostComment()
.review("Database essentials to the rescue!")
.createdOn(Timestamp.from(
LocalDateTime.now().minusDays(3).toInstant(ZoneOffset.UTC))
)
);
entityManager.persist(post);
});
這並不重要(至少對於我的答案),但你混合休眠(標題)和OpenJPA(體內) 。 – 2010-08-06 17:46:03
謝謝,我將編輯以備將來參考 – 2010-08-24 16:11:29