如何使用spring jpa在一个表中连接三个实体?

时间:2015-07-13 18:25:07

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

我正在尝试使用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图中的连接:

enter image description here

有人可以帮助我使用多对多关系加入这三个表,让我知道如何使用spring-jpa和REST实现这一目标吗? 如果你能解释如何在这个" User_Resource_Privilege"中插入数据,那将会很棒。使用REST / curl命令的表?

1 个答案:

答案 0 :(得分:2)

您可以做的是制作一个可嵌入的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
}

不要只使用这个例子中的字符串,而应该使用完整的对象,让休眠(或类似的东西)做它的东西。它应该只将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;
  }
}