JPA休眠一对一关系

编程入门 行业动态 更新时间:2024-10-24 20:19:47
本文介绍了JPA休眠一对一关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一对一的关系,但是hibernatetool在生成模式时抱怨。下面是一个显示问题的例子:

@Entity public class Person { @Id public int id; @OneToOne public OtherInfo otherInfo; 其余属性... }

与OtherInfo的一对一关系:

@Entity 公共类OtherInfo { @Id @OneToOne(mappedBy =otherInfo) public Person person; 其余属性... }

Person is拥有OtherInfo的一面。 OtherInfo是所有者,所以人们使用 mappedBy 在Person中指定属性名称otherInfo。

我得到使用hibernatetool生成数据库模式时出现以下错误:

org.hibernate.MappingException:无法确定类型:Person,在表格:OtherInfo中,对于列:[org.hibernate.mapping.Column(person)] 在org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292)在org.hibernate。 mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175) at org.hibernate.cfg.Configuration.iterateGenerators(Configuration.java:743) at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration。 java:854) at org.hibernate.tool.hbm2ddl.SchemaExport。< init>(SchemaExport.java:128) ...

任何想法为什么?我是一个做错了什么或者这是一个Hibernate的bug?

解决方案

JPA不允许 @Id 或 ManyToOne 映射的注释。您试图做的是使用共享主键的一对一实体关联。最简单的情况是单向一对一共享密钥:

@Entity public class Person { @Id private int id; @OneToOne @PrimaryKeyJoinColumn 私有OtherInfo otherInfo; 其余属性... }

主要问题在于JPA不支持在 OtherInfo 实体中共享主键生成。经典书籍 Bauer和King提供的Hibernate Java持久化给出了以下解决方案Hibernate扩展:

@Entity public class OtherInfo { @Id @GeneratedValue(generator =customForeignGenerator ) @ org.hibernate.annotations.GenericGenerator( name =customForeignGenerator, strategy =foreign, parameters = @Parameter(name =property,值=人))私人长ID; @OneToOne(mappedBy =otherInfo) @PrimaryKeyJoinColumn public Person person; 其余属性... }

另外,请参阅此处。

I have a one-to-one relationship but hibernatetool complains when generating the schema. Here's an example that shows the problem:

@Entity public class Person { @Id public int id; @OneToOne public OtherInfo otherInfo; rest of attributes ... }

Person has a one-to-one relationship with OtherInfo:

@Entity public class OtherInfo { @Id @OneToOne(mappedBy="otherInfo") public Person person; rest of attributes ... }

Person is owning side of OtherInfo. OtherInfo is the owned side so person uses mappedBy to specify the attribute name "otherInfo" in Person.

I get the following error when using hibernatetool to generate the database schema:

org.hibernate.MappingException: Could not determine type for: Person, at table: OtherInfo, for columns: [org.hibernate.mapping.Column(person)] at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:292) at org.hibernate.mapping.SimpleValue.createIdentifierGenerator(SimpleValue.java:175) at org.hibernate.cfg.Configuration.iterateGenerators(Configuration.java:743) at org.hibernate.cfg.Configuration.generateDropSchemaScript(Configuration.java:854) at org.hibernate.tool.hbm2ddl.SchemaExport.<init>(SchemaExport.java:128) ...

Any idea why? Am I a doing something wrong or is this a Hibernate bug?

解决方案

JPA doesn't allow the @Id annotation on a OneToOne or ManyToOne mapping. What you are trying to do is one-to-one entity association with shared primary key. The simplest case is unidirectional one-to-one with shared key:

@Entity public class Person { @Id private int id; @OneToOne @PrimaryKeyJoinColumn private OtherInfo otherInfo; rest of attributes ... }

The main problem with this is that JPA provides no support for shared primary key generation in OtherInfo entity. The classic book Java Persistence with Hibernate by Bauer and King gives the following solution to the problem using Hibernate extension:

@Entity public class OtherInfo { @Id @GeneratedValue(generator = "customForeignGenerator") @org.hibernate.annotations.GenericGenerator( name = "customForeignGenerator", strategy = "foreign", parameters = @Parameter(name = "property", value = "person") ) private Long id; @OneToOne(mappedBy="otherInfo") @PrimaryKeyJoinColumn public Person person; rest of attributes ... }

Also, see here.

更多推荐

JPA休眠一对一关系

本文发布于:2023-11-16 11:51:08,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   JPA

发布评论

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

>www.elefans.com

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