JPA 1.0にJPA Fluent API / Critera APIはありますか? OpenJPAを使用しています
質問
クエリビルディング用のJPA 1.0 Fluent API/インターフェイスはありますか? OpenJPA 1.xを使用しているので、JPA1にこだわっています。
私は見つけました querybyproxy, 、しかし、そのmavenリポジトリは適切に機能していません。
解決
JPA 1.0にこだわっている場合は、使用することを検討してください querydsl これにより、JPAの上にFluent TypeSafe APIが提供されます。 1.6.0、つまり1.5.4より前にバージョンを使用する必要があります(1.6.0でJPA 2.0に切り替えました)。これはあなたの最良の選択肢です。
他のヒント
短い答えはノーです。ただし、使用しているプロバイダーに依存します。たとえば、Hibernateを使用している場合、Hibernateからいつでも基準APIを取得できます。ただし、JPA 1.0では、これはサポートされていません。 JPA 2.0ではそうです。
JPAとHibernateを使用して、流fluentインターフェイスパターンを使用できます。私が書いた このトピックを非常に詳細に説明する記事.
要約するには、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インターフェイスメソッドを追加する必要があります。
@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);
});
所属していません StackOverflow