2013-08-03 77 views
1

我有兩個表 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。

所以我創建教職員

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) 

,使用Hibernate的標準query.Is它possible.If可能請回復例子。

爲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_surve的映射文件Ÿ表如下

<?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_survey

回答

-1

當然,你可以查詢或從視圖檢索 - 只是它映射在Hibernate中,像任何表一樣。您可以爲此映射創建一個類,或者使用現有的類將其映射爲「命名實體」。

根據需要,您可以使用Session.createCriteria(Class)Session.createCriteria(String entityName) API。

雖然您不會(一般)能夠更新,因此可以在映射中設置mutable='false'

順便說一句,格式&正確縮進你的代碼和映射文件。如果你無法有效地閱讀它們,你期望如何正確地工作或發現錯誤?

+0

你能給這個例子 – Pramil

+0

當然,如果你不格式化你的代碼。任何人如何輕鬆閱讀你提供的內容,給你一個正確的例子? –

相關問題