gpt4 book ai didi

java - Hibernate 取消唯一化表中的列

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:30:05 25 4
gpt4 key购买 nike


Hibernate UnUniqueify a column in table(已解决)


我希望一个字段集本身是非唯一的,但与另一个字段组合是唯一的,我得到了这个有两列(复合主键)的表; 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 的解决方案,但没有用。

最佳答案

简答:@ManyToMany 关系替换 @ElementCollection@JoinTable 如下:

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

参见 Hibernate Annotation Documentation 中的“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 以某种方式创建了一个集合表,该集合表具有引用键的一对多关系(集合 | 反向连接),它添加了一个唯一约束引用另一边表的键来反射(reflect)我不想要的一对多关系。所以我删除了 @ElementCollection 并将其替换为带有 @JoinTable 注释的 @ManyToMany 关系。我还尝试在 @ElementCollection 中声明 @ManyToMany 关系,但它一直向引用的键添加 Unique 约束。

我的设置类现在看起来像这样:

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

关于java - Hibernate 取消唯一化表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10650536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com