I am trying to learn hibernate and I came across this little problem en route. Any help will be appreciated.
I have created a entity and a value typed object.
My entity is of type Person which is mapped to value object of Address.
The mapping is given below:
Code:
1. <?xml version="1.0"?>
2. <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
3. "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
4. <hibernate-mapping>
5. <class name="entityobject.Person" table="Person" schema="dev_mds">
6. <composite-id name="keyId" class="entityobject.key.PersonKey">
7. <key-property name="ssnNumber" column="ssn_number" type="string" />
8. <key-property name="personId" column="person_id" type="string" />
9. </composite-id>
10. <property name="name" column="person_name" type="string" />
11. <property name="age" column="person_age" type="float" />
12. <set name="address" table="address" schema="dev_mds">
13. <key>
14. <column name="ssn_number" />
15. <column name="person_id" />
16. </key>
17. <composite-element class="valueobject.Address">
18. <parent name="user" />
19. <property name="address" type="string" column="address" />
20. </composite-element>
21.
22. </set>
23.
24. </class>
25. </hibernate-mapping>
My question is can i query from a value typed object? When i query directly from the entity i get the details of the address value object too.
However what if a use case arises where i need to directly query from value object how would i do that.
I tried the below query
Code:
# <query name="getFromAddress"><![CDATA[from valueobject.Address as a where
# a.address =:address
# ]]></query>
However on executing the query from code using the below code:
Code:
# Query q = hibernateSession.getNamedQuery("getFromAddress");
# q.setString("address", "myaddress2");
# List<Address> result = q.list();
I get an exception which says:
Exception in thread "main" java.lang.IllegalArgumentException: Parameter address does not exist as a
named parameter in [from valueobject.Address as a where
a.address =:address
]
at org.hibernate.impl.AbstractQueryImpl.setParameter(AbstractQueryImpl.java:356)
at org.hibernate.impl.AbstractQueryImpl.setString(AbstractQueryImpl.java:643)
at hibernatetrial.run.RunApplication.main(RunApplication.java:43)
Please help.