Hibernate INNER JOIN返回实体/模型结果

时间:2016-12-30 14:16:26

标签: java hibernate constructor inner-join jointable

我对使用返回实体/模型结果的Hibernate INNER JOINS感兴趣。

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)

1 个答案:

答案 0 :(得分:0)

让这个工作!非常高兴..

创建了一个新类:

放在我的Hibernate文件夹中只是为了方便/相关:

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

  }
}

我真的希望这对人们有所帮助......