我有一个SQL查询,可以提供准确的结果。尝试使用休眠条件构建相同的查询,但在创建休眠条件时无法在某些时候执行此操作。以下是我的实体类(在这里我仅在实体类中添加了必填字段)
@Table(name="m_address")
public class MAddress {
private Long id;
.
.
.
// setter/getter
}
--------------------------------------------------------------------------
@Table(name = "j_address")
public class JAddress {
private Long id;
private MAddress address;
private JHeader JHeader;
.
.
}
---------------------------------------------------------------------------
@Table(name = "j_header")
public class JHeader {
private Long id;
private List<Job> jobs;
.
.
}
---------------------------------------------------------------------------
@Table(name = "job")
public class Job {
private Long id;
private JHeader JHeader;
.
.
.
}
---------------------------------------------------------------------------
@Table(name = "project_p_address")
public class ProjectPAddress {
private Long id;
private Job job;
private PMetric PMetric;
private MAddress address;
.
.
}
---------------------------------------------------------------------------
@Table(name = "const_metric")
public class ConstMetric {
private Long id;
private Job job;
}
---------------------------------------------------------------------------
@Table(name = "P_METRIC")
@PrimaryKeyJoinColumn(name="const_metric_ID")
public class PMetric extends ConstMetric {
private ProjectPStatus status;
}
----------------------------------------------------------------------------
@Entity
@Table(name = "lookup")
public class ProjectPStatus implements UserType, ParameterizedType {
public static final ProjectPStatus CODE_ACTIVATED = new ProjectPStatus(new Long(422), "ACTV", "Activated");
private Long id;
private String code;
private String description;
}
在db上运行良好的查询是
select ja.* from j_address ja
JOIN j_header jh on jh.id = ja.j_header_ID
JOIN job j on j.j_header_ID = jh.ID
join project_p_address ppa on ppa.JOB_ID = j.ID and ppa.ADDRESS_ID = ja.ADDRESS_ID
join const_metric cm on cm.JOB_ID = j.ID and cm.id = ppa.const_metric_ID
join P_METRIC pm on pm.const_metric_ID = ppa.const_metric_ID and pm.STATUS_ID = 589
where jh.id = 111111;
以下是我要在休眠状态下构建的条件
Criteria crit1 = session.createCriteria(JAddress.class); // Main enityt which need to be there in select
Criteria crit2 = crit1.createCriteria("JHeader"); // Joining JHeader
Criteria crit3 = crit2.createCriteria("jobs", "j"); // Joining Job with JonHeader
Criteria crit4 = crit3.createCriteria("job", "j"); // Joining ProjectPAddress with job
在这里,ProjectPAddress也正在使用JAddress的地址
我们如何建立这类条件。
请有人帮忙。谢谢