Retrieve emebedded or component using Hibernate Criteria api

一笑奈何 提交于 2019-12-10 13:09:38

问题


I have this class mapped as a entity, lets call it Person. Person has an embedded/component relation to Address. I am having trouble using a Criteria that would return Address objects. I have tried this:

Criteria.createCriteria(Address.class)

Which does not work. I guess I need to go through the entity but then I would need some kind of projection?

Criteria.createCriteria(Person.class).<<what goes here???>>

Suggestions?


回答1:


Component's lifetime is controlled by its owner; they are NOT considered associations. You therefore cannot retrieve component by itself from a query. You can, however, use it in criteria.

Assuming your "Address" class is mapped as "address" within "Person", you could do something like:

Criteria.createCriteria(Person.class)
 .add(Restrictions.eq("address.street", street));



回答2:


This is how one can retrieve or refer the property of embedded object in hibernate.

Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
                Parent.class,"parent");
criteria.createAlias("embeddedObjectFieldName", "parent.embeddedObjectFieldName");

criteria.setProjection(Projections.projectionList()
                .add(Projections.groupProperty("parent.propertyOne"))
                .add(Projections.max("embeddedObjectFieldName.embeddedobjectproperty")));

Hope above clarifies



来源:https://stackoverflow.com/questions/1471892/retrieve-emebedded-or-component-using-hibernate-criteria-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!