Hibernate UnUniquify Column in a Table


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; /*...constructors and methods...*/ } 

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.

+6
source share
2 answers

Short answer: replace @ElementCollection with @ManyToMany relation with @JoinTable as follows:

 @ManyToMany @JoinTable( name="tbl_settings_objecteproxy_v2", joinColumns = @JoinColumn(name = "id"), inverseJoinColumns = @JoinColumn( name = "objectproxy_id")) private Set<SomeObject> objectproxy; 

See "2.2.5.3.2.1. Definition" in the Hibernation Annotation Documentation

The result is the same table, but without a unique constraint. So now it is possible:

 |-------------------------------| | tbl_object_proxy | | ------------------------------| | Id (pk)| object_proxy_id (pk) | |-------------------------------| | 1 | 150 -- | | 1 | 149 |= It works! The unique constraint is gone! | 2 | 150 -- | | 2 | 151 | |-------------------------------| 


A detailed description of the answer and the reasons: Somehow @ElementCollection created a collection with a one-to-many reference key relationship (collection | inverse join) that adds a unique constraint to the key that references the other parties table to reflect one for many relationships that I did not want. So I reset @ElementCollection and replaced it with @ManyToMany relation with @JoinTable annotation. I also tried to declare @ManyToMany relation in @ElementCollection , but he continued to add a Unique constraint to the reference key.

My settings class now looks like this:

 @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...*/ } 
+2
source

I had a similar problem with articles and categories:

 public class ArticleCategoriesEntity { @EmbeddedId public ArticleCategoriesIdPk getArticleCategoriesIdPk() { return articleCategoriesIdPk; } public void setArticleCategoriesIdPk(ArticleCategoriesIdPk articleCategoriesIdPk) { this.articleCategoriesIdPk = articleCategoriesIdPk; } ... @Embeddable public class ArticleCategoriesIdPk implements Serializable { public ArticleCategoriesIdPk() { } public ArticleCategoriesIdPk(Integer articleCategoryIdPk, Integer articleCategoryVersionFk) { this.articleCategoryIdPk = articleCategoryIdPk; this.articleCategoryVersionFk = articleCategoryVersionFk; } private Integer articleCategoryIdPk; @Column(name = "article_category_id_pk") public Integer getArticleCategoryIdPk() { return articleCategoryIdPk; } public void setArticleCategoryIdPk(Integer articleCategoryIdPk) { this.articleCategoryIdPk = articleCategoryIdPk; } private Integer articleCategoryVersionFk; @Column(name = "article_cat_version_fk") public Integer getArticleCategoryVersionFk() { return articleCategoryVersionFk; } public void setArticleCategoryVersionFk(Integer articleCategoryVersionFk) { this.articleCategoryVersionFk = articleCategoryVersionFk; } 

And you need to set the uniqueness of the two columns of the PK built-in class

0
source

Source: https://habr.com/ru/post/916080/


All Articles