Hibernate/JPA可选的一对一双向映射

编程入门 行业动态 更新时间:2024-10-21 06:48:05
本文介绍了Hibernate/JPA可选的一对一双向映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个表,每个表用于描述有关对象的信息,但它们来自两个不同的系统.这样,一个给定的对象可能同时存在于一个或另一个中.

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>

service_B --------------- service_B_pkey UUID NOT NULL PRIMARY KEY, common_identifier TEXT NOT NULL, <more columns to store data>

理想情况下,我希望两个JPA实体都包含彼此的引用(如果它们存在的话),因此,如果我使用任一存储库来查找给定服务的对象,则它会附带来自另一服务的额外"信息而不会需要手动进行更多查找.从网络上的其他示例中,我认为这样的方法会起作用:

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

应用程序启动,但实际上查找ServiceAData对象会导致

The application starts, but actually looking up a ServiceAData object results in

org.postgresql.util.PSQLException: ERROR: operator does not exist: character varying = uuid

将ServiceAData的批注更改为与ServiceBData的批注完全相同(即,@OneToOne上没有mappingBy属性,并添加@JoinColumn)会导致本质上相同的问题

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

Caused by: java.lang.IllegalArgumentException: Invalid UUID string: CCGNG23DG0WQ at java.util.UUID.fromString(UUID.java:194) ~[na:1.7.0_65]

无论哪种情况,它都试图将text/string common_identifier列转换为UUID.我是否缺少简单的东西?这样做是错误的方式吗?还是我试图做的只是不可能?我不确定双向一对一映射"是否真的应该谷歌搜索.

In either case, its trying to convert text/string common_identifier column to a 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.

感谢您的帮助!

推荐答案

我正在使用JPA 2.1和Hibernate 5.0.1.以下对我有用:

I am using JPA 2.1 and 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)

两个实体都实现了Serializable.我不确定为什么这部分是必要的,但似乎是必要的.

Have both entities implement Serializable. I'm not sure why this part is necessary, but it seems to be.

更多推荐

Hibernate/JPA可选的一对一双向映射

本文发布于:2023-10-14 22:06:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1492350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:可选   双向   Hibernate   JPA

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!