我创建了一个基于springboot的用户管理服务。
用户将有一个附件列表,因此用户和附件的关系是OneToMany。
我忽略了这里的插入逻辑,因为我的问题是关于lazyload以及entitymanager何时打开和关闭的。下面是entity、controller、service、dao、repository相关代码。
实体
@Entity
@Table(name="User")
public class UserInfoEntity {
private long id;
private String mail;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "userInfoEntity", cascade = CascadeType.ALL)
private List<UserAttachmentEntity> attachmentList = new ArrayList<>();
}
@Entity
@Table(name = "ATTACHMENT")
public class UserAttachmentEntity {
private long id;
private String name;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name="userRequestId")
private UserInfoEntity userInfoEntity;
}
服务
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
@Override
@Transactional
public void save(UserInfoEntity userInfoEntity) throws RestException {
userDao.save(userInfoEntity);
}
@Override
// I did set @Transactional here
public UserInfoEntity findByMail(String mail) {
UserInfoEntity userInfoEntity = userDao.findByMail(mail);
return userInfoEntity;
}
}
DAO
@Service
public class UserDaoImpl implements UserDao {
@PersistenceContext
private EntityManager entityManager;
@Autowired
private UserInfoEntityRepository userInfoRepository;
@Override
public UserInfoEntity findByMail(String mail) {
return userInfoRepository.findFirstByMailOrderByIdDesc(mail);
}
}
存储库
@Repository
public interface UserInfoEntityRepository extends JpaRepository<UserInfoEntity, Integer> {
UserInfoEntity findFirstByMailOrderByIdDesc(String mail);
}
Controller
@Controller
@RequestMapping(value = "/user")
public class UserController {
@RequestMapping(value = "load", method = RequestMethod.POST)
@ResponseBody
public UserInfoEntity load(UserInfoEntity userInfoEntityInput, HttpServletRequest request) throws Exception {
UserInfoEntity userInfoEntity = userService.findByMail(userInfoEntityInput.getMail());
System.out.println(userInfoEntity.getAttachmentList());
return userInfoEntity;
}
}
在 Controller 中测试load方法后,我发现即使我设置了
@OneToMany(fetch = FetchType.LAZY,...) 在 用户信息实体。
我仍然可以在 Controller 中调用userInfoEntity.getAttachmentList()。 (我可以看到那里打印了从附件中选择*查询)
帖子回复lazyinitializationexception-in-spring-data-jpa说你需要在事务内部获取惰性数据。
但是我没有在服务中的findByMail方法中设置@Transaction。
我记得几天前我也遇到过这个异常。但现在我可以在 Controller 中成功加载惰性数据。
我主要有以下问题。
- 实体管理器何时打开和关闭? (是在service中还是dao中打开的?)
- 为什么 Controller 可以加载惰性数据?
- 实体管理器是线程安全的吗? (我用谷歌搜索,但没有找到有用的答案)
- 实体管理器是单例吗? (上面的代码中,我将实体管理器注入(inject)到dao中,虽然我没有使用它,我使用了spring data,我可以将实体管理器注入(inject)到service、controller中,发现hashcode不同)
提前致谢。这个问题是我在公司写的,公司不让push任何代码到github。如果可以的话,我想这对你来说会更方便,因为带有 h2 数据库的 spring boot 项目很容易在本地设置。
最佳答案
Spring Boot JPA 默认在 View 中打开 Session
如果你沿着 Appendix A. Common application properties 走,你会发现Spring boot默认定义了
spring.jpa.open-in-view=true
这实际上注册了OpenEntityManagerInViewInterceptor
。将 JPA EntityManager
绑定(bind)到线程以完成请求的整个处理。
这实际上会导致困惑,请参阅此 issue社区报道。
EntityManager何时打开和关闭?
基本上,EntityManager 打开一个事务,其中声明了 @Transaction
注释,或者在您的情况下,它由 OpenEntityManagerViewInterceptor
在每个请求上打开,并且在响应之前保持打开状态被制成。
实体管理器线程安全吗?
不。它不是线程安全的,但 EntityManagerFactory 是线程安全的。而EntityManager
是从EntityManagerFatory
获取的。因此,EntityManager 很便宜,并且预计单个工作单元只能使用一次。其中 EntityManagerFactory 通常在应用程序初始化时创建并在应用程序端关闭。有关 Obtaining an EntityManager in a Java SE environment 的详细信息,请参阅 Hibernate 文档。
实体管理器是单例吗?
没有。 EntityManager 是一个接口(interface),当您 Autowiring 时,注入(inject)到 spring bean 中的不是实体管理器本身,而是一个上下文感知代理,它将在运行时委托(delegate)给具体的实体管理器。通常,用于代理的具体类是 SharedEntityManagerInvocableHandler
有关整个 JPA 事务如何进行的更多详细说明。我建议阅读此How Does Spring @Transactional Really Work?
关于java - 对 jpa 实体管理器和延迟加载感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55019473/