0

I am interested in using Hibernate INNER JOINS that return an entity/model result.

At Hibernate Community Documentation, they write:

Or - assuming that the class Family has an appropriate constructor - as an actual typesafe Java object:

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

For the life of me, I have not been able to build that appropriate constructor. I want to query

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

Should I create a new Java class, let's say Participant_Address.java that reads like this:

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

With constructor:
    public Participant_Address(new Participant(...), String addressType)
halfer
  • 18,701
  • 13
  • 79
  • 158
emm
  • 19
  • 1
  • 6

1 Answers1

0

Got this to work! Very happy..

Created a new class:

Placed in my Hibernate folder just for convenience/relevance:

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

 }

Tested with:

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

  }
}

I really hope that this helps folks...

emm
  • 19
  • 1
  • 6