2010-08-06 8 views
4

Existe-t-il une API/interface PHP 1.0 pour la création de requêtes? J'utilise openjpa 1.x, donc je suis coincé avec JPA1.Existe-t-il une API API/Critera API JPA pour JPA 1.0? J'utilise OpenJPA

J'ai trouvé QueryByProxy, mais son repo maven ne fonctionne pas correctement.

+1

Ce n'est pas vraiment important (du moins pas pour ma réponse) mais vous mélangez Hibernate (dans le titre) et OpenJPA (dans le corps) . –

+0

Merci, je vais éditer pour les futures références –

Répondre

2

Si vous êtes bloqué avec JPA 1.0, alors pensez à utiliser Querydsl qui fournit une API typesafe fluide au-dessus de JPA. Vous devrez utiliser une version antérieure à 1.6.0, c'est-à-dire 1.5.4 (ils sont passés à JPA 2.0 dans la version 1.6.0). C'est IMO votre meilleure option.

+1

Querydsl supporte aussi JDO, les collections Java, SQL et Lucene. (commentaire partial, comme je suis un développeur Querydsl) –

0

La réponse courte est non. Cependant, cela dépend du fournisseur que vous utilisez. Par exemple, si vous utilisez Hibernate, vous pouvez toujours obtenir l'API Criteria depuis Hibernate. Cependant, dans JPA 1.0, ceci n'est pas supporté. Dans JPA 2.0 cependant, c'est.

0

Vous pouvez utiliser le modèle d'interface Fluent avec JPA et Hibernate. J'ai écrit an article which explains this topic in great detail.

Pour résumer, si vous utilisez Hibernate, vous pouvez simplement modifier les setters pour retourner l'entité:

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

De cette façon, vous pouvez construire les parents et les entités enfant comme celui-ci:

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

Si vous vous souciez de la portabilité JPA, vous voudrez peut-être de ne pas violer la spécification Java Bean, dans ce cas, vous devez ajouter les méthodes d'interface Courant le long des setters réguliers:

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

Le bâtiment de l'entité est presque identique à la précédente:

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