如何使用HQL(Hibernate查询语言)在辅助表中声明复合主键?以前我在我的类中声明了一个连接表,一切正常,它创建了两列的复合主键。这是我使用的代码:
BEFORE(工作)
@Id
@SequenceGenerator(name="someSequence", sequenceName="SEQ_APP", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="appSequence")
@Column(name="id")
private int setting_id;
@OneToOne
private User user;
@ManyToMany
@JoinTable(
name="tbl_settings_objectproxy",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id")
)
private Set<SomeObject> objectproxy;
现在我把所有东西都搞砸了,我把Set改成了List并添加了另一个side table。现在hibernate会创建两个旁边表,因为它应该这样做但它没有声明任何主键...有谁知道如何解决这个问题?这是我的新代码:
AFTER(它不再创建复合主键,甚至不会声明任何主键)
@Id
@SequenceGenerator(name="someSequence", sequenceName="SEQ_APP", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="appSequence")
@Column(name="id")
private int setting_id;
@OneToOne
private User user;
@ManyToMany
@JoinTable(
name="tbl_settings_objectproxy",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id")
)
private List<SomeObject> objectProxyForSomething;
答案 0 :(得分:3)
答案/解决方案:
更改列表&lt;&gt;设置&lt;&gt; 将在这种情况下完成所有魔法。
您可以 使用HQL手动创建表格。
@ManyToMany
@JoinTable(
name="tbl_settings_objectproxy_for_something",
joinColumns = @JoinColumn(name = "id", columnDefinition = "int primary key"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id")
uniqueConstraints = {@UniqueConstraint(columnNames={"id", "objectproxy_id"})}
)
private List<SomeObject> SomeObjectProxy;
对JB Nizet的评论进行投票,因为这些是他的解决方案,但是他给了他们评论而不是答案。