Hibernate Criteria查询:获取域对象的设置时遇到问题

时间:2016-02-26 19:33:38

标签: java spring hibernate

我有一个模仿下面列结构的表。

  

ID,Harbor_Name,Country,Country_Code,State,State_Code,City

我有一个实体港口如下。

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>

2 个答案:

答案 0 :(得分:0)

尝试修改这样的标准:

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());
}

答案 1 :(得分:0)

这对我有用。

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());
}