我有两个表 1)application(application_id(pk),application_type,application_date,......,......,...... ...) 2)application_survey(application_survey_id,application_id(fk),survey_no,resurvey_no,...... ……………………)
这里的application_survey 表中包含多个与application_id 对应的条目。
所以我创建了 veiw
CREATE OR REPLACE VIEW v_application_survey AS
SELECT application_survey.application_id, string_agg(application_survey.survey_no::text,
CASE
WHEN btrim(application_survey.survey_no::text) = ''::text OR application_survey.survey_no IS NULL THEN ''::text
ELSE ','::text
END) AS survey_no, string_agg(application_survey.resurvey_no::text,
CASE
WHEN btrim(application_survey.resurvey_no::text) = ''::text OR application_survey.resurvey_no IS NULL THEN ''::text
ELSE ','::text
END) AS resurvey_no
FROM application_survey
GROUP BY application_survey.application_id;
ALTER TABLE v_application_survey OWNER TO postgres;
然后我需要查询
select* from application
left join v_application_survey
on(v_application_survey.application_id=application.application_id)
,使用休眠条件查询。是否可能。如果可能,请以示例回复。
v_application_survey对应的pojo类如下
public class VApplicationSurvey implements java.io.Serializable {
private VApplicationSurveyId id;
public VApplicationSurvey() {
}
public VApplicationSurvey(VApplicationSurveyId id) {
this.id = id;
}
public VApplicationSurveyId getId() {
return this.id;
}
public void setId(VApplicationSurveyId id) {
this.id = id;
}
}
v_application_survey 对应的映射文件如下
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Aug 1, 2013 10:36:46 AM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="nic.mams.model.VApplicationSurvey" table="v_application_survey" schema="public">
<composite-id name="id" class="nic.mams.model.VApplicationSurveyId">
<key-property name="applicationId" type="java.lang.Long">
<column name="application_id" />
</key-property>
<key-property name="surveyNo" type="string">
<column name="survey_no" />
</key-property>
<key-property name="resurveyNo" type="string">
<column name="resurvey_no" />
</key-property>
</composite-id>
</class>
</hibernate-mapping>
application_survey 表的模型类如下
@SuppressWarnings("serial")
public class ApplicationSurvey implements java.io.Serializable {
private long applicationSurveyId;
private Application application;
private Village village;
private Direction direction;
private Employee employee;
private String surveyNo;
private String resurveyNo;
private Date updatedOn;
private char surveyType;
private String resurveyBlock;
private Double area;
public ApplicationSurvey() {
}
public ApplicationSurvey(long applicationSurveyId, char surveyType) {
this.applicationSurveyId = applicationSurveyId;
this.surveyType = surveyType;
}
public ApplicationSurvey(long applicationSurveyId, Application application,
Village village, Direction direction, Employee employee,
String surveyNo, String resurveyNo, Date updatedOn,
char surveyType, String resurveyBlock, Double area) {
this.applicationSurveyId = applicationSurveyId;
this.application = application;
this.village = village;
this.direction = direction;
this.employee = employee;
this.surveyNo = surveyNo;
this.resurveyNo = resurveyNo;
this.updatedOn = updatedOn;
this.surveyType = surveyType;
this.resurveyBlock = resurveyBlock;
this.area = area;
}
public long getApplicationSurveyId() {
return this.applicationSurveyId;
}
public void setApplicationSurveyId(long applicationSurveyId) {
this.applicationSurveyId = applicationSurveyId;
}
public Application getApplication() {
return this.application;
}
public void setApplication(Application application) {
this.application = application;
}
public Village getVillage() {
return this.village;
}
public void setVillage(Village village) {
this.village = village;
}
public Direction getDirection() {
return this.direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Employee getEmployee() {
return this.employee;
}
public void setEmployee(Employee employee) {
this.employee = employee;
}
public String getSurveyNo() {
return this.surveyNo;
}
public void setSurveyNo(String surveyNo) {
this.surveyNo = surveyNo;
}
public String getResurveyNo() {
return this.resurveyNo;
}
public void setResurveyNo(String resurveyNo) {
this.resurveyNo = resurveyNo;
}
public Date getUpdatedOn() {
return this.updatedOn;
}
public void setUpdatedOn(Date updatedOn) {
this.updatedOn = updatedOn;
}
public char getSurveyType() {
return this.surveyType;
}
public void setSurveyType(char surveyType) {
this.surveyType = surveyType;
}
public String getResurveyBlock() {
return this.resurveyBlock;
}
public void setResurveyBlock(String resurveyBlock) {
this.resurveyBlock = resurveyBlock;
}
public Double getArea() {
return this.area;
}
public void setArea(Double area) {
this.area = area;
}
}
application_survey 表的映射文件如下
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 14 Nov, 2011 5:11:45 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="nic.mams.model.ApplicationSurvey" table="application_survey" schema="public">
<id name="applicationSurveyId" type="long">
<column name="application_survey_id" />
<generator class="increment"/>
</id>
<many-to-one name="application" class="nic.mams.model.Application" fetch="select">
<column name="application_id" />
</many-to-one>
<many-to-one name="village" class="nic.mams.model.Village" fetch="select">
<column name="village_id" length="6" />
</many-to-one>
<many-to-one name="direction" class="nic.mams.model.Direction" fetch="select">
<column name="direction" />
</many-to-one>
<many-to-one name="employee" class="nic.mams.model.Employee" fetch="select">
<column name="updated_by" />
</many-to-one>
<property name="surveyNo" type="string">
<column name="survey_no" length="10" />
</property>
<property name="resurveyNo" type="string">
<column name="resurvey_no" length="10" />
</property>
<property name="updatedOn" type="timestamp">
<column name="updated_on" length="29" />
</property>
<property name="surveyType" type="char">
<column name="survey_type" length="1" not-null="true" />
</property>
<property name="resurveyBlock" type="string">
<column name="resurvey_block" length="10" />
</property>
<property name="area" type="java.lang.Double">
<column name="area" precision="17" scale="17" />
</property>
</class>
</hibernate-mapping>
请注意,application表与application_survey表具有一对多的关系