gpt4 book ai didi

java - 如何使用 spring jpa 将三个实体连接到一个表中?

转载 作者:行者123 更新时间:2023-11-30 08:49:30 25 4
gpt4 key购买 nike

我正在尝试使用多对多关系将使用 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 实现这一点吗?另外,如果您能解释一下如何使用 REST/curl 命令在此“User_Resource_Privilege”表中插入数据,那就太好了?

最佳答案

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

关于java - 如何使用 spring jpa 将三个实体连接到一个表中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31390709/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com