我与国家和人有1到M的关联。意思是一个国家可以有多个人。 country.hbm.xml文件如下所示:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.hibernate">
<class name="Country">
<id name="countryId" column="CountryID" > </id>
<property name="countryName" column="CountryName" length="50"></property>
<set name="persons" table="Person" fetch="select" inverse="true">
<key>
<column name="CountryId" not-null="true"></column>
</key>
<one-to-many class="com.test.hibernate.Person"/>
</set>
</class>
</hibernate-mapping>
Person.hbm.xml如下所示
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.test.hibernate">
<class name="Person">
<id name="personID" column="PersonID" > </id>
<property name="name" column="Name" length="50"></property>
<property name="age" column="Age"></property>
<property name="gender" column="Gender" length="1"></property>
<property name="email" column="Email" length="50"></property>
<property name="countryID" column="CountryID" insert="false" update="false"></property>
<many-to-one name="Country" class="com.test.hibernate.Country" fetch="select">
<column name="CountryID" not-null="true"></column>
</many-to-one>
</class>
</hibernate-mapping>
现在,我正在尝试查询所有属于印度国家的男性
Criteria countryCriteria = session.createCriteria(Country.class);
Criterion country = Restrictions.eq("countryName", "India");
Criterion male = Restrictions.eq("persons.gender", "M");
countryCriteria.add(country);
countryCriteria.add(male);
List<Country> countryList = countryCriteria.list();
我得到了 线程&#34; main&#34;中的例外情况org.hibernate.QueryException:无法解析属性:persons.gender:com.test.hibernate.Country 在org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) 在org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:98) 在org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:61) 在org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1960) 在org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:523) 在org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:538) 在org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:66) 在org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419) 在org.hibernate.loader.criteria.CriteriaJoinWalker。(CriteriaJoinWalker.java:123) 在org.hibernate.loader.criteria.CriteriaJoinWalker。(CriteriaJoinWalker.java:92) 在org.hibernate.loader.criteria.CriteriaLoader。(CriteriaLoader.java:95) 在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1643) 在org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374) 在com.test.hibernate.Main.main(Main.java:54)
请帮忙。我是Hibernate的新手。
提前致谢。
答案 0 :(得分:2)
Country.persons
的类型为Collection<Person>
。 Collection没有任何名为“gender”的属性。
如果您使用HQL而不是Criteria(对于这样一个简单的静态查询,您应该使用HQL),则必须进行连接:
select c from Country c
join country.persons person
where c.countryName = 'India'
and person.gender = 'M'
因此,您必须对Criteria查询执行相同的操作:
Criteria countryCriteria = session.createCriteria(Country.class, "c");
countryCriteria.createALias("c.persons", "person");
countryCriteria.add(Restrictions.eq("c.countryName", "India"));
countryCriteria.add(Restrictions.eq("person.gender", "M"));
List<Country> countryList = countryCriteria.list();
答案 1 :(得分:0)
我使用M来1并使用Criteria on Person对象而不是Country Object。
Criteria personCriteria = session.createCriteria(Person.class,"p");
personCriteria.createAlias("p.Country", "c");
Criterion gender = Restrictions.eq("gender", "M");
Criterion country = Restrictions.eq("c.countryName", "India");
personCriteria.add(gender);
personCriteria.add(country);
List<Person> personList = personCriteria.list();
这样,personList拥有所有男性并属于印度的人。