Hibernate加入复合主键

时间:2015-03-24 19:37:02

标签: hibernate hibernate-mapping hibernate-criteria

需要有关hibernate join的帮助。

问题 - 我有三个hibernate pojo类。 1. ReportRequest 2. AnalysisType 3.申请

我从hibernate创建了表,并且所有表都已成功创建。所以似乎hibernate映射没有问题。

ReportRequest包含复合主键 - ReportRequestPk。 ReportRequest与Application具有“ManyToOne”关系,它也是复合主键的一部分。 ReportRequest也与AnalysisType有“ManyToOne”关系。

我正在尝试将ReportRequest与Application一起加入,以限制Application的appKey属性。它不起作用。但是,如果我尝试将AnalysisType与ReportRequest连接,并对AnalysisType的任何属性有一些限制,那么它正常工作。如果我将Application保留在ReportRequet中,即不是复合主键的一部分,那么对appKey的限制也是完美的。如果Application是复合主键的一部分,则它不起作用。我试过'createAlias','fetchMode','createCriteria'。请查找pojo类和条件查询的代码段。我不能使用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;


}

我根据建议尝试了不同的方式。我也提到了例外情况。

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

0 个答案:

没有答案