2014-04-22 81 views
0

我有1到M協會與國家和人。意味着一個國家可以有多個人。該country.hbm.xml費爾如下所示:休眠1到M兒童限制

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

我越來越線程 「main」 org.hibernate.QueryException一個 異常:無法解析屬性:persons.gender的:com.test.hibernate.Countryorg.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83) at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:98) at 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.CriteriaQueryTransla tor.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) at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1643) at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java :374) at com.test.hibernate.Main.main(Main.java:54)

請幫忙。我是Hibernate的新手。

在此先感謝。

回答

0

我用M來表示1,並在Person對象上使用Criteria而不是Country對象。

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擁有所有的男性和屬於印度的人。

2

Country.persons的類型是Collection<Person>。集合沒有任何名爲「性別」的屬性。

如果使用HQL,而不是標準(你應該,對於這樣一個簡單的靜態查詢),你就必須做一個連接:

select c from Country c 
join country.persons person 
where c.countryName = 'India' 
and person.gender = 'M' 

你這樣都與條件查詢相同:

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(); 
+0

如果我將我的代碼更改爲您的建議,它將countryList中的3個元素返回給我,每個元素都包含屬於兩個性別(M和F)的印度的所有3個人。該查詢似乎只適用於國家級別,而不適用於具有HQL的人員級別 – user3010197

+0

,請使用'select distinct c ...'。使用Criteria,使用'countryCriteria。setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY)' –

+0

我向CountryCriteria變量添加了不同的根實體。現在,我只看到1個根元素。但根元素中的孩子仍然沒有過濾記錄。意味着兩性(M和F)仍然存在。查詢規範僅適用於根級別。 – user3010197