Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA ManyToMany unidirectional relationship

Lets say we have two Entities, Entity Node and Entity Cluster. A Cluster has many Nodes. A Node can belong to multiple Cluster. So in Cluster there is a @ManyToMany annotation. However Node is unaware of any Cluster it belongs to (intentional).

When I delete a Cluster I want to keep all the Nodes (in future I may add those Nodes to another Cluster) it had. So I don't need Cascade.DELETE. But If I delete a Node, all the Cluster it belonged to should be updated (the Node will be removed from them).

What is the best way to handle this using JPA ?

like image 722
dumb_terminal Avatar asked May 12 '16 11:05

dumb_terminal


People also ask

Can many-to-many relationship be unidirectional?

A Course can have many Students and a Student can take many courses. This creates a Many to Many relationship. The following example shows a unidirectional Many to Many mapping in Hibernate. @Entity @Table(name = "Student") class Student { @Id @Column(name="stu_id") @GeneratedValue(strategy=GenerationType.

What is unidirectional and bidirectional in JPA?

A bidirectional relationship has both an owning side and an inverse side. A unidirectional relationship has only an owning side. The owning side of a relationship determines how the Persistence runtime makes updates to the relationship in the database.

What is unidirectional mapping in JPA?

One-To-One mapping is an association between one persistence object and another one related persistence object. If one persistence object uses other and in back if other is not using the first persistence object then it becomes unidirectional.

Is OneToMany bidirectional?

Bidirectional @OneToMany Relationship – Employer/EmployeeWhen you traverse from the “Many” side to the “One” side, you only need to make reference to one object, which is why the Employee class holds a single reference to an Employer class via the private Employer employer instance variable.


2 Answers

I am way too late to this thread, but I had similar issues with Spring Boot 2.4. I had 2 ManyToMany unidirectional relationships from the parent entity to the same child entity.

I would end up getting 1 or 2 less entity relationships saved in the join table.

Then I realized that I had missed out on the most basic thing for an entity/pojo - overriding the hashCode and equals method!

Once I added those, Hibernate started to behave as expected.

like image 181
sunitkatkar Avatar answered Oct 03 '22 05:10

sunitkatkar


In fact in a unidirectional relationship, you can specify the holder entity of the relationship using joinColumn="clusterId" property in the @ManyToMany annotation in the `Cluster class.

This is how should be your code:

@Entity
@Table(name="Cluster")
public class Cluster {

    //Id and properties

    @ManyToMany(cascade = {CascadeType.ALL})
    @JoinTable(name="Cluster_Nodes", 
                joinColumns={@JoinColumn(name="clusterId")}, 
                inverseJoinColumns={@JoinColumn(name="nodeId")})
    private Set<Node> nodes = new HashSet<Node>();

    // Getter and Setter methods
}

For further reading please take a look at :

Hibernate @ManyToMany Unidirectional and Bidirectional

like image 24
cнŝdk Avatar answered Oct 03 '22 05:10

cнŝdk