2016-12-30 41 views
0

我有興趣使用返回實體/模型結果的Hibernate INNER JOINS。Hibernate INNER JOIN返回實體/模型結果

Hibernate Community Documentation,他們寫道:

或者 - 假設類Family有一個合適的構造函數 - 作爲實際的類型安全的Java對象:

select new Family(mother, mate, offspr) 
     from DomesticCat as mother 
     join mother.mate as mate 
     left join mother.kittens as offspr 

對於我的生活,我一直無法構建適當的構造函數。我想查詢

Select new Participant(part, addr.adddressType) 
    from Participant part 
    INNER JOIN part.adddresses addr 

我應該創建一個新的Java類,讓我們說Participant_Address.java,上面寫着這樣的:

Select new Participant_Address (part, addr.adddressType) 
    from Participant part 
    INNER JOIN part.adddresses addr 

With constructor: 
    public Participant_Address(new Participant(...), String addressType) 
+0

看看[這個](http://stackoverflow.com/questions/4027805/new-object-with-hq l)後,你的問題已經被回答 –

+0

halfer ...爲什麼編輯? – emm

回答

0

得到這個工作!很開心..

創建一個新的類:

在我的休眠文件夾放置只是爲了方便/相關性:

package echomarket.hibernate; 

public class ParticipantAddress implements java.io.Serializable { 

    private Participant part; 
    private String addressType; 

    public ParticipantAddress() { 
    } 

    public ParticipantAddress(Participant part, String addressType) { 
    this.part = part; 
    this.addressType = addressType; 
} 

    public Participant getPart() { 
    return part; 
} 

    public void setPart(Participant part) { 
    this.part = part; 
    } 

    public String getAddressType() { 
    return addressType; 
    } 

    public void setAddressType(String addressType) { 
    this.addressType = addressType; 
    } 

} 

測試了:

package echomarket.hibernate; 

import echomarket.hibernate.HibernateUtil; 
import java.util.List; 
import org.hibernate.Session; 
import org.hibernate.Transaction; 

public class TestHib { 

    public static void main(String[] args) { 
     Session session = null; 
    Transaction tx = null; 
    List result = null; 
     String query = null; 
    try { 
     session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     tx = session.beginTransaction(); 
    try { 
    query = "SELECT new echomarket.hibernate.ParticipantAddress(part, addr.addressType) " 
       + " from Participant part " 
       + " INNER JOIN part.addresses addr " 
       + " WHERE addr.addressType = 'primary' AND part.participant_id = '603aec80-3e31-451d-9ada-bc5c9d75b569' GROUP BY part.participant_id, addr.addressType"; 
    System.out.println(query); 
     result = session.createQuery(query) 
      .list(); 
    tx.commit(); 
    } catch (Exception e) { 
     System.out.println("Error result/commit in TestHib"); 
     e.printStackTrace(); 
     tx.rollback(); 
    } finally { 
     tx = null; 
     session = null; 
    } 
    /// typically check that result is not null, and in my case that result.size() == 1 
    echomarket.hibernate.ParticipantAddress hold = (echomarket.hibernate.ParticipantAddress)result.get(0); 
    Participant pp = (Participant) hold.getPart(); /// Got my Participant record 
    System.out.println("wait"); /// put a break here so I could evaluate return on result, hold and pp 

    } 
} 

我真的希望這幫助人們...