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;

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...