英文:
Hibernate @JoinColumn reusing column name to map to another entity
问题
我有一个包含字符串的列,表示另一个实体的名称(因此是主键),我正在尝试将列连接起来以将实体存储在父实体中。实体看起来如下:
@Entity
public class Thing {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Id
@Column(name = "thingname")
private String name;
}
@Entity
public class Consumer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "sourcething")
private String source;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sourcething", referencedColumnName = "thingname")
private Thing thing;
}
这将引发映射错误,表示存在重复的列映射,因此根据这个问题我添加了额外的参数:
@JoinColumn(name = "sourcething", referencedColumnName = "thingname", insertable = false, updatable = false)
然而,在单元测试期间出现了ConstraintViolationExceptions
错误。
我是否漏掉了某些内容以完成这个映射/列连接?
英文:
I have a column that contains a string, representing the name (and thereby primary key) of another entity, to which I am attempting to join the columns to store the entity in the parent. The entities look like so:
@Entity
public class Thing {
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Id
@Column(name = "thingname")
private String name;
}
@Entity
public class Consumer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "sourcething")
private String source;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sourcething", referencedColumnName = "thingname")
private Thing thing;
}
This will throw a mapping error, denoting duplicate column mappings, so from this question I added the additional arguments:
@JoinColumn(name = "sourcething", referencedColumnName = "thingname", insertable = false, updatable = false)
However, this beings to fail with ConstraintViolationExceptions
during unit testing.
Is there something I am missing for completing this mapping/column join?
答案1
得分: 0
我不知道你的确切用例,但是表格定义对我来说看起来有些奇怪。首先,标识符(@ID)应该是一个数字值,或者至少是一个UUID,而不是一个任意的字符串。为什么不将数字ID存储在你提到的指向那个字符串的表格中呢?类似这样:
@Entity
public class Thing {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
@Column(name = "thingname")
private String name;
}
@Entity
public class Consumer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sourcething_id")
private Thing thing;
}
通过这样,你也可以通过Consumer.thing.name
来访问字符串。
顺便说一下,约束违规可能是因为你将sourcething定义为@JoinColumn(name = "sourcething", referencedColumnName = "thingname", insertable = false, updatable = false)
,但同时你使用@Column(name = "sourcething") private String source;
列插入了一个值。
英文:
I do not know your exact use case but the table defintions look strange to me. First of all an identifier (@ID) should be a numeric value or at least an UUID but not an arbitrary string. Why do not store the numeric ID in the table you mentioned pointing to that string? Something like this:
@Entity
public class Thing {
@GeneratedValue(strategy = GenerationType.AUTO)
@Id
private Long id;
@Column(name = "thingname")
private String name;
}
@Entity
public class Consumer {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "sourcething_id")
private Thing thing;
}
By this you can also access the string bby Consumer.thing.name
BTW: the constraint violation presumably occurrs as you defined the sourcething as @JoinColumn(name = "sourcething", referencedColumnName = "thingname", insertable = false, updatable = false)
but at the same time you insert a value using @Column(name = "sourcething") private String source;
column.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论