繁体   English   中英

对孩子进行Hibernate 1到M限制

[英]Hibernate 1 to M restriction on children

我与国家和人有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();

我在线程“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)位于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)

请帮忙。 我是Hibernate的新手。

提前致谢。

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

我使用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拥有所有男性和属于印度的人。

暂无
暂无

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

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