Ben*_*Ben 6 java database postgresql hibernate hql
我希望字段集本身不是唯一的,但是与其他字段组合是唯一的,我得到了这个表有两列(复合主键); id(主键)和object_proxy_id(主键),这正是我所需要的,但是hibernate将object_proxy_id设置为自身唯一,因此值不能在表中重复,我需要此列接受重复值.因为每个用户都有自己的对象代理,这些代理不一定是唯一的.
这就是我想要实现的目标:
|-------------------------------|
| tbl_object_proxy |
| ------------------------------|
| Id (pk)| object_proxy_id (pk) |
|-------------------------------|
| 1 | 150 -- |
| 1 | 149 |= must be able to be DUPLICATE which is not the case right now.
| 2 | 150 -- |
| 2 | 151 |
|-------------------------------|
Run Code Online (Sandbox Code Playgroud)
当前代码:
@Entity
@Table(name = "tbl_user_settings", uniqueConstraints = {@UniqueConstraint(columnNames={"user_id"})})
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Settings implements Serializable
{
@Id
@SequenceGenerator(name="someSequence", sequenceName="SEQ_SOMENAME", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="someSequence")
@Column(name="id")
private int setting_id;
@OneToOne
private User user;
@ManyToOne
private SomeObject someobject;
@ElementCollection
@CollectionTable(name="tbl_collection_name", joinColumns=
@JoinColumn(name="id"), uniqueConstraints = {@UniqueConstraint(columnNames={"id", "object_proxy_id"})})
@Column(name="SomeObject")
private Set<SomeObject> objectProxy;
/*...constructors and methods...*/
}
Run Code Online (Sandbox Code Playgroud)
结果是:
-- Table schema
|-------------------|
| tbl_user_settings |
|-------------------|
| id |PK <<Unique>>
| user_id |FK reference tbl_user <<Unique>>
| object_id |FK reference tbl_object
|-------------------|
|------------------|
| tbl_object_proxy |
|------------------|
| id |PK reference tbl_user_settings
| object_proxy_id |PK reference tbl_object <<Unique>> BUT I DON'T WANT THIS TO BE UNIQUE ON ITSELF !!!!
|------------------|
Run Code Online (Sandbox Code Playgroud)
编辑:
tbl_object_proxy中的两个主键是复合主键
我尝试过Xeon的解决方案,但它没有用.
简短的回答:将 the 替换@ElementCollection为这样的@ManyToMany关系@JoinTable:
@ManyToMany
@JoinTable(
name="tbl_settings_objecteproxy_v2",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id"))
private Set<SomeObject> objectproxy;
Run Code Online (Sandbox Code Playgroud)
请参阅Hibernate 注解文档中的“2.2.5.3.2.1.定义”
这会产生相同的边表,但没有唯一约束。所以现在这是可能的:
|-------------------------------|
| tbl_object_proxy |
| ------------------------------|
| Id (pk)| object_proxy_id (pk) |
|-------------------------------|
| 1 | 150 -- |
| 1 | 149 |= It works! The unique constraint is gone!
| 2 | 150 -- |
| 2 | 151 |
|-------------------------------|
Run Code Online (Sandbox Code Playgroud)
详细的答案和原因描述:
不知何故,@ElementCollection创建了一个具有引用键(集合|反向连接)的一对多关系的集合表,它向引用另一侧表的键添加了唯一约束,以反映我没有的一对多关系不想。所以我删除了@ElementCollection并将其替换为@ManyToMany带有@JoinTable注释的关系。我还尝试@ManyToMany在 中声明关系,@ElementCollection但它不断向引用的键添加唯一约束。
我的设置类现在看起来像这样:
@Entity
@Table(name = "tbl_user_settings", uniqueConstraints = {@UniqueConstraint(columnNames={"user_id"})})
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Settings
{
@Id
@SequenceGenerator(name="someSequence", sequenceName="SEQ_SOMENAME", allocationSize =1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="someSequence")
@Column(name="id")
private int setting_id;
@OneToOne
private User user;
@ManyToOne
private SomeObject someobject;
@ManyToMany
@JoinTable(
name="tbl_settings_objecteproxy_v2",
joinColumns = @JoinColumn(name = "id"),
inverseJoinColumns = @JoinColumn( name = "objectproxy_id"))
private Set<SomeObject> objectProxy;
/*...the rest...*/
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
339 次 |
| 最近记录: |