Hibernate取消表中列的唯一性约束(已解决)
我想要一个字段本身不唯一,但与其他字段的组合在表中是唯一的。我已经有了一个包含两个列(复合主键)的表:id(主键)和object_proxy_id(主键),这正是我需要的,但Hibernate会将object_proxy_id设置为唯一的,因此该值不能在表中重复出现,而我需要该列接受重复值。因为每个用户都有自己的对象代理,而这些代理不一定是唯一的。 这是我想要实现的:
我想要一个字段本身不唯一,但与其他字段的组合在表中是唯一的。我已经有了一个包含两个列(复合主键)的表: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 |
|-------------------------------|
当前代码:
@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中的两个主键是复合主键
我尝试过Xeon的解决方案但它没有起作用。