在@Transactional方法中获取LazyInitializationException

时间:2015-07-14 15:50:04

标签: spring hibernate transactional lazy-initialization hibernate-4.x

当我从我的控制器访问此方法时,我收到此异常:

  

{ “状态”: “失败”, “异常”: “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。如果升级其中任何一个都可以解决我的问题,请告诉我。

0 个答案:

没有答案