Using this excellent information,
http://www.hibernate.org/261.html
I have created a UserType implementation for one of my Oracle types. I can now use Hibernate to read data from the database. My Oracle type field is defined in my hbm file as,
Code:
<property name="materialLink" type="com.apldbio.lims.database.mapping.MaterialLinkUserType">
<column name="MATERIAL_LINK" sql-type="NAIUT_MATERIAL"/>
</property>
Now I am having trouble extending my Hibernate Criteria queries to use this newly available-to-Hibernate type,
I have tried two approaches (both with Groovy under Grails) without success. One of the three fields within my Oracle type is 'id' and in the following two hibernate examples I am trying to search by 'id'. I've added a third Example to show how raw SQL works. Any help with getting my Criteria searches to work wold be great.
Example 1:
=======
Code:
groovy> import org.hibernate.criterion.Restrictions
groovy> ctx
groovy> def sessionFactory = ctx.getBean("sessionFactory")
groovy> def session = sessionFactory.openSession()
groovy> List list = session.createCriteria(Sample.class).add(Expression.eq("condition", "APPROVED")).add(Restrictions.eq("materialLink.id", 1)).list()
groovy> print list.size()
Exception thrown: org.hibernate.QueryException: could not resolve property: materialLink.id of: Sample
org.hibernate.QueryException: could not resolve property: materialLink.id of: Sample
at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:44)
at org.hibernate.persister.entity.AbstractPropertyMapping.toColumns(AbstractPropertyMapping.java:59)
at org.hibernate.persister.entity.BasicEntityPropertyMapping.toColumns(BasicEntityPropertyMapping.java:31)
at org.hibernate.persister.entity.AbstractEntityPersister.toColumns(AbstractEntityPersister.java:1354)
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:434)
Example 2:
=======
Code:
groovy> import org.hibernate.criterion.Expression
groovy> ctx
groovy> def sessionFactory = ctx.getBean("sessionFactory")
groovy> def session = sessionFactory.openSession()
groovy> List list = session.createCriteria(Sample.class).add(Expression.eq("condition", "APPROVED")).createCriteria("materialLink").add(Expression.eq("id", 1)).list()
Exception thrown: org.hibernate.QueryException: not an association: materialLink
org.hibernate.QueryException: not an association: materialLink
at org.hibernate.loader.criteria.CriteriaQueryTranslator.getPathEntityName(CriteriaQueryTranslator.java:216)
Example 3:
=======
Raw SQL that works,
Code:
select s.material_link from samples s where s.material_link.id = 810