Ternary (and n-ary) relationships in Hibernate

Q 1) How can we model ternary relationships using Hibernate? For example, how can we model the ternary relationships represented here using Hibernate (or JPA)?

NOTE I know that JPA 2 adds some constructs for building ternary relationships using maps. However, this question suggests JPA 1 or Hibernate 3.3.x, and I don't like using maps to model this.

ER Model
(source: grussell.org )


ER Model with ternary relationships replaced
(source: grussell.org )

Ideally, I prefer my model to be like this:

class SaleAssistant { Long id; //... } class Customer { Long id; //... } class Product { Long id; //... } class Sale { SalesAssistant soldBy; Customer buyer; Product product; //... } 

Q 1.1)

How can we model this option in which each sale item can have many products?

 class SaleAssistant { Long id; //... } class Customer { Long id; //... } class Product { Long id; //... } class Sale { SalesAssistant soldBy; Customer buyer; Set<Product> products; //... } 

Q 2) In general, how can we model n-ary, n> = 3 relationships with Hibernate?

Thanks in advance.

+6
java orm hibernate jpa database-design
source share
2 answers

Q1. How can we model triple relationships using Hibernate? For example, how can we model the triple relationships presented here using Hibernate (or JPA)? (...)

I would redo the relationship with an intermediate entity class (and what is recommended with Hibernate). Applies to your example:

 @Entity public class Sale { @Embeddable public static class Pk implements Serializable { @Column(nullable = false, updatable = false) private Long soldById; @Column(nullable = false, updatable = false) private Long buyerId; @Column(nullable = false, updatable = false) private Long productId; public Pk() {} public Pk(Long soldById, Long buyerId, Long productId) { ... } // getters, setters, equals, hashCode } @EmbeddedId private Pk pk; @ManyToOne @JoinColumn(name = "SOLDBYID", insertable = false, updatable = false) private SaleAssistant soldBy; @ManyToOne @JoinColumn(name = "BUYERID", insertable = false, updatable = false) private Customer buyer; @ManyToOne @JoinColumn(name = "PRODUCTID", insertable = false, updatable = false) private Product product; // getters, setters, equals, hashCode } 

B1.1. How can we model this option in which each Sale element can have many products?

I would not use a composite primary key here and enter PK for the Sale object.

Q2. In general, how can we model n-ary, n> = 3 relationships with Hibernate?

I think my answer is Q1. covers it. If not, please specify.


Update: Responding to OP comments

(...) pk fields are not filled, and as a result, I can not save sales objects in the database. Should I use setters like this for the Sale class? public void setBuyer (Client cust) {this.buyer = cust; this.pk.buyerId = cust.getId (); }

You need to create a new Pk (I removed the constructors from my original answer for short) and set it in the Sale element. I would do something like this:

 Sale sale = new Sale(); Pk pk = new Pk(saleAssistant.getId(), customer.getId(), product.getId()); sale.setPk(pk); sale.setSoldBy(saleAssistant); sale.setBuyer(customer); sale.setProduct(product); ... 

And then save the Sale .

Also, in JoinColumn annotations, in which column are the "name" fields indicated? Do pks “target relationships” or sales tables have their own column names?

In the columns for the attributes of a composite Pk (i.e., the sales column proper names tables) we want them to get PK and FK constraints.

+11
source share

Do you use primary keys generated by the database for customers, products, and SalesAssistant? This can cause a problem because it looks like you are trying to use the actual database identifiers, rather than letting Hibernate resolve object references during the actual save.

The nested PK above looks weird to me personally, but I didn't have the opportunity to try it. The columns seem to overlap and collide with each other.

I would think that simply using ManyToOne links is enough.

Also, enable debugging of the SQL query and see what is sent to the database.

0
source share

All Articles