java - How to join three entities in one table using spring jpa? -
i trying join 3 entities (table) using spring-jpa 1 table using many-to-many relationship.
three classes :
1] user
2] resource
3] privilege
and want combine these 3 entities 1 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 , 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 , 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 , setters .... }
now want create 1 table joining 3 entities described above.
the join in er diagram:
can please me in joining these 3 tables using many-to-many relationship , let me know how achieve using spring-jpa , rest ? great if please explain how insert data in "user_resource_privilege" table using rest/curl command ?
what make embeddable id , wrap class. can afterwards expand wrapper class hold other fields.
java geeks example of embedded id
you
@embeddable public class embeddedidclass implements serializable { private string userid; private string resourceid; private string privilegeid; // constructors, getters , setters, equals, etc } @entity public class wrapper { @embeddedid private embeddedidclass id; // constructors, etc }
instead of using strings in example, should use complete objects , let hibernate (or it) it's stuff. should take id's database , it's magic itself.
edit: wanting insert id's values, keeping relationships 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; } }
Comments
Post a Comment