3

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

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
} 

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:

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?

brabec
  • 4,632
  • 8
  • 37
  • 63

1 Answers1

1

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

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

or

select tf from StateTransitionEvent e join e.transitionFrom tf 
where e.foo = :foo and tf.bar = :bar
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255