Frage

Gibt es eine JPA 1.0 -Fluent -API/-schnittstelle für das Abfragebuilding? Ich benutze OpenJPA 1.x, also bin ich mit JPA1 fest.

ich fand QueryByProxy, aber sein Maven -Repo funktioniert nicht richtig.

War es hilfreich?

Lösung

Wenn Sie bei JPA 1.0 festhalten, sollten Sie dies verwenden QueryDSL Dies bietet eine fließende Typ -API von Typen, die über JPA übernommen wird. Sie müssen vor 1.6.0 eine Version verwenden, dh 1.5.4 (sie haben zu JPA 2.0 in 1.6.0 umgestellt). Dies ist IMO Ihre beste Option.

Andere Tipps

Kurze Antwort ist nein. Es hängt jedoch von dem von Ihnen verwendeten Anbieter ab. Wenn Sie beispielsweise Hibernate verwenden, können Sie die Kriterien -API immer von Hibernate erhalten. In JPA 1.0 wird dies jedoch nicht unterstützt. In JPA 2.0 wie auch immer es ist.

Sie können das fließende Schnittstellenmuster mit JPA und Hibernate verwenden. Ich schrieb Ein Artikel, der dieses Thema ausführlich erklärt.

Um es zusammenzufassen, können Sie die Setzer so ändern, dass Sie Hibernate verwenden, um die Entität zurückzugeben:

@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;
    }
}

Auf diese Weise können Sie die Eltern und die Kinderhersteller wie diese bauen:

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);
});

Wenn Sie sich für die JPA -Portabilität interessieren, möchten Sie möglicherweise nicht gegen die Java -Bean -Spezifikation verstoßen. In diesem Fall müssen Sie die fließenden Schnittstellenmethoden entlang der regulären Setzer hinzufügen:

@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;
    }
}

Das Entitätsgebäude ist fast identisch mit dem vorherigen:

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);
});
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top