Hibernate UnUniquify column in table (allowed)
I want the field set to be non-unique in itself, but to be unique in combination with another field, I got this table with two columns (composite primary keys); id (primary key) and object_proxy_id (primary key), this is exactly what I need, but hibernate establishes that object_proxy_id is unique for itself so that the value cannot be duplicated in the table, and I need this column to accept duplicate values. Since each user has his own object proxy, and this proxy does not have to be unique.
This is what I want to achieve:
|-------------------------------| | 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 | |-------------------------------|
Current code:
@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; }
Results in:
-- 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 !!!! |------------------|
EDIT: Two primary keys in tbl_object_proxy are composite primary keys
I tried the Xeon solution, but that didn't work.
source share