1

enter image description here

My Database Relation

I create 3 Java file to map like this.

User.java

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id" , unique = true)
private int id;

@Column(name = "username")
private String username; 

@Column(name = "name")
private String name;

@Column(name = "last_name")
private String lastName;

@Column(name = "password")
@Transient
private String password;

@Column(name = "email")
private String email;

@ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name =  "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
private Role role = new Role(); 


public String getUsername() {
    return username;
}

public void setUsername(String username) {
    this.username = username;
}

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getPassword() {
    return password;
}

public void setPassword(String password) {
    this.password = password;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public Role getRole() {
    return role;
}

public void setRole(Role role) {
    this.role = role;
}

User_Role.java

@Entity
@Table(name = "user_role")
@IdClass(User_Role.class)
public class User_Role implements Serializable {


private static final long serialVersionUID = -5488274192125813525L;

@Id
@Column(name="user_id")
private int user_id;

@Id
@Column(name="role_id")
private int role_id;

@ManyToOne(fetch = FetchType.LAZY ,cascade = CascadeType.ALL)
@JoinColumn(name="user_id" , referencedColumnName = "user_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkuroleuser"))
private User user = new User();

@ManyToOne(fetch = FetchType.LAZY ,cascade = CascadeType.ALL)
@JoinColumn(name="role_id" , referencedColumnName = "role_id",insertable=false, updatable=false , foreignKey = @ForeignKey(name = "fkurolerole"))
private Role role = new Role() ;

public int getUser_id() {
    return user_id;
}

public void setUser_id(int user_id) {
    this.user_id = user_id;
}

public int getRole_id() {
    return role_id;
}

public void setRole_id(int role_id) {
    this.role_id = role_id;
}

public User getUser() {
    return user;
}

public void setUser(User user) {
    this.user = user;
}

public Role getRole() {
    return role;
}

public void setRole(Role role2) {
    this.role = role2;
}

Role.java

@Entity
@Table(name = "role")
public class Role {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name="role_id" , nullable = true)
private int role_id;

@Column(name="role")
private String role;

public int getId() {
    return role_id;
}
public void setId(int role_id) {
    this.role_id = role_id;
}
public String getRole() {
    return role;
}
public void setRole(String role) {
    this.role = role;
}

I'm trying to make add,delete and update with JPA Repository but it's seem like i still wrong with mapping entity. But i can query with jpa criteria with this mapping. And i can't change private Role role = new Role(); private User user = new User(); in my project.

I want to know how to map entity in correct way and please give me some reference.

Edit Now my User.java like

@Entity
@Table(name = "user")
public class User {

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "user_id" , unique = true)
private int id;

@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "user_role", joinColumns = @JoinColumn(name =   "user_id"),inverseJoinColumns = @JoinColumn(name = "role_id"))
private Set<Role> roleList = new HashSet<>();

// getter setter

public User(int user_id,int role_id , String role){

this.id = user_id
this.roleList.add(new Role()); //still not work because can't add data role_id and role
}
I3eaver
  • 19
  • 1
  • 6

2 Answers2

4

Your User and Role relationship is ManyToMany. Where your user_role table is just for mapping their many to many associations. Hence you don't need to create an extra Entity User_Role. You can just use a ManyToMany relationship in User entity and use @JoinTable for the association mapping

Change this inUser.java

    @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    @JoinTable(name = "user_role", 
              joinColumns = @JoinColumn(name =  "user_id"),
              inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<Role> roleList = new ArrayList<>();

Now if you want to make this relationship bidirectional, you can just map it in Role entity too

    @ManyToMany(mappedBy = "roleList")
    private List<user> userList = new ArrayList<>();

You will find lots and lots example and blogs about this common scenario.

Further reading

Shafin Mahmud
  • 3,831
  • 1
  • 23
  • 35
  • Thank you,That work for CRUD , but i have some problem about JPA Criteria query.How to make constructor for recieve data role_id(int) and role(String) from List when i use `private Role role = new Role();` it can use `this.role.setId(role_id);` for get value. – I3eaver Oct 25 '18 at 02:51
  • can you elaborate what you are actually want to to do with your Criteria query? There are many ways @I3eaver – Shafin Mahmud Oct 25 '18 at 06:55
  • I use AJAX to post some data for query then I receive query with constructor like `public User(int user_id,int role_id,String role)` and put data to response with `this.id = user_id;` my question is how to do this with role_id , role ? because it have form `Set` – I3eaver Oct 25 '18 at 07:41
  • I mean what you want to find? like "I want to check whether a user having a certain role" ? – Shafin Mahmud Oct 25 '18 at 08:36
  • Check my edit.The last line is only 1 problem.My query data work fine i can check with print them.But can't pass data form to ajax response. – I3eaver Oct 25 '18 at 08:52
  • Are you sending back the User object directly in response? Its always better to have separate your response object than Entity. Why dont you just create another pojo class like `UserPojo` for response. and construct it whatever way you like from `user` object ? – Shafin Mahmud Oct 25 '18 at 09:55
  • You mean something like DTO right? or other idea? Please leave some reference.Thank you – I3eaver Oct 26 '18 at 03:58
  • Or if you think this User - Role relationship is ManyToOne in your case, You dont even need the `user_role` join table either. You could just have `role_id` reference on `user` table. And on `User` entity you have then `private Role role;` which you will simply annotate `ManyToOne`. – Shafin Mahmud Oct 26 '18 at 12:08
0

In user_role:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "role_id")
@JsonIgnore
private Role role ;

your Role class can be coded like this :

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "role ")
private List<Role > role = new ArrayList<>();