0

Need a help on hibernate join.

Problem - I have three hibernate pojo classes. 1. ReportRequest 2. AnalysisType 3. Application

I created the tables from the hibernate and all of them are created successfully. So it seems that there are no issue in the hibernate mapping.

ReportRequest contains a composite primary key - ReportRequestPk. ReportRequest has a 'ManyToOne' relationship with Application which is also a part of the composite primary key. ReportRequest has a 'ManyToOne' relationship with AnalysisType as well.

I am trying to join ReportRequest with Application to put a restriction on the appKey property of Application. It is not working. But if I try to join AnalysisType with ReportRequest with some restriction on any property of AnalysisType then it is working correctly. If I keep Application within ReportRequet i.e not a part of the composite primary key, then also the restriction on appKey is working perfectly. It is not working if the Application is a part of the composite primary key. I tried with 'createAlias' , 'fetchMode', 'createCriteria'. Please find the pojo classes and the code snippet for the criteria query. I cannot use HQL.

@Entity
@Table(name = "t_report_request")
public class ReportRequest implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    private ReportRequestPk primaryKey;

    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "analysis_type", nullable = false)
    private AnalysisType analysisType;
}

@Embeddable
public class ReportRequestPk implements Serializable {

    private static final long serialVersionUID = 1L;

    @Column(name = "key", nullable = false)
    private String requestKey;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "application_id", nullable = false)
    private Application application;
}

@Entity
@Table(name = "t_analysis_type")
public class AnalysisType {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
    @SequenceGenerator(name = "id_sequence", sequenceName = "ANALYSIS_TYPE_ID_SEQ")
    private Integer id;

    @Column(name = "description", nullable = false, unique = true)
    private String description;
}

@Entity
@Table(name = "t_application")
public class Application {

    @Id
    @Column
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "id_sequence")
    @SequenceGenerator(name = "id_sequence", sequenceName = "APP_ID_SEQ", initialValue = 1)
    private int id;

    @Column(name = "name", nullable = false, unique = true)
    private String name;

    @Column(name = "app_key", nullable = false, unique = true)
    private String appKey;


}

I tried different ways depending upon the suggestions. I mentioned the exceptions as well.

public List retrieveReportRequest(Application application, String requestKey) {

    List<ReportRequest> reportRequestList = null;
    Criteria criteriaQuery = null;

    try {
        /*
         * First Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.setFetchMode("primaryKey.application", FetchMode.JOIN); // not
                                                                                // working
        criteriaQuery.setFetchMode("analysisType", FetchMode.JOIN);
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("primaryKey.application.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        // could not resolve property: primaryKey.application.appKey of:
        // com.tcs.textualanalysis.bean.hibernate.report.ReportRequest
        e.printStackTrace();
    }

    try {
        /*
         * Second Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createAlias("primaryKey.application", "app"); // not
                                                                    // working
        criteriaQuery.createAlias("analysisType", "at");
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("app.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * AnalysisType join working but not the application joining select this_.application_id
         * as applicat2_8_1_, this_.key as key1_8_1_, this_.analysis_type as analysis3_8_1_,
         * at2_.id as id1_0_0_, at2_.description as descript2_0_0_, at2_.frequency as
         * frequenc3_0_0_, at2_.sentiment as sentimen4_0_0_ from t_report_request this_ inner
         * join t_analysis_type at2_ on this_.analysis_type=at2_.id where this_.key=? and
         * app1_.app_key=? 2015-03-25 00:50:57 WARN SqlExceptionHelper:144 - SQL Error: 0,
         * SQLState: 42P01 2015-03-25 00:50:57 ERROR SqlExceptionHelper:146 - ERROR: missing
         * FROM-clause entry for table "app1_" Position: 349
         */
        e.printStackTrace();
    }

    try {
        /*
         * Third Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createCriteria("primaryKey.application", "app"); // not
                                                                        // working
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("app.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * Hibernate: select this_.application_id as applicat2_8_0_, this_.key as key1_8_0_,
         * this_.analysis_type as analysis3_8_0_ from t_report_request this_ where this_.key=?
         * and app1_.app_key=? 2015-03-25 00:52:53 WARN SqlExceptionHelper:144 - SQL Error: 0,
         * SQLState: 25P02 2015-03-25 00:52:53 ERROR SqlExceptionHelper:146 - ERROR: current
         * transaction is aborted, commands ignored until end of transaction block
         */
        e.printStackTrace();
    }

    try {
        /*
         * Fourth Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createCriteria("primaryKey", "app"); // not working
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("app.application.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * Criteria objects cannot be created directly on components. Create a criteria on
         * owning entity and use a dotted property to access component property: primaryKey
         */
        e.printStackTrace();
    }

    try {
        /*
         * Fifth Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createCriteria("primaryKey.application", JoinType.FULL_JOIN); // not
                                                                                    // working
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("primaryKey.application.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * org.hibernate.QueryException: could not resolve property:
         * primaryKey.application.appKey of:
         * com.tcs.textualanalysis.bean.hibernate.report.ReportRequest
         */
        e.printStackTrace();
    }

    try {
        /*
         * Sixth Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createCriteria("primaryKey", JoinType.FULL_JOIN); // not working
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("primaryKey.application.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * org.hibernate.QueryException: Criteria objects cannot be created directly on
         * components. Create a criteria on owning entity and use a dotted property to access
         * component property: primaryKey
         */

        e.printStackTrace();
    }

    try {
        /*
         * Seventh Try
         */
        criteriaQuery = sessionFactory.getCurrentSession().createCriteria(ReportRequest.class);
        criteriaQuery.createAlias("primaryKey", "app"); // not
                                                        // working
        criteriaQuery.createAlias("analysisType", "at");
        criteriaQuery.add(Restrictions.eq("primaryKey.requestKey", requestKey));
        criteriaQuery.add(Restrictions.eq("app.application.appKey", application.getAppKey()));
        reportRequestList = criteriaQuery.list();
    } catch(Exception e) {
        /*
         * org.hibernate.QueryException: Criteria objects cannot be created directly on
         * components. Create a criteria on owning entity and use a dotted property to access
         * component property: primaryKey
         */
        e.printStackTrace();
    }
    return reportRequestList;
}
JamesENL
  • 6,400
  • 6
  • 39
  • 64
  • You're not adding all the aliases needed to fulfill the join. You were close enough on the second and seventh try. Also, you shouldn't have an entity inside your embeddable id, it's against the spec, and you'll have lots of troubles while trying to persist anything related to it. – ra2085 Mar 25 '15 at 04:44
  • Embeddable ids should be annotated as @EmbeddedId. – ra2085 Mar 25 '15 at 04:47
  • @ra2085 - using '@EmbeddedId' is also not working. Can you please suggest what need to add in second and seventh try ? If I dont mention entity inside embeddable then how can I mention the mapping in this case? As this entity is a part of the composite primary key. – Dibyarup Bera Mar 25 '15 at 08:12

0 Answers0