簡體   English   中英

Hibernate Criteria查詢:難以獲取Domain對象的Set

[英]Hibernate Criteria query : trouble getting Set of a Domain object

我有一個表,模仿下面的列結構。

ID,港口名稱,國家/地區,國家/地區代碼,州/省,州代碼,城市

我有一個實體港口,如下所示。

public class Harbor{
   private Long Id;
   private String name;
   private Address address;
   -- getters and setters ---
}

Public class Address{
  private Country country;
  private State state;
  private String city;
  -- getters and setters ---
}

Public class Country{
  private String name;
  private String code;
  -- getters and setters ---
}

Public class State{
  private String name;
  private String code;
  -- getters and setters ---
}

我想從給定國家名稱的表格中獲取Set。 我嘗試了以下條件對象,並且遇到了以下SQL異常。

    public Set<State> getStatesByCountry(String countryName) {
    Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Harbor.class, "harbor");
        criteria.createAlias("harbor.address", "a");
        criteria.createAlias("a.country", "c");
        criteria.createAlias("a.state", "s");
        criteria.setProjection(Projections.distinct(Projections.property("s.name")));
        criteria.add(Restrictions.eq("c.name", countryName).ignoreCase());
        criteria.add(Restrictions.isNotNull("s.name"));
        states.addAll(criteria.list());
}

SQL生成和異常:

select distinct state3_.Harbor_Name as y0_ from HARBOR_DATA this_ where lower(country2_.Harbor_Name)=? and state3_.Harbor_Name is not null

java.sql.SQLSyntaxErrorException: ORA-00904: "STATE3_"."Harbor_Name": invalid identifier

<class name="sample.package.Harbor" table="Harbor_data" mutable="false">
        <id name="Id" column="ID"></id>
        <property name="name" column="Harbor_Name" />
        <component name="address" class="sample.domain.Address">
            <property name="city" column="City" />
            <component name="state" class="sample.domain.State">
                <property name="code" column="State_Code" />
                <property name="name" column="State" />
            </component>
            <component name="country" class="sample.domain.Country">
                <property name="name" column="Country" />
                <property name="code" column="Country_Code" />
            </component>
        </component>
    </class>

嘗試像這樣修改您的條件:

Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Harbor.class, "harbor");
        criteria.createAlias("harbor.address", "a");
        criteria.createAlias("a.country", "c");
        criteria.createAlias("a.state", "s");

        //note I've changed projection property to harbor name
        criteria.setProjection(Projections.distinct(Projections.property("harbor.name")));
        criteria.add(Restrictions.eq("c.name", countryName).ignoreCase());
        criteria.add(Restrictions.isNotNull("s.name"));
        states.addAll(criteria.list());
}

這對我有用。

public Set<State> getStatesByCountry(String countryName) {
    Criteria criteria = this.sessionFactory.getCurrentSession().createCriteria(Harbor.class, "harbor");
    criteria.setProjection(Projections.distinct(Projections.property("harbor.address.state.name")));
        criteria.add(Restrictions.eq("harbor.address.country.name", countryName).ignoreCase());
        criteria.add(Restrictions.isNotNull("harbor.address.state.name"));
        states.addAll(criteria.list());
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM