JPA导航单向关系

时间:2015-01-28 15:13:19

标签: java entity-framework hibernate java-ee jpa

我有单向关系。这里我有Employee和Andress实体。在Employee实体中,我有以下代码:

@OneToOne(cascade=CascadeType.ALL)
@JoinColumn(name = "HOME_ADDRESS")
private Address homeAddress;

当我只有Adress实体时,找到Employee实体的正确方法是什么。我可以只通过jpql吗?

1 个答案:

答案 0 :(得分:2)

如果您拥有地址的ID

,则可以这样做
select e from Employee e where e.homeAddress.id = :addressId

使用Hibernate条件API,它看起来像这样

Criteria criteria = session.createCriteria(Employee.class);
criteria.createAlias("homeAddress", "homeAddress");
criteria.add(Restrictions.eq("homeAddress.id", addressId);
Employee employee = (Employee)criteria.uniqueResult();