简体   繁体   English

JPA导航单向关系

[英]JPA navigate unidirectional relationship

I have an unidirectional relationship. 我有单向关系。 Here i have Employee and Andress entities. 在这里,我有Employee和Andress实体。 In Employee entity i have the following code: 在Employee实体中,我有以下代码:

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

What is the correct way to find the Employee entity when i have only the Adress entity. 当我只有Adress实体时,找到Employee实体的正确方法是什么? Can I only do that via jpql? 我只能通过jpql这样做吗?

You could do it like this if you have, for example, the id of the address 例如,如果您有地址的ID,则可以这样操作

select e from Employee e where e.homeAddress.id = :addressId

Using Hibernate criteria API, it would look like this 使用Hibernate条件API,它看起来像这样

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM