Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/321.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 JPA@JoinTable-三个ID列_Java_Hibernate_Unique Constraint_Multiple Columns_Jointable - Fatal编程技术网

Java JPA@JoinTable-三个ID列

Java JPA@JoinTable-三个ID列,java,hibernate,unique-constraint,multiple-columns,jointable,Java,Hibernate,Unique Constraint,Multiple Columns,Jointable,我的情况与这个问题图表中的情况非常相似,尽管有不同的问题 我有三个表:函数,组,和位置。目前,我使用@JoinTable在位置和组之间设置了一个联接表。它的两面都是@manytomy,工作非常好 我试图添加一个约束,即没有位置应与多个具有相同功能的组相关联。因此,我在我的SQL模式中将Function列添加到我的联接表中,并在Location和Function列之间添加了唯一性约束,如下所示: create table function_table ( id varchar(50), p

我的情况与这个问题图表中的情况非常相似,尽管有不同的问题

我有三个表:
函数
,和
位置
。目前,我使用
@JoinTable
位置
之间设置了一个联接表。它的两面都是
@manytomy
,工作非常好

我试图添加一个约束,即没有
位置
应与多个具有相同
功能的
相关联。因此,我在我的SQL模式中将
Function
列添加到我的联接表中,并在
Location
Function
列之间添加了唯一性约束,如下所示:

create table function_table (
  id varchar(50),
  primary key(id)
);

create table group_table (
  id varchar(50),
  function_id varchar(50) not null,
  primary key(id)
);

alter table group_table add constraint FK_TO_FUNCTION foreign key (function_id) references function_table;

create table location_table (
  id varchar(50),
  primary key(id)
);

create table group_location_join (
  location_id varchar(50) not null,
  group_id varchar(50) not null,
  function_id varchar(50) not null,
  primary key(location_id, group_id, function_id),
  unique(location_id, function_id)
);

alter table group_location_join add constraint FK_TO_LOCATION foreign key (location_id) references location_table;
alter table group_location_join add constraint FK_TO_GROUP foreign key (group_id) references group_table;
alter table group_location_join add constraint FK_TO_FUNCTION foreign key (function_id) references function_table;
然后,我尝试在模型实体中设置以下内容:

@Entity
@Table(name = "function_table")
public class Function {
  @Id
  @Column(name = "id", length = 50)
  private String id;
}

@Entity
@Table(name = "group_table")
public class Group {
  @Id
  @Column(name = "id", length = 50)
  private String id;

  @ManyToOne
  @JoinColumn(name = "function_id", referencedColumnName = "id", nullable = false)
  private Function function;

  @ManyToMany
  @JoinTable(name = "group_location_join",
    joinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id"),
                   @JoinColumn(name = "function_id", referencedColumnName = "function_id")},
    inverseJoinColumns = @JoinColumn(name="location_id", referencedColumnName = "id"))
  private Set<Location> locations;
}

@Entity
@Table(name = "location_table")
public class Location {
  @Id
  @Column(name = "id", length = 50)
  private String id;

  @ManyToMany
  @JoinTable(name = "group_location_join",
    joinColumns = @JoinColumn(name="location_id", referencedColumnName = "id")
    inverseJoinColumns = {@JoinColumn(name = "group_id", referencedColumnName = "id"),
                   @JoinColumn(name = "function_id", referencedColumnName = "function_id")})
  private Set<Group> groups;
}
我认为现在发生的事情是Hibernate变得困惑,举起手说“我将序列化它,将它发送到数据库,并希望它知道发生了什么。”

当我添加
implements Serializable
并将
serialVersionUID
添加到
时,我得到以下结果:

org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: FUNCTION_ID
我真的不确定在这一点上如何继续,或者也许我已经走错了太远的路。也许我没有正确地考虑SQL,有一种更简单的方法来确保这个约束不包含所有这些荒谬的内容

编辑:在我的系统中,相关表的DAO没有保存功能。这意味着只要在数据库中设置了约束,我的应用程序就不在乎;它无法插入违反约束的内容,因为它根本无法插入内容


edit2:我最初从未解决过上述问题,而是在数据库模式中添加了第三列,而没有涉及Java代码,正如我在上面的第一节Edit中所述。但后来我尝试了用
@Embedded
复合键创建一个显式联接表对象,它似乎可以工作。

您正在尝试创建一个复合主键。在Hibernate中,您可以使用
@embeddeble
注释来完成此操作。在下面的示例中,您可以找到为两个实体使用复合键的方法

我相信您可以继续使用这个示例并创建自己的主键版本


您是指
组中的复合键吗?因为我根本不想那样。当系统中的任何其他内容通过主键引用
时,它应该只处理
id
列。如果我给
Group
一个复合主键,这将改变这个事实;所有内容都将通过
id
function\u id
查找
Group
s。我不要这个。我只想将
function\u id
放入
group\u location\u join
表中。
org.hibernate.exception.SQLGrammarException: user lacks privilege or object not found: FUNCTION_ID