是否有任何jpa 1.0流畅的api /接口用于查询构建?我正在使用openjpa 1.x,所以我坚持使用JPA1。
我找到了QueryByProxy,但它的maven repo工作不正常。
如果您坚持使用JPA 1.0,那么请考虑使用Querydsl,它在JPA之上提供流畅的类型安全API。您必须使用1.6.0之前的版本,即1.5.4(他们在1.6.0中切换到JPA 2.0)。这是IMO你最好的选择。
简短的回答是否定的。但是,这取决于您使用的提供商。例如,如果你正在使用Hibernate,你总是可以从hibernate获取Criteria api。但是在JPA 1.0中,这不受支持。但是在JPA 2.0中。
您可以将Fluent Interface Pattern与JPA和Hibernate一起使用。我写了an article which explains this topic in great detail。
总结一下,如果你正在使用Hibernate,你可以修改setter来返回实体:
@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规范,在这种情况下,您需要在常规setter中添加Fluent Interface方法:
@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);
});