[英]Hibernate / JPA optional one-to-one bidirectional mapping
I have two tables, each used to describe information about an object, but coming from two different systems. 我有两个表,每个表用于描述有关对象的信息,但来自两个不同的系统。 As such a given object could exist in one, the other, or both. 因为这样的给定对象可以存在于一个,另一个或两者中。
service_A
---------------
service_A_pkey UUID NOT NULL PRIMARY KEY,
common_identifier TEXT NOT NULL,
<more columns to store data>
and 和
service_B
---------------
service_B_pkey UUID NOT NULL PRIMARY KEY,
common_identifier TEXT NOT NULL,
<more columns to store data>
Ideally, I'd like both JPA entities to contain references to each other if they exist so that if I use either repository to lookup an object for the given service, it comes with the "extra" information from the other service without needing to do more lookups by hand. 理想情况下,我希望两个JPA实体在它们存在时都包含对彼此的引用,这样如果我使用任一存储库来查找给定服务的对象,它就会附带来自其他服务的“额外”信息而无需执行更多手工查找。 From other examples on the web, I thought something like this would work: 从网络上的其他例子来看,我认为这样的事情会起作用:
@Entity
@Table(name = "service_A")
public class ServiceAData {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "service_A_pkey")
private UUID pkey;
@Column(name = "common_identifier", nullable = false)
private String commonIdentifier;
@OneToOne(optional = true, mappedBy = "serviceAData")
private ServiceBData serviceBData;
}
@Entity
@Table(name = "service_B")
public class ServiceBData {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "service_B_pkey")
private UUID pkey;
@Column(name = "common_identifier", nullable = false)
private String commonIdentifier;
@OneToOne(optional = true)
@JoinColumn(name = "common_identifier", insertable = false, updatable = false)
private ServiceAData serviceAData;
}
The application starts, but actually looking up a ServiceAData object results in 应用程序启动,但实际上查找ServiceAData对象会导致
org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = uuid
Changing ServiceAData's annotations to be exactly the same as ServiceBData's (ie, no mappedBy attribute on @OneToOne, and adding an @JoinColumn) results in essentially the same problem 将ServiceAData的注释更改为与ServiceBData完全相同(即@OneToOne上没有mappedBy属性,并添加@JoinColumn)会导致基本相同的问题
Caused by: java.lang.IllegalArgumentException: Invalid UUID string: CCGNG23DG0WQ
at java.util.UUID.fromString(UUID.java:194) ~[na:1.7.0_65]
In either case, its trying to convert text/string common_identifier column to a UUID. 在任何一种情况下,它都试图将text / string common_identifier列转换为UUID。 Am I missing something simple? 我错过了一些简单的事吗? Going about this the wrong way? 走错了路? Or is what I am trying to do just not possible? 或者我正在尝试做什么是不可能的? I'm not sure if a "bi-directional one-to-one mapping" is really what I should be googling either heh. 我不确定“双向一对一映射”是否真的应该用谷歌搜索。
To be clear, I don't want either object to own the other - I simply want looking up either object to be supplemented with the other if it is present. 要清楚,我不希望任何一个对象拥有另一个 - 我只是想要查找任何一个对象,如果它存在则补充另一个。
Thanks for any help! 谢谢你的帮助!
I am using JPA 2.1 and Hibernate 5.0.1. 我使用的是JPA 2.1和Hibernate 5.0.1。 The following works for me: 以下适用于我:
Have both mapped object members use the following annotations: 两个映射对象成员都使用以下注释:
@OneToOne(optional = true)
@JoinColumn(name = "common_identifier", referencedColumnName = "common_identifier", insertable = false, updatable = false)
Have both entities implement Serializable. 让两个实体都实现Serializable。 I'm not sure why this part is necessary, but it seems to be. 我不确定为什么这部分是必要的,但它似乎是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.