Hibernate UnUniqueify 表中的列(已解决)
我希望一个字段本身不唯一,但与另一个字段结合起来是唯一的,我得到这个表有两列(复合主键);id (主键)和 object_proxy_id (主键),这正是我需要的,但休眠将 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 |
|-------------------------------|
当前代码:
@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...*/
}
结果是:
-- 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 !!!!
|------------------|
编辑:
tbl_object_proxy 中的两个主键是复合主键
我尝试过至强的解决方案,但它没有用。