Hibernate:一对一延迟加载,可选= false [英] Hibernate: one-to-one lazy loading, optional = false

查看:97
本文介绍了Hibernate:一对一延迟加载,可选= false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临的问题是,一对一延迟加载在hibernate中不起作用。我已经解决,但仍然 正确理解会发生什么。



我的代码(延迟加载在这里不起作用,当我拉取Person时 - 地址也被提取):

  @Entity 
public class Person {

@Id
@SequenceGenerator(name =person_sequence ,sequenceName =sq_person)
@GeneratedValue(strategy = GenerationType.SEQUENCE,generator =person_sequence)
@Column(name =id)
private long personID;

@OneToOne(mappedBy =person,cascade = CascadeType.ALL,fetch = FetchType.LAZY)
私人地址;
// .. getters,setters
}

@Entity
public class地址{

@Id
@Column (name =id,unique = true,nullable = false)
@GeneratedValue(generator =gen)
@GenericGenerator(name =gen,strategy =foreign,parameters = @参数(name =property,value =person))
private long personID;

@PrimaryKeyJoinColumn
@OneToOne
私有FileInfo person;
}

:如果我添加 <在OneToOne关系中,延迟加载正常工作

<$ p <$ p code> optional = false

$ private私人地址; $ p $ @OneToOne(mappedBy =person,cascade = CascadeType.ALL,可选= false,fetch = FetchType.LAZY)

问题/问题:请向我解释可选= false 注释有助于实现延迟加载。 =https://stackoverflow.com/questions/1444227/making-a-onetoone-relation-lazy> post1 和 post2 ,并理解为什么简单的OneToOne不能懒惰,但我仍然无法掌握 optional = false magic。

解决方案

如果关联是可选的,Hibernate无法知道给定人的地址是否存在,一个问题。所以它不能用代理填充地址字段,因为可能没有地址引用这个人,并且它不能用null填充它,因为可能有地址引用这个人。



当您强制使用associatio(即 optional = false )时,它会信任您并假定存在地址,因为该关联是强制性的。所以它直接用代理填充地址字段,知道有一个地址引用这个人。


I faced the problem that one-to-one lazy loading doesn't work in hibernate. I've already solved it, but still don't properly understand what happens.

My code (lazy loading doesn't work here, when I pull Person - Address is also fetched):

@Entity
public class Person{

  @Id
  @SequenceGenerator(name = "person_sequence", sequenceName = "sq_person")
  @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "person_sequence")
  @Column(name = "id")
  private long personID;

  @OneToOne(mappedBy="person", cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  private Adress address;
  //.. getters, setters
}

@Entity
public class Address {

  @Id
  @Column(name="id", unique=true, nullable=false)
  @GeneratedValue(generator="gen")
  @GenericGenerator(name="gen", strategy="foreign", parameters=@Parameter(name="property", value="person"))
  private long personID;

  @PrimaryKeyJoinColumn
  @OneToOne
  private FileInfo person;
}

But: if I add optional=false in OneToOne relationship, lazy loading works fine!

@OneToOne(mappedBy="person", cascade=CascadeType.ALL, optional = false, fetch = FetchType.LAZY)
private Adress address;

Question/Entreaty: please, explain to me how optional=false annotation helps to achieve lazy loading.

P.S. I've read posts post1 and post2, and understand why simple OneToOne can't be lazy, but I still can't grasp optional=false magic.

解决方案

If the association is optional, Hibernate has no way to know if an address exists for a given person without issuing a query. So it can't populate the address field with a proxy, because there could be no addres referencing the person, and it can't populate it with null, because there might be an address referencing the person.

When you make the associatio mandatory (i.e. optional=false), it trusts you and assumes that an address exists, since the association is mandatory. So it directly populates the address field with a proxy, knowing that there is an address referencing the person.

这篇关于Hibernate:一对一延迟加载,可选= false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆