栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

JPA hibernate一对一关系

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JPA hibernate一对一关系

JPA不允许在 OneToOneManyToOne 映射上使用 @Id 批注。您想要做的是 与共享主键*
进行一对一的实体关联。最简单的情况是使用共享密钥的单向一对一:
*

@Entitypublic class Person {    @Id    private int id;    @oneToOne    @PrimaryKeyJoinColumn    private OtherInfo otherInfo;    rest of attributes ...}

这样做的主要问题是JPA不支持 OtherInfo
实体中共享主键的生成。鲍尔和金所著的经典书籍《Java
Persistence with
Hibernate》
使用Hibernate扩展为问题提供了以下解决方案:

@Entitypublic 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 ...}

另外,请参阅此处。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388126.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号