【问题标题】:How to join three entities in one table using spring jpa?如何使用spring jpa在一张表中加入三个实体?
【发布时间】:2015-07-13 18:25:07
【问题描述】:

我正在尝试使用 spring-jpa 将三个实体(表)连接到一个使用多对多关系的表中。

三个类是:

1] 用户

2] 资源

3] 特权

我想将这三个实体合并到一个 User_Resource_Privilege 表中

用户实体

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 ....
}

现在我想通过连接上述所有三个实体来创建一个表。

ER图中的连接:

有人可以帮助我使用多对多关系加入这三个表,并让我知道如何使用 spring-jpa 和 REST 来实现这一点吗? 如果您能解释一下如何使用 REST/curl 命令在这个“User_Resource_Privilege”表中插入数据,那也太好了?

【问题讨论】:

    标签: java spring hibernate spring-boot spring-data-jpa


    【解决方案1】:

    您可以做的是制作一个可嵌入的 ID 并将其与类一起包装。之后你甚至可以扩展这个包装类来保存其他字段。

    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
    }
    

    在这个例子中,你应该使用完整的对象,而不是只使用字符串,并让 hibernate(或类似的东西)来完成它的工作。它应该只将 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;
      }
    }
    

    【讨论】:

    • 感谢您的回复。这会在表之间创建任何关系吗?还是只是创建一个主键?因为我想在 mySql 数据库中的这个连接表中插入值
    • 如果您使用对象版本而不是字符串版本,您将获得插入值的主键。如果您只想插入值,请生成单独的 PK 并将对象本身用作字段。
    • 感谢您的回答。我会尝试这个解决方案,并会尽快回复您。
    • 您好,我可以创建表格并且工作正常。只是在插入部分苦苦挣扎。您将如何在连接表中插入数据?如果我有 userId、resourceId 和 privilegeId 如何在 UserResourcePrivilege 表中插入行?请帮忙。
    • 当您使用 JPA 时,您只需创建一个新的 UserResourcePriviledge 对象并将其持久化。你已经用类创建了这个对象,然后你用你想耦合在一起的对象来构造你的对象
    猜你喜欢
    • 2022-01-05
    • 1970-01-01
    • 2015-08-13
    • 2010-12-12
    • 2019-04-18
    • 1970-01-01
    • 2020-08-04
    • 2019-02-05
    • 1970-01-01
    相关资源
    最近更新 更多