Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在Spring中将其他列添加到joinTable?_Java_Spring_Hibernate_Join - Fatal编程技术网

Java 如何在Spring中将其他列添加到joinTable?

Java 如何在Spring中将其他列添加到joinTable?,java,spring,hibernate,join,Java,Spring,Hibernate,Join,我目前有一个用户实体用于我的用户,还有一个权限实体用于我的用户“角色”。如果我的应用程序只要求用户拥有特定的权限来完成任务,那么这样做效果很好,但事实并非如此。在我继续之前,以下是这些实体的代码: 用户: 现在,如果我拉一个用户,我会得到如下结果: { username: "john", email: "john@doe.com", authorities: [ { name: "ADMIN" } ] }

我目前有一个
用户
实体用于我的用户,还有一个
权限
实体用于我的用户“角色”。如果我的应用程序只要求用户拥有特定的
权限
来完成任务,那么这样做效果很好,但事实并非如此。在我继续之前,以下是这些实体的代码:

用户:

现在,如果我拉一个用户,我会得到如下结果:

{
    username: "john",
    email: "john@doe.com",
    authorities: [
        {
            name: "ADMIN"
        }
    ]
}
{
    username: "john",
    email: "john@doe.com",
    authorities: [
        {
            name: "ADMIN",
            companyId: 3
        },
        {
            name: "USER",
            locationId: 12
        },
        {
            name: "USER"
        }
    ]
}
现在,我正在将
公司
位置
实体添加到组合中。我希望一个用户能够拥有,比如说,一个
公司的
管理员
角色
,一个特定
位置的
用户
角色
,以及一个正常的
用户
角色。因此,对于上述用户,我希望如下所示:

{
    username: "john",
    email: "john@doe.com",
    authorities: [
        {
            name: "ADMIN"
        }
    ]
}
{
    username: "john",
    email: "john@doe.com",
    authorities: [
        {
            name: "ADMIN",
            companyId: 3
        },
        {
            name: "USER",
            locationId: 12
        },
        {
            name: "USER"
        }
    ]
}

我想在我的
joinTable
company\u id
location\u id
中添加2个
NULLABLE
列,但我似乎不能向
user\u authorities
表添加多个联接,因为它说该表已经与另一个实体联接。有没有一种简单的方法可以将其添加到我当前的设置中,还是我的做法不对?对此问题的任何启示都将不胜感激。谢谢大家!

@manytomy
关联替换为两个双向
@OneToMany
关联,因此为用户权限表创建
@Entity

您可以创建其他实体
用户权限
并创建两个双向
@OneToMany
关联。。 例:

@Data
@Entity
@Table(name = "user_authorities")
public class UserAuthority {

   @JsonIgnore
   @Id
   @Column(name = "id")
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "user_authorities_seq")
   @SequenceGenerator(name = "user_authorities_seq", sequenceName = "user_authorities_seq", allocationSize = 1)
   private Long id;

   @ManyToOne
   @JoinColumn(name="user_id", nullable=false)
   private User user;

   @ManyToOne
   @JoinColumn(name="authority_id", nullable=false)
   private Authority authority;

   @NotNull
   private String otherField;

   @NotNull
   private String otherField2;

   //...

   public UserAuthority() {}

    // getters and setters