JPA/休眠-在单向OneToOne关联上反向连接吗? [英] JPA/Hibernate - inverse join on unidirectional OneToOne association?

查看:242
本文介绍了JPA/休眠-在单向OneToOne关联上反向连接吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此链接显示了如何从中加入单向的ManyToOne.联想的另一面".
这也适用于OneToOne关联吗?

This link shows how a unidirectional ManyToOne can be joined "from the other side" of association.
Does this also work for OneToOne associations?

就我而言,我有StateTransitionEvent实体,该实体与OrderStateEntity具有单向一对一关联.我不想使关系在对象级别上是双向的,OrderState类不需要了解转换.

In my case, I have StateTransitionEvent entity that has a unidirectional one-to-one association with OrderStateEntity. I don't want to make the relationship bidirectional on the object level, the OrderState class doesn't need to know about the transitions.

@Entity
class StateTransitionEvent {
    @OneToOne
    private Order transitionFrom;

    @OneToOne
    private Order transTo;
}

@Entity
class OrderState {
   // no association to StateTransitionEvent
} 

由于Order具有许多单向关系,因此它想从Order实体开始查询,但是我需要使用StateTransitionEvent将Order加入. 该查询将类似于:

Since Order has many unidirectional relationships, it would like to start the query from the Order entity, but I need to join the Order with the StateTransitionEvent. The query would look something like:

从订单中选择e o从tf中加入stateTranstionEvent.transition ...

SELECT e FROM Order o JOIN stateTranstionEvent.transitionFrom tf...

这是可能的,还是必须在对象级别使双向关联?

Is that possible, or do I have to make the association bidirectional on the object level?

推荐答案

您的问题不是很清楚. SELECT e FROM Order o JOIN stateTranstionEvent.transitionFrom tf中的e是什么?

Your question is not very clear. What is e, in SELECT e FROM Order o JOIN stateTranstionEvent.transitionFrom tf?

您不能从Order加入StateTransitionEvent(因为Order和StateTransitionEvent没有关联).但是您可以加入另一个方向,可以选择顺序或事件:

You can't join from Order to StateTransitionEvent (since there is no association from Order to StateTransitionEvent). But you can join in the other direction, and you may select the order or the event:

select e from StateTransitionEvent e join e.transitionFrom tf 
where e.foo = :foo and tf.bar = :bar

select tf from StateTransitionEvent e join e.transitionFrom tf 
where e.foo = :foo and tf.bar = :bar

这篇关于JPA/休眠-在单向OneToOne关联上反向连接吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆