是否有任何 jpa 1.0 fluent api/interface 用于查询构建?我正在使用openjpa 1.x,所以我坚持使用JPA1。
我找到了 QueryByProxy,但它的 maven repo 工作不正常。
如果您坚持使用 JPA 1.0,那么请考虑使用在JPA 之上提供流式类型安全 API 的Querydsl。您必须使用 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。
总结一下,如果您使用的是 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 规范,在这种情况下,您需要添加 Fluent Interface 方法以及常规 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 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);
});