gpt4 book ai didi

java - Hibernate标准查询: trouble getting Set of a Domain object

转载 作者:行者123 更新时间:2023-12-01 10:19:58 25 4
gpt4 key购买 nike

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

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

我有一个实体Harbor,如下所示。

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

关于java - Hibernate标准查询: trouble getting Set of a Domain object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35660171/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com