6

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 中的两个主键是复合主键
我尝试过至强的解决方案,但它没有用。

4

2 回答 2

2

简短的回答:@ElementCollection用这样的@ManyToMany关系替换@JoinTable

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

请参阅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                  |
|-------------------------------|


详细答案和原因描述: 不知何故,@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...*/
}
于 2012-05-22T09:24:41.467 回答
0

我对文章和类别有类似的问题:

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;
}

并且你需要设置嵌入式PK类的两列的唯一性

于 2012-05-18T16:01:40.893 回答