Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java JPA1.0是否有JPA fluent API/Critera API?I';我正在使用OpenJPA_Java_Hibernate_Jpa_Orm_Openjpa - Fatal编程技术网

Java JPA1.0是否有JPA fluent API/Critera API?I';我正在使用OpenJPA

Java JPA1.0是否有JPA fluent API/Critera API?I';我正在使用OpenJPA,java,hibernate,jpa,orm,openjpa,Java,Hibernate,Jpa,Orm,Openjpa,是否有用于查询构建的JPA1.0 fluent api/接口?我使用的是OpenJPA1.x,所以我只能使用JPA1 我发现,但是它的Maven RePO不正常工作。 < P>如果你被JPA 1所困扰,那么考虑使用它,在JPA之上提供一个流畅的类型化API。您必须使用1.6.0之前的版本,即1.5.4(他们在1.6.0中切换到JPA2.0)。这是您最好的选择。简短的回答是否。但这取决于您使用的提供商。例如,如果您使用的是Hibernate,则始终可以从Hibernate获取CriteriaAPI

是否有用于查询构建的JPA1.0 fluent api/接口?我使用的是OpenJPA1.x,所以我只能使用JPA1


我发现,但是它的Maven RePO不正常工作。

< P>如果你被JPA 1所困扰,那么考虑使用它,在JPA之上提供一个流畅的类型化API。您必须使用1.6.0之前的版本,即1.5.4(他们在1.6.0中切换到JPA2.0)。这是您最好的选择。

简短的回答是否。但这取决于您使用的提供商。例如,如果您使用的是Hibernate,则始终可以从Hibernate获取CriteriaAPI。然而,在JPA1.0中,这是不受支持的。在JPA2.0中,您可以将与JPA和Hibernate一起使用

总之,如果使用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;
    }
}
如果您关心JPA的可移植性,那么您可能不想违反Java Bean规范,在这种情况下,您需要添加Fluent接口方法以及常规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;
    }
}

这并不是很重要(至少我的答案不是这样),但你将Hibernate(标题中)和OpenJPA(正文中)混合在一起。谢谢,我将编辑以供将来参考QueryDSL还支持JDO、Java集合、SQL和Lucene。(有偏见的评论,因为我是Querydsl开发人员)
@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);
});