如何使用 spring jpa 将三个实体合并为一个 table?
How to join three entities in one table using spring jpa?
我正在尝试使用 spring-jpa 将三个实体 (table) 加入一个 table 使用多对多关系。
三个类是:
1] 用户
2] 资源
3] 特权
我想把这三个实体合二为一User_Resource_Privilegetable
用户实体
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 .......
}
资源实体
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 ......
}
特权实体
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 ....
}
现在我想通过加入上述所有三个实体来创建一个 table。
ER图中的连接:
有人可以帮助我使用多对多关系加入这三个 table 并让我知道如何使用 spring-jpa 和 REST 来实现吗?
如果您能解释一下如何使用 REST/curl 命令在此 "User_Resource_Privilege" table 中插入数据,那将会很棒?
你可以做的是制作一个可嵌入的 ID 并将其用 class 包裹起来。之后您甚至可以扩展此包装器 class 以容纳其他字段。
java geeks example of embedded id
你会得到类似
的东西
@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
}
在此示例中,您不应只使用字符串,而应使用完整的对象并让休眠(或类似的东西)来完成这些工作。它应该只将 id 带入数据库并发挥它本身的魔力。
编辑:
只想插入 id 作为值,但保持关系看起来像这样
@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;
}
}
我正在尝试使用 spring-jpa 将三个实体 (table) 加入一个 table 使用多对多关系。
三个类是:
1] 用户
2] 资源
3] 特权
我想把这三个实体合二为一User_Resource_Privilegetable
用户实体
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 .......
}
资源实体
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 ......
}
特权实体
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 ....
}
现在我想通过加入上述所有三个实体来创建一个 table。
ER图中的连接:
有人可以帮助我使用多对多关系加入这三个 table 并让我知道如何使用 spring-jpa 和 REST 来实现吗? 如果您能解释一下如何使用 REST/curl 命令在此 "User_Resource_Privilege" table 中插入数据,那将会很棒?
你可以做的是制作一个可嵌入的 ID 并将其用 class 包裹起来。之后您甚至可以扩展此包装器 class 以容纳其他字段。
java geeks example of embedded id
你会得到类似
的东西@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
}
在此示例中,您不应只使用字符串,而应使用完整的对象并让休眠(或类似的东西)来完成这些工作。它应该只将 id 带入数据库并发挥它本身的魔力。
编辑: 只想插入 id 作为值,但保持关系看起来像这样
@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;
}
}