2012-08-29 48 views
2

调用服务时出现奇怪的错误。
Spring 3.1.1服务不延迟加载自定义对象

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

错误在我 POJO显示在用户属性(通过Eclipse的调试模式下查看)。

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(); 
    } 

    ... 
} 

:然后试图呈现视图时,会发生

com.sun.jdi.InvocationException occurred invoking method. 

将所得误差10

@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; 
    } 

    ... 
} 

我已经启用了日志记录,它看起来像它在某一点(用户= com.webapp.model.User#让他们35,泛= 5499999999999999,有效期= 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 [[email protected]] 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.springframewo[email protected]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 [[email protected]] 
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 [[email protected]] 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 


任何建议,使该Er的原因ROR?

我正在使用Spring 3.1.1.RELEASE。

回答

1

我有同样的问题,原因是thymeleaf,加载对象后,关闭数据库会话,如@ garis-m-suero在my own post上所述。

到现在为止,这是thymeleaf视图的工作方式,并注明@先生加里斯-M-末郎

你也许可以使模型的范围在春天是会话,而不是值得

编辑:

另一种选择,这似乎工作真的没事,我是这样设置你的配置的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

+0

这个解决方案的另一个选项是使加载一个从模型类需要和揭露这一切DTO对象风景。恕我直言,这更像是一种MVVM方法。 –