是否有任何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