2016-12-30 11 views
0

エンティティ/モデル結果を返すHibernate INNER JOINSの使用に興味があります。エンティティ/モデル結果を返すHibernate INNER JOIN

または - クラスの家族が適切なコンストラクタがあると仮定して - 実際の型保証されたJavaオブジェクトとして:Hibernate Community Documentation

、彼らが書いた私の人生のために

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

を、私はその適切なコンストラクタを構築することができませんでした。私は、新しいJavaクラスを作成する必要があります

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

を照会したい、このように読み取るのが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

[this](http://stackoverflow.com/questions/4027805/new-object-with-hq l)あなたの質問に既に回答している投稿 –

+0

halfer ...なぜ編集していますか? – emm

答えて

0

はこれが動作するようになりました!非常に満足..

は、新しいクラスを作成しました:私は本当にこの願う

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 

    } 
} 

:でテスト

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

} 

だけ便利/関連性のための私のHibernateのフォルダに配置します人々を助けます...