简体   繁体   English

对孩子进行Hibernate 1到M限制

[英]Hibernate 1 to M restriction on children

I have 1 to M association with Country and Person. 我与国家和人有1到M的关联。 Meaning a Country can have multiple persons. 意思是一个国家可以有多个人。 The country.hbm.xml fils is shown below: 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>

The Person.hbm.xml is shown below 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>

Now, I am trying to query all the persons who are males belonging to India Country 现在,我正在试图询问所有属于印度国家的男性

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

I am getting a Exception in thread "main" org.hibernate.QueryException: could not resolve property: persons.gender of: com.test.hibernate.Country at org.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) at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1960) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:523) at org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:538) at org.hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:66) at org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419) at org.hibernate.loader.criteria.Crite 我在线程“main”中得到一个异常org.hibernate.QueryException:无法解析属性:person.gender:com.test.hibernate.Country at 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)at 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)at org。位于org.hibernate.loader.criteria.Crite的org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:419)中的hibernate.criterion.SimpleExpression.toSqlString(SimpleExpression.java:66) riaJoinWalker.(CriteriaJoinWalker.java:123) at org.hibernate.loader.criteria.CriteriaJoinWalker.(CriteriaJoinWalker.java:92) at 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) riaJoinWalker。(CriteriaJoinWalker.java:123)位于org.hibernate的org.hibernate.loader.criteria.CriteriaJoinWalker。(CriteriaJoinWalker.java:92)org.hibernate.loader.criteria.CriteriaLoader。(CriteriaLoader.java:95)。 internal.essionImpl.list(SessionImpl.java:1643)atg.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)at com.test.hibernate.Main.main(Main.java:54)

Please help. 请帮忙。 I am new to Hibernate. 我是Hibernate的新手。

Thanks in advance. 提前致谢。

Country.persons is of type Collection<Person> . Country.persons的类型为Collection<Person> A Collection doesn't have any property named "gender". Collection没有任何名为“gender”的属性。

If you used HQL instead of Criteria (and you should, for such a simple static query), you would have to do a join: 如果您使用HQL而不是Criteria(对于这样一个简单的静态查询,您应该使用HQL),则必须进行连接:

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

You thus have to do the same with the Criteria query: 因此,您必须对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();

I used M to 1 and used Criteria on Person object as opposed to Country Object. 我使用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();

This way the personList had all the Persons who were males and belonged to India. 通过这种方式,personList拥有所有男性和属于印度的人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM