我们有一个奇怪的LazyInitializationException,它有一个实体和一个JPA存储库。但是,正如您所看到的,实体非常简单:
@Entity
@Table(name = "USERS")
@Getter
@Setter
@NoArgsConstructor
public class UserEntity {
@Id
@Column(name = "ID", nullable = false)
private Long id;
@Column(name = "NAME", length = 50)
private String name;
}
使用JPARepository:
获取@Repository
public interface UserEntityRepository extends JpaRepository<UserEntity, Long> {
}
// At some other point
final userEntity userEntity = userEntityRepository.getOne(order.getUserId());
但是,在实体上仅使用getName()会引发LazyInitializationException:
userEntity.getName();
org.hibernate.LazyInitializationException: could not initialize proxy - no Session
at org.hibernate.proxy.AbstractLazyInitializer.initialize(AbstractLazyInitializer.java:155)
at org.hibernate.proxy.AbstractLazyInitializer.getImplementation(AbstractLazyInitializer.java:268)
at org.hibernate.proxy.pojo.javassist.JavassistLazyInitializer.invoke(JavassistLazyInitializer.java:73)
at application.UserEntity_$$_jvst303_3.getName(UserEntity_$$_jvst303_3.java)
at application.OrderConverter.from(OrderConverter.java:42)
我可以看到该实体已被代理替换,但为什么此时它已被代理替换?实体中没有任何关系。
感谢您的帮助。