I have 1 to M association with Country and Person. Meaning a Country can have multiple persons. The country.hbm.xml fils is shown below:
<?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
<?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 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)
Please help. I am new to Hibernate.
Thanks in advance.
Country.persons
is of type Collection<Person>
. A Collection doesn't have any property named "gender".
If you used HQL instead of Criteria (and you should, for such a simple static query), you would have to do a join:
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 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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.