查询构建是否有任何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)。这是IMO的最佳选择。

其他提示

简短的答案是否定的。但是,这取决于您正在使用的提供商。例如,如果您正在使用Hibernate,则可以随时从Hibernate获得标准API。但是,在JPA 1.0中,这不支持。在JPA 2.0中。

您可以使用JPA和Hibernate的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 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);
});
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top