How to map many-to-many table relationship with entity without breaking unidirectional many-to-many relationship join table structure?
You know that join table is necessary to manage the many-to-many relationship. many-to-many relationship can be unidirectional and bidirectional.
After creating entities unidirectional relationship, sometimes middle entity can be necessary to map own entity.
According to above mentioned image, our entities should be pre and post as follows.
_pre
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROLE_ID", unique = true, nullable = false)
private Long id;
@Column(name = "name")
private String name;
//getter and setter
}
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID", unique = true, nullable = false)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "email", unique = true, nullable = false)
private String email;
@ManyToMany
@JoinTable(name = "tbl_user_role",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
@LazyCollection(LazyCollectionOption.TRUE)
private Set<Role> roles = new HashSet<Role>();
//getter and setter
}
_post
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROLE_ID", unique = true, nullable = false)
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @LazyCollection(LazyCollectionOption.TRUE)
//getter and setter
}
@Embeddable
public class UserRoleId implements Serializable {
After creating entities unidirectional relationship, sometimes middle entity can be necessary to map own entity.
According to above mentioned image, our entities should be pre and post as follows.
_pre
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROLE_ID", unique = true, nullable = false)
private Long id;
@Column(name = "name")
private String name;
//getter and setter
}
@Entity
@Table(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "USER_ID", unique = true, nullable = false)
private Long id;
@Column(name = "username", nullable = false)
private String username;
@Column(name = "email", unique = true, nullable = false)
private String email;
@ManyToMany
@JoinTable(name = "tbl_user_role",
joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"),
inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")
)
@LazyCollection(LazyCollectionOption.TRUE)
private Set<Role> roles = new HashSet<Role>();
//getter and setter
}
_post
@Entity
@Table(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ROLE_ID", unique = true, nullable = false)
private Long id;
@Column(name = "name")
private String name;
@OneToMany(mappedBy = "role", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY) @LazyCollection(LazyCollectionOption.TRUE)
private Set<UserRole> userRoles = new HashSet<>();
//getter and setter
}
@Entity@Table(name = "user_role")public class UserRole implements Serializable{ @EmbeddedId
private UserRoleId userRoleId; @ManyToOne
@MapsId("userId") //references EmbeddedId's property @JoinColumn(name = "user_id", referencedColumnName = "id", nullable = false)
private User user = new User(); @ManyToOne @MapsId("roleId") //references EmbeddedId's property @JoinColumn(name = "role_id", referencedColumnName = "id", nullable = false)
private Role role = new Role();
//getter and setter
}
@Embeddable
public class UserRoleId implements Serializable {
@Column(name = "user_id", nullable = false)
private Long userId;
@Column(name = "role_id", nullable = false)
private Long roleId;
//getter setter
}
}
@Entity @Table(name = "user") public class User implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "USER_ID", unique = true, nullable = false) private Long id; @Column(name = "username", nullable = false) private String username;@Column(name = "email", unique = true, nullable = false) private String email;@OneToMany(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true)@LazyCollection(LazyCollectionOption.TRUE) private Set<UserRole> userRoles = new HashSet<>();//getter and setter}
Comments
Post a Comment