微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

java – 休眠一到零或一个映射

我试图在Hibernate中将一个映射到“零或一个”关系.我想我可能已经找到了一种使用多对一的方法.
class A {
  private B b;
  // ... getters and setters
}

class B {
  private A a;
}

A类的映射指定:

<many-to-one name="b" class="B" 
insert="false" update="false" 
column="id" unique="true"/>

B类映射指定:

<one-to-one name="a" class="A" constrained="true"/>

我想要的是在数据库中找不到B的匹配行时,b为空.所以我可以这样做(在A班):

if (b == null)

但是,似乎b从不为空.

我能做些什么呢

解决方法

就像博登所说,答案是在A中的多对一语句中添加not-found =“ignore”.用注释来做这个:

在A类:

@ManyToOne
@Cascade({ CascadeType.ALL })
@JoinColumn(name = "Id")
@NotFound(action=NotFoundAction.IGnorE)
private B b

在B类:

@Id
@GeneratedValue(generator = "myForeignGenerator")
@org.hibernate.annotations.GenericGenerator(
    name = "myForeignGenerator",strategy = "foreign",parameters = @Parameter(name = "property",value = "a")
)
private Long subscriberId;

@OnetoOne(mappedBy="b")
@PrimaryKeyJoinColumn
@NotFound(action=NotFoundAction.IGnorE)
private A a;

原文地址:https://www.jb51.cc/java/123106.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐