开发者

JPA or Play Framework list from query joining 2 tables

Mainly I work with JSF so am totally new to this annotation subject

If anyone can help

I wanna a list from this query

SELECT  f.CODE  ,f.NAME || '-' || e.NAME  
FROM FS.ELIGIBLE e  RIGHT
OUTER JOIN FS.FINANCIAL_SUPPORT f ON e.CODE = f.CODE ; 

The query above retrieves a li开发者_如何学Cst from 2 tables and concatenating the name field from both tables!!

How can i do this in JPA or in play with another query supported by Play Framework ???


Have a read of the Play Framework documentation, specifically the part about JPA and your Domain Model.

You can access the entity manager at any time by calling

EntityManager entityManager = JPA.em();

Using this you can create any query that you want, even a "Native" Query. For example:

List<Object> results = JPA.em().createNativeQuery(
    "SELECT  f.CODE  ,f.NAME || '-' || e.NAME "+
    "FROM FS.ELIGIBLE e  RIGHT "+
    "OUTER JOIN FS.FINANCIAL_SUPPORT f ON e.CODE = f.CODE").getResultList()


JPA is not like relational database system which you can do your queries like join, left join or outer joins it is a mapping technology of objects. You can also do the same fetch just like those RDBMS counterparts but with a different approach.

What you have to do is make an Object then relate your second Object to your first Object, that is the proper way to relate 2 or more Objects. The Objects I'm talking about is your Table. See my example below:

  1. Table1: Items.java
  2. ...
  3. // do your imports here ...
  4. // do your annotations here like
  5. @Entity
  6. @Table(name="Items")
  7. public class Items implements Serializable {
  8.    private String id;
  9.    private String itemno;
  10.    private String description;
  11.    private Set<Vendors> vendors;      //this is the 2nd table (1:n relationship)
  12.    ...
  13.    // don't forget your constructor
  14.    // in your setter and getter for Vendors do the ff:
  15.    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
  16.    @JoinColumn(name="id")
  17.    public Set<Vendors> getVendors() {
  18.       return vendors;
  19.    }
  20.    public void setVendors(Set<Vendors> vendors) {
  21.      this.vendors = vendors;
  22.    }
  23.   ...
  24. }

Table2: Vendors.java

  1. @Entity
  2. @Table(name="Vendors")
  3. public class Vendors implements Serializable {
  4.    private long id;
  5.    private String company;
  6.    private String contact;
  7.    private String sequence;
  8.    ...
  9.    public Vendors() { }
  10.    ...
  11.    // in your setter & getter
  12.   @Id
  13.   public long getId() {
  14.      return id;
  15.   }
  16.    public void setId(long id) {
  17.       this.id = id;
  18.    }
  19.      ...
  20.   }

Now on your query, just do a regular select as in the ff:

  1. public void makeQuery(String seq) {
  2.    EntityManagerFactory emf = Persistence.createEntityManagerFactory(...);
  3.    EntityManager em = emf.createEntityManager();
  4.   TypedQuery<Items> query = em.createQuery(" SELECT i, j.contact, j.company, j.sequence FROM Items i LEFT OUTER JOIN i.vendors j WHERE i.vendors.sequence = :seq
  5.       ORDER BY i.id", Items.class);
  6.   List<Items> items = query.setParameter("sequence", seq).getResultList();
  7.    ...
  8. }

Now you can refer to your 2nd table Vendors by using items.vendors.company ... and so on.

One disadvantage with this one is that, JPA make its related objects in the form of Set, see the declaration in Table1 (Items) - private Set vendors. Since it was a Set, the sequence is not in order it was received, unlike using List.

Hope this will help ...

Regards, Nelson Deogracias

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜