P
P
Pavel Dudenkov2018-06-26 14:21:55
Java
Pavel Dudenkov, 2018-06-26 14:21:55

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
}

Buy and SellOrder should not know about each other's connection via MatchedOrders.
Entries in MatchedOrders appear much later after the creation of Sell and BuyOrder.
As a result, I would like the matched_orders table to remain at the base schema level as the MatchedOrders class is described above, and see MatchedOrders.class like this:
@Entity
public class MatchedOrders {
@Id
private String id;
private SetbuyOrders;
private Set sellOrders;
//... other fields
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2018-06-26
@leahch

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 question

Ask a Question

731 491 924 answers to any question