我遇到了一个问题,即一对一的延迟加载在休眠状态下不起作用。我已经解决了它,但仍然无法正确理解会发生什么。我的代码(延迟加载在这里不起作用,当我拉人时-地址也被获取):@Entitypublic 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}@Entitypublic 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;}但是:如果我添加optional=falseOneToOne关系,则延迟加载效果很好!@OneToOne(mappedBy="person", cascade=CascadeType.ALL, optional = false, fetch = FetchType.LAZY)private Adress address;问题/完整性:请向我解释optional=false注释如何帮助实现延迟加载。PS我已经阅读了post1和post2的帖子,并了解了为什么简单的OneToOne不能偷懒,但是我仍然无法掌握optional=false魔术。
2 回答
- 2 回答
- 0 关注
- 412 浏览
添加回答
举报
0/150
提交
取消