Answer the question
In order to leave comments, you need to log in
Hibernate - how to make such a connection?
My design has a one-to-many mirror relationship and they don't intersect.
Those. there is Entity1, Entity2 , each has its own id and other properties. And there is an operational table that stores entity1_id, entity2_id and some other properties calculated by business logic.
In this case, entity1_id can correspond to many entity2_id , and vice versa, but for example, if entity1_id is already in a one-to-many relationship, then it will not fall into many-to-one, from the point of view of business logic, entity1->entity2 match one-to-one.
How to express it with Hibernate? All my attempts ended up with hibernate either creating a cross FK on the Entity1 and Entity2 tables, or creating an entity1_id in the Entity2 table and vice versa.
The important point is that the connection between these Entities is quite dynamic, i.e. the relationship is not created during the creation of these entities, but as a result of the business logic. In addition, this relationship in the life cycle of entities may disappear in the case of some business rules.
@Entity
public class BuyOrder {
@Id
private String id;
//... other fields
}
@Entity
public class SellOrder {
@Id
private String id;
//... other fields
}
@Entity
public class MatchedOrders {
@Id
private String id;
private String buyOrderId;
private String sellOrderId;
//... other fields
}
Answer the question
In order to leave comments, you need to log in
As I understand it, you need a composite key. Here you can read how in several versions https://vladmihalcea.com/the-best-way-to-map-a-com...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question