2

调用服务时出现奇怪的错误。

它不会将用户对象加载到卡 pojo 中。

错误显示在我的pojo中的用户属性上(通过 eclipse 调试模式查看)。

com.sun.jdi.InvocationException occurred invoking method.

然后在尝试渲染视图时发生错误:

SEVERE: Servlet.service() for servlet [WebApp] in context with path [/WebApp] threw exception [Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "user.card.alias" (card/index:37)] with root cause
org.hibernate.LazyInitializationException: could not initialize proxy - no Session


@Controller
public class CardController extends AppController
{
    static Logger logger = LoggerFactory.getLogger(CardController.class);

    @Autowired
    private ICardService cardService;

    // Card home page (list all cards and options)
    @RequestMapping(value = "/card", method = RequestMethod.GET)
    public String cardMain(Model model, HttpServletRequest request)
    {
        PagedListHolder<Card> cards = new PagedListHolder<Card>(
                cardService.getAll());

        ...
    }

    ...
}


@Service
public class CardService implements ICardService
{
    @Autowired
    ICardDAO cardDAO;

    @Transactional
    public List<Card> getAll()
    {
        return cardDAO.findAll();
    }

    ...
}


@Entity
public class Card implements Serializable
{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumns({ @JoinColumn(name = "USERID", referencedColumnName = "ID") })
    @Valid
    private User user;

    public User getUser()
    {
        if (user == null)
        {
            user = new User();
        }

        return user;
    }

    public void setUser(User user)
    {
        this.user = user;
    }

    ...
}


@Entity
public class User implements Serializable
{
    @OneToOne(mappedBy="user", cascade={CascadeType.ALL})
    private UserRole userRole;

    @OneToMany(mappedBy = "user", fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
    private Set<Card> cards;

    public UserRole getUserRole()
    {
        if (userRole == null)
        {
            userRole = new UserRole();
        }

        return userRole;
    }

    public void setUserRole(UserRole userRole)
    {
        this.userRole = userRole;
    }

    public Set<Card> getCards()
    {
        if (cards == null)
        {
            cards = new LinkedHashSet<Card>();
        }

        return cards;
    }

    public void setCards(Set<Card> cards)
    {
        this.cards = cards;
    }

    ...
}

我启用了日志记录,看起来它确实在某个时候得到了它们(user=com.webapp.model.User#35,pan=5499999999999999,expiry=1214....):

2012-08-30 00:15:47,212 DEBUG [http-bio-8080-exec-4] o.s.b.f.s.DefaultListableBeanFactory [AbstractBeanFactory.java:245] Returning cached instance of singleton bean 'transactionManager'
2012-08-30 00:15:47,214 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:365] Creating new transaction with name [com.webapp.service.impl.CardService.getAll]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
2012-08-30 00:15:47,215 DEBUG [http-bio-8080-exec-4] o.h.i.SessionImpl [SessionImpl.java:265] opened session at timestamp: 13462821472
2012-08-30 00:15:47,216 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [JpaTransactionManager.java:368] Opened new EntityManager [org.hibernate.ejb.EntityManagerImpl@d49865b] for JPA transaction
2012-08-30 00:15:47,216 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:78] begin
2012-08-30 00:15:47,217 DEBUG [http-bio-8080-exec-4] o.h.j.ConnectionManager [ConnectionManager.java:444] opening JDBC connection
2012-08-30 00:15:47,217 DEBUG [http-bio-8080-exec-4] o.s.j.d.DriverManagerDataSource [DriverManagerDataSource.java:162] Creating new JDBC DriverManager Connection to [jdbc:mysql://localhost:3306/Thesis]
2012-08-30 00:15:47,288 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:83] current autocommit status: true
2012-08-30 00:15:47,289 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:86] disabling autocommit
2012-08-30 00:15:47,290 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [JpaTransactionManager.java:400] Exposing JPA transaction as JDBC transaction [org.springframework.orm.jpa.vendor.HibernateJpaDialect$HibernateConnectionHandle@296fb592]
2012-08-30 00:15:47,292 DEBUG [http-bio-8080-exec-4] o.h.j.AbstractBatcher [AbstractBatcher.java:410] about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2012-08-30 00:15:47,293 DEBUG [http-bio-8080-exec-4] o.h.SQL [SQLStatementLogger.java:111] select card0_.id as id6_, card0_.active as active6_, card0_.alias as alias6_, card0_.cvc as cvc6_, card0_.expiry as expiry6_, card0_.pan as pan6_, card0_.USERID as USERID6_ from Card card0_
2012-08-30 00:15:47,297 DEBUG [http-bio-8080-exec-4] o.h.j.AbstractBatcher [AbstractBatcher.java:426] about to open ResultSet (open ResultSets: 0, globally: 0)
2012-08-30 00:15:47,300 DEBUG [http-bio-8080-exec-4] o.h.l.Loader [Loader.java:1322] result row: EntityKey[com.webapp.model.Card#1]
2012-08-30 00:15:47,301 DEBUG [http-bio-8080-exec-4] o.h.l.Loader [Loader.java:1322] result row: EntityKey[com.webapp.model.Card#2]
2012-08-30 00:15:47,303 DEBUG [http-bio-8080-exec-4] o.h.l.Loader [Loader.java:1322] result row: EntityKey[com.webapp.model.Card#3]
2012-08-30 00:15:47,317 DEBUG [http-bio-8080-exec-4] o.h.j.AbstractBatcher [AbstractBatcher.java:433] about to close ResultSet (open ResultSets: 1, globally: 1)
2012-08-30 00:15:47,317 DEBUG [http-bio-8080-exec-4] o.h.j.AbstractBatcher [AbstractBatcher.java:418] about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2012-08-30 00:15:47,318 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:130] resolving associations for [com.webapp.model.Card#1]
2012-08-30 00:15:47,319 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:255] done materializing entity [com.webapp.model.Card#1]
2012-08-30 00:15:47,320 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:130] resolving associations for [com.webapp.model.Card#2]
2012-08-30 00:15:47,321 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:255] done materializing entity [com.webapp.model.Card#2]
2012-08-30 00:15:47,322 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:130] resolving associations for [com.webapp.model.Card#3]
2012-08-30 00:15:47,323 DEBUG [http-bio-8080-exec-4] o.h.e.TwoPhaseLoad [TwoPhaseLoad.java:255] done materializing entity [com.webapp.model.Card#3]
2012-08-30 00:15:47,339 DEBUG [http-bio-8080-exec-4] o.h.e.StatefulPersistenceContext [StatefulPersistenceContext.java:893] initializing non-lazy collections
2012-08-30 00:15:47,339 TRACE [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:922] Triggering beforeCommit synchronization
2012-08-30 00:15:47,340 TRACE [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:935] Triggering beforeCompletion synchronization
2012-08-30 00:15:47,341 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:752] Initiating transaction commit
2012-08-30 00:15:47,341 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [JpaTransactionManager.java:507] Committing JPA transaction on EntityManager [org.hibernate.ejb.EntityManagerImpl@d49865b]
2012-08-30 00:15:47,342 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:130] commit
2012-08-30 00:15:47,342 DEBUG [http-bio-8080-exec-4] o.h.e.d.AbstractFlushingEventListener [AbstractFlushingEventListener.java:134] processing flush-time cascades
2012-08-30 00:15:47,345 DEBUG [http-bio-8080-exec-4] o.h.e.d.AbstractFlushingEventListener [AbstractFlushingEventListener.java:177] dirty checking collections
2012-08-30 00:15:47,347 DEBUG [http-bio-8080-exec-4] o.h.e.d.AbstractFlushingEventListener [AbstractFlushingEventListener.java:108] Flushed: 0 insertions, 0 updates, 0 deletions to 12 objects
2012-08-30 00:15:47,348 DEBUG [http-bio-8080-exec-4] o.h.e.d.AbstractFlushingEventListener [AbstractFlushingEventListener.java:114] Flushed: 0 (re)creations, 0 updates, 0 removals to 0 collections
2012-08-30 00:15:47,349 DEBUG [http-bio-8080-exec-4] o.h.p.Printer [Printer.java:106] listing entities:
2012-08-30 00:15:47,354 DEBUG [http-bio-8080-exec-4] o.h.p.Printer [Printer.java:113] com.webapp.model.Card{id=2, alias=sadsdsdf, cvc=111, active=true, user=com.webapp.model.User#35, pan=5499999999999999, expiry=1214}
2012-08-30 00:15:47,355 DEBUG [http-bio-8080-exec-4] o.h.p.Printer [Printer.java:113] com.webapp.model.Card{id=3, alias=asdsdsdfsdf, cvc=122, active=false, user=com.webapp.model.User#47, pan=5411222222222222, expiry=1214}
2012-08-30 00:15:47,358 DEBUG [http-bio-8080-exec-4] o.h.p.Printer [Printer.java:113] com.webapp.model.Card{id=1, alias=sdfsdfsdfd, cvc=111, active=true, user=com.webapp.model.User#35, pan=5411111111111111, expiry=1214}
2012-08-30 00:15:47,361 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:223] re-enabling autocommit
2012-08-30 00:15:47,363 DEBUG [http-bio-8080-exec-4] o.h.t.JDBCTransaction [JDBCTransaction.java:143] committed JDBC Connection
2012-08-30 00:15:47,363 DEBUG [http-bio-8080-exec-4] o.h.j.ConnectionManager [ConnectionManager.java:427] aggressively releasing JDBC connection
2012-08-30 00:15:47,364 DEBUG [http-bio-8080-exec-4] o.h.j.ConnectionManager [ConnectionManager.java:464] releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2012-08-30 00:15:47,365 TRACE [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:948] Triggering afterCommit synchronization
2012-08-30 00:15:47,365 TRACE [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [AbstractPlatformTransactionManager.java:964] Triggering afterCompletion synchronization
2012-08-30 00:15:47,366 DEBUG [http-bio-8080-exec-4] o.s.o.j.JpaTransactionManager [JpaTransactionManager.java:593] Closing JPA EntityManager [org.hibernate.ejb.EntityManagerImpl@d49865b] after transaction
2012-08-30 00:15:47,367 DEBUG [http-bio-8080-exec-4] o.s.o.j.EntityManagerFactoryUtils [EntityManagerFactoryUtils.java:343] Closing JPA EntityManager


关于这个错误的原因有什么建议吗?

顺便说一句,我正在使用 Spring 3.1.1.RELEASE。

4

1 回答 1

1

我有同样的问题,原因是百里香,加载对象后,关闭数据库会话,正如@garis-m-suero 在我自己的帖子中所述。

到目前为止,这就是 thymeleaf 视图的工作方式,并引用 @garis-m-suero 先生

您可能可以在春季将模型的范围设为会话,但这不值得

编辑:

另一个对我来说似乎真的很好的选择是在您的配置上设置 OpenSessionInViewInterceptor,如下所示:

@Bean
public OpenSessionInViewInterceptor openSessionInViewInterceptor(){
    OpenSessionInViewInterceptor openSessionInterceptor = new OpenSessionInViewInterceptor();
    openSessionInterceptor.setSessionFactory(sessionFactory);
    return openSessionInterceptor;
}

他们用这个覆盖 addInterceptor :

@Override
public void addInterceptors(InterceptorRegistry registry) { 
    registry.addInterceptor(thymeleafLayoutInterceptor());
    registry.addInterceptor(applicationInterceptor());
    registry.addWebRequestInterceptor(openSessionInViewInterceptor());
}

在github上找到了这个解决方案

于 2015-05-05T21:41:49.327 回答