Java Hibernate创建复合密钥而不是使用标识符
我有一个实体FooBar,它充当Foo和Bar实体的@ManyToMany联接表,包括一些附加信息Java Hibernate创建复合密钥而不是使用标识符,java,hibernate,orm,hibernate-mapping,composite-key,Java,Hibernate,Orm,Hibernate Mapping,Composite Key,我有一个实体FooBar,它充当Foo和Bar实体的@ManyToMany联接表,包括一些附加信息 @Entity @Table(name = "foo_bar") public class FooBar { @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", unique = true, nullable = false) protected Long id;
@Entity
@Table(name = "foo_bar")
public class FooBar
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
protected Long id;
@Column(name = "someInfo", nullable = true)
private String someInfo;
@ManyToOne(optional = false)
private Foo foo;
@ManyToOne(optional = false)
private Bar bar;
//getters, setters, and toString()
}
@Entity
@Table(name = "foo")
public class Foo
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
protected Long id;
@OneToMany(mappedBy = "foo", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
private Set<FooBar> fooBars;
//Foo has a number of other fields
@Column(name = "orderIndex", nullable = false)
private int orderIndex;
@Column(name = "upgradeDirection", nullable = false)
@Enumerated(EnumType.STRING)
private Order direction;
@ManyToOne(optional = false)
private SomeEntity e;
//getters, setters, and toString()
}
@Entity
@Table(name = "bar")
public class Bar
{
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "id", unique = true, nullable = false)
protected Long id;
@OneToMany(mappedBy = "bar") //TODO specify a cascade and fetch attribute
private Set<FooBar> fooBars;
//Bar contains a number of other fields
@Column(name = "value", nullable = false)
private String value;
//getters, setters, and toString()
}
当Hibernate创建表时,表包含“id”、“someInfo”、“foo\u id”和“bar\u id”列foo_id'和'bar_id'在中用作复合键,而不是使用'id'字段,知道为什么吗?在foo或bar中是否有@manytomy注释?foo和bar都与foobari有@OneToMany关系是否可能是在对注释进行某些更改之前生成的表?如果删除该表并让Hibernate重新创建它,会得到相同的结果吗?是的,我只删除了foo_bar并删除了整个数据库。当Hibernate创建表时,它使复合键变得非常奇怪。。。在你给我们看的代码中,我看不出是什么原因造成的。也许是因为你没给我们看的东西。