为什么Spring JPA没有初始化LAZY属性MyChildEntity.myParentEntity
(所有字段都为空)?
我尝试使用Hibernate.initialize
和@Transactional
,但这没有帮助。
我的服务:
@Service
@Transactional
public class MyService {
@Resource
private MyChildEntityRepository myChildEntityRepository;
@Resource
private MyParentEntityRepository myParentEntityRepository;
@PostConstruct
public void init() {
MyParentEntity p = myParentEntityRepository.save(new MyParentEntity("my name"));
myChildEntityRepository.save(new MyChildEntity(p, "first value"));
myChildEntityRepository.save(new MyChildEntity(new MyParentEntity(1L, "another name"), "another value"));
// At this point both MyChildEntity's are in database and have correct foreign key value
List<MyChildEntity> result = myChildEntityRepository.findAll();
//even this doesn't help, myParentEntity property still has all fields equals to null
Hibernate.initialize(result.get(0).getMyParentEntity());
MyParentEntity p2 = result.get(0).getMyParentEntity();
//trigger proxy's method to initialize lazy field
System.out.print(p2.getName()); // null
System.out.println(p2.getId()); // null
// PROBLEM: p2 has all fields equals null
// the same for result.get(1)
// BUT, this works correct - returns (1L, "my name") entity
myParentEntityRepository.findAll();
}
}
子实体:
@Entity
public class MyChildEntity {
@Id
@SequenceGenerator(sequenceName = "CHILD_SEQ", name = "ChildSeq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "ChildSeq")
private Long id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "my_parent_entity_id", referencedColumnName = "id")
private MyParentEntity myParentEntity;
@Column
private String value;
// constructors, getters, setters...
父实体:
@Entity
public class MyParentEntity {
@Id
@SequenceGenerator(sequenceName = "WORKFLOW_SEQ", name = "WorkflowSeq", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "WorkflowSeq")
private Long id;
@Column
private String name;
//constructors, getters, setters...
答案 0 :(得分:0)
fetch
属性指示何时应从中检索相关实体
使用javax.persistence.FetchType
枚举的数据库。 FetchType.EAGER
表示JPA
提供程序必须在检索实体时检索值。另一方面,FetchType.LAZY
作为JPA提供者的提示,它只能在属性时等待并获取值
首先访问(可能永远不会,因此节省了数据库之旅)。 然而,JPA提供商
不需要支持延迟加载,因此无论如何都可能会急切地加载这些值。
来自 Nicholas S Williams 的 Web应用程序专业Java
编辑:
我真的道歉我花了很长时间。这是我认为错的。我没有在父实体中看到子实体的实例。它应该是这样的:
public class MyParentEntity {
... //other fields
@OneToMany(fetch = FetchType.LAZY, mappedBy = "myParentEntity")
private Set<MyChildEntity> myChildEntities = new HashSet<MyChildEntity>;
... //other fields or constructors or getters or setters
...
}
我希望这有效。如果没有,那么在MyChildEntity
课程中,@JoinColumn
内有一个名为referencedColumnName
的奇怪注释。我不知道那是什么。请删除它。
由于