我有一个域对象,其中包含 id(自动生成)、person_id、airport_id、姓名、职位、状态。 person_id 和 airport_id 组合可以唯一标识一行。
有没有办法填写这两个属性并通过hibernate发送域对象,以便hibernate自动填充其他字段。实现此目的的另一种方法是通过命名查询,但这是我的最后一个选择。
请帮忙。
最佳答案
如果您使用 HibernateTemplate ,您可以利用该方法 findByExample(Object) .
其中,参数传递给findByExample
方法将是 criteria object
- 因此,就您而言,entity
与 personId
和airportId
放。 findByExample(entity)
的结果,是 List
包含所有已填充其他数据的匹配实体。
如果您使用普通 hibernate ,则可以使用 Criteria 来实现此目的.
Criteria c = session.createCriteria(YourEntity.class);
c.add(Restrictions.eq("personId", p_id_value));
c.add(Restrictions.eq("airportId", a_id_value));
c.list(); //This should again return a list containing all the matching entities with the values filled
关于java - 使用域对象来获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16028042/