我可以编写标准查询加入视图和表

时间:2013-08-03 06:27:02

标签: hibernate hibernate-criteria

我有两张桌子 1)应用程序(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)

,使用hibernate条件查询。是否有可能。如果可能,请回答示例。

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_survey 表有一对多的关系

1 个答案:

答案 0 :(得分:-1)

当然,您可以查询或从视图中检索 - 只需将其映射到Hibernate中,就像您在任何表格中一样。您可以为此映射创建一个类,或使用现有类将其映射为“命名实体”。

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

你不会(通常)能够更新 - 所以可以在映射中设置mutable='false'

BTW,格式&amp;缩进代码并正确映射文件。如果您无法有效地阅读它们,您希望如何正确地处理它们或发现错误?