Java Spring JPA即使在事务中也不会初始化惰性属性

Java Spring JPA即使在事务中也不会初始化惰性属性,java,spring,hibernate,spring-data-jpa,mapping,Java,Spring,Hibernate,Spring Data Jpa,Mapping,为什么Spring JPA不初始化惰性属性MyChildEntity.myParentEntity(所有字段都为空) 我尝试使用Hibernate.initialize和@Transactional,但这没有帮助 我的服务: @Service @Transactional public class MyService { @Resource private MyChildEntityRepository myChildEntityRepository; @Resour

为什么Spring JPA不初始化惰性属性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 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...

fetch
属性指示何时应从数据库中检索相关实体 使用
javax.persistence.FetchType
enum的数据库
FetchType.EAGER
表示JPA 检索实体时,提供程序必须检索值。另一方面,
FetchType.LAZY
作为JPA提供程序的提示,它只能在属性 第一次访问(可能永远不会访问,因此节省了数据库的访问)但是,JPA提供商 不需要支持延迟加载,因此这些值可能会被急切地加载。

资料来源:Nicholas S Williams的专业Java Web应用程序

编辑:

我真的很抱歉我花了这么长时间。以下是我认为错误的地方。我在父实体中没有看到子实体的实例。应该是这样的:

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 
...     
}
公共类MyParentEntity{
…//其他字段
@OneToMany(fetch=FetchType.LAZY,mappedBy=“myParentEntity”)
私有集myChildEntities=新哈希集;
…//其他字段或构造函数或getter或setter
...     
}
我希望这能奏效。如果没有,那么在您的
MyChildEntity
类中,
@JoinColumn
中有一个奇怪的注释,名为
referencedColumnName
。我不知道那是什么。请把它拿走


谢谢

你缺少@JoinColumn吗?@BhathiyaW我对\@JoinColumn也有同样的问题(我已经更新了我的问题)。最后我意识到这是Intellij调试器的一个问题,非常感谢,但问题是我根本无法获取lazy属性。意味着我不能通过Spring JPA初始化it字段
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 
...     
}