2016-12-30 44 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 

    } 
} 

我真的希望这帮助人们...