Sumant Sumant - 1 month ago 23
Java Question

How to join three entities in one table using spring jpa?

I am trying to join three entities (table) using spring-jpa into one table using Many-To-Many relationship.

Three classes are :

1] User

2] Resource

3] Privilege

And I want to combine these three entities into one User_Resource_Privilege table

User Entity



package com.****.acl.domain;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToMany;

import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;

@Entity
public class User {

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name="user_id", nullable=false, length=40)
private String userId;

@Column(name="user_name", nullable=false, length=45)
private String userName;

@Column(name="first_name", nullable=true, length=45)
private String firstName;

@Column(name="last_name", nullable=true, length=45)
private String lastName;

@Column(name="email", nullable=true, length=50)
private String email;

public User(){

}

public User(String userName, String firstName, String lastName, String email) {
this.userName = userName;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
}


getter and setters .......
}


Resource Entity



import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@Entity
public class Resource {

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name="resource_id", nullable=false, length=40)
private String resourceId;

@Column(name="resource_name", nullable=false, length=45)
private String name;

@Column(name="resource_type", nullable=false, length=45)
private String type;

public Resource(){

}

public Resource(String name, String type) {
this.name = name;
this.type = type;
}

getter and setter ......
}


Privilege Entity



import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.*;
import org.hibernate.annotations.GenericGenerator;

@Entity
public class Privilege {

@Id @GeneratedValue(generator="system-uuid")
@GenericGenerator(name="system-uuid", strategy = "uuid")
@Column(name="privilege_id", nullable=false, length=40)
private String privilegeId;

@Column(name="resource_name", nullable=false, length=45)
private String name;

@Column(name="resource_description", nullable=true, length=45)
private String description;

public Privilege(){

}

getters and setters ....
}


Now I want to create one table by joining all the three entities described above.

The join in ER diagram:

enter image description here

Can someone please help me in joining these three tables using Many-To-Many relationship and let me know how to achieve this using spring-jpa and REST ?
Also it will be great if you please explain how to insert data in this "User_Resource_Privilege" table using REST/curl command ?

Answer

What you could do is make an embeddable ID and wrap it with the class. You can afterwards even expand this wrapper class to hold other fields.

java geeks example of embedded id

You would get something like

@Embeddable
public class EmbeddedIdClass implements Serializable {
  private String userId;
  private String resourceId;
  private String privilegeId;

// constructors, getters and setters, equals, etc
}

@Entity
public class Wrapper {
  @EmbeddedId
  private EmbeddedIdClass id;

// constructors, etc
}

Instead of just using the strings in this example, you should use the complete objects and let hibernate (or something like it) do it's stuff. It should only take the id's into the database and do it's magic itself.

edit: Just wanting to insert the id's as values, but keeping relationships would look something like this

@Entity
public class Wrapper {
  @Id
  private String id;
  private User user;
  private Resource resource;
  private Privilege privilege;


// constructors
  public Wrapper(final User user, final Resource resource, final Privilege privilege) {
    this.user = user;
    this.resource = resource;
    this.privilege = privilege;
  }
}