【发布时间】:2015-10-03 09:00:18
【问题描述】:
当我从控制器访问此方法时遇到此异常:
{"status":"failure","exception":"LazyInitializationException","exceptionMessage":"失败 懒惰地初始化角色集合: org.mainco.subco.lessonplan.domain.LessonPlan.classrooms,不能 初始化代理 - 没有会话","errorMessage":"懒惰地失败 初始化角色集合: org.mainco.subco.lessonplan.domain.LessonPlan.classrooms,不能 初始化代理 - 没有会话”}
控制器:
@Autowired
private ThirdPartyService m_thirdPartySvc;
…
@RequestMapping(value = "/launch", method = RequestMethod.GET)
@Transactional
public String launchLti(final @RequestParam String assignmentId,
final Model model,
final HttpServletRequest request,
final HttpServletResponse response,
final Principal principal) throws InvalidKeyException, UnsupportedEncodingException, NoSuchAlgorithmException
{
final subcoAuthenticationUser auth = (subcoAuthenticationUser) ((Authentication) principal).getPrincipal();
String nextPage = null;
final User user = m_userSvc.findById(auth.getId());
// Provision the assignment in ThirdParty if not already done so
final Assignment assmt = m_lessonPlanDao.getAssignment(assignmentId);
if (!assmt.isSentToThirdParty())
{
m_thirdPartySvc.sendAssignment(assignmentId);
} // if
@Transactional 注释是不必要的吗?特别是因为我已经在我的@Service 课堂上使用了它……
@Service
@Transactional
public class ThirdPartyServiceImpl implements ThirdPartyService
{
@Override
public void sendAssignment(final String assignmentId)
{
final Assignment assignment = m_lessonPlanDao.getAssignment(assignmentId);
if (isThirdPartyAssignment(assignment))
{
final String ThirdPartyPromptId = assignment.getTocItem().getThirdPartyPromptId();
// Gather the teacher id
final LessonPlan lessonPlan = m_lessonPlanDao.getLessonPlan(assignment.getLessonPlan().getId());
final String teacherId = lessonPlan.getOwnerId();
// Gather the students who have been assigned this assignment
final List<Classroom> classes = lessonPlan.getClassrooms();
// Send one request for each class assignment
for (final Classroom classroom : classes)
{
错误发生在for (final Classroom classroom : classes) 行。我到处都有@Transactional,但我得到了这个LazyInitializationException。为什么?以及如何创建事务以便我可以运行我的方法?
我在 JBoss 7.1.3.Final 上使用 Spring 3.2.11.RELEASE、Hibernate 4.3.6.Final 和 JPA 2.1。如果升级其中任何一个可以解决我的问题,请告诉我。
【问题讨论】:
标签: spring hibernate transactional lazy-initialization hibernate-4.x