懒加载奇怪的行为 [英] Lazy loading strange behaviour

查看:316
本文介绍了懒加载奇怪的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用spring-mvc [Layers:Controller - > Service - > DAO - > Entities]创建web应用程序,并且我遇到了一个延迟加载的问题,这使得我变成了buffle。一般来说,有以下关系。我有一个帐户[实体]可以有许多单词[实体]和许多单词可能被分配到许多帐户,所以这是@ManyToMany。



帐户实体

  @Entity 
@Table(name =account)
@Inheritance(strategy = InheritanceType.JOINED)
public class Account {

...

@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY,targetEntity = Word.class)
@JoinTable(name =account_word,joinColumns = {@JoinColumn(name =account_id)} ,inverseJoinColumns = {@JoinColumn(name =word_id)})
private List< Word>话;

公开列表< Word> getWords(){
返回单词;
}

public void setWords(List< Word> words){
this.words = words;


Word实体 p>

  @Entity 
@Table(name =word)
@Inheritance(strategy = InheritanceType.JOINED)
public class Word {

...

@Basic
@NotBlank
@Column(name =word)
私人字符串字;

@ManyToMany(cascade = CascadeType.ALL,fetch = FetchType.LAZY)
@JoinTable(name =account_word,joinColumns = {@JoinColumn(name =word_id)}}, inverseJoinColumns = {@JoinColumn(name =account_id)})
private List< Account>账户;

公共列表<帐户> getAccounts(){
返回帐户;
}

public void setAccounts(List< Account> accounts){
this.accounts = accounts;
}

...

}

这种情况是,当用户登录时,我想在视图中显示他的单词。我有:

控制器类

  @Controller 
@RequestMapping(value =/ words)
public class WordsController {

@Autowired
AccountService accountService;

@Autowired
WordService wordService;

@RequestMapping(method = RequestMethod.GET)
public ModelAndView showWords(Principal principal){

ModelAndView model = new ModelAndView(words);
Word word = new Word();
列表< Word> accountWords = new ArrayList< Word>();

accountWords.addAll(wordService.listUserWords(principal.getName()));

model.addObject(word,word);
model.addObject(accountWords,accountWords);

返回模型;
}

服务分类 $ b

  @Transactional 
@Service
公共类WordServiceImpl实现WordService {

private static final Logger logger = LoggerFactory .getLogger(WordServiceImpl.class);

@Autowired
AccountDao accountDao;

@PersistenceContext
EntityManager entityManager;

@Override
@Transactional(propagation = Propagation.REQUIRED)
public Collection< Word> listUserWords(String username){
try {
Account foundAccount = accountDao.findUser(username);
列表< Word> userWords = foundAccount.getWords(); (Word word:userWords)
{
logger.info(Word:+ word.getWord());
}
返回userWords;
} catch(UserNotFoundException不是){
logger.error(User not found:+ username);
}
返回null;


$ / code $ / pre
$ b

DAO类 p>

  @Repository 
public class AccountDaoImpl implements AccountDao {

@PersistenceContext
private EntityManager EntityManager的;
私人CriteriaBuilder cb;

@PostConstruct
private void init(){
cb = entityManager.getCriteriaBuilder();
}

@Override
public Account findUser(String username)throws UserNotFoundException {
CriteriaQuery< Account> c = cb.createQuery(Account.class);
根<帐户> r = c.from(Account.class);
尝试{
c.select(r).where(cb.equal(r.get(username),username));
Account foundAccount = entityManager.createQuery(c).getSingleResult();
return foundAccount;
} catch(NoResultException nre){
throw new UserNotFoundException();


$ b b

JSP视图
$ b $> / strong>


  ... 
< ul id =word_list>
< c:when test =$ {not empty accountWords}>
< li class =word>
< span> $ {word.word}< / span>
< / li>
< / c:forEach>
< / c:当>
< / ul>
...

在我的Service类中,我想使用Lazy加载,奇怪的行为。片段:

  for(Word word:userWords){
logger.info(Word:+ word。屏幕取词());
}

仅用于测试,但它至关重要!没有它,我得到 LazyInitializationException ,我无法找到发生这种情况的原因。当我评论这个片段时,我得到:



$ p $ [StatefulPersistenceContext] - 初始化非懒惰集合
[Loader] - 加载集合:[model.Account.roles#104]
[AbstractBatcher] - 即将打开PreparedStatement(打开PreparedStatements:0,全局:0)
[SQL] - 选择roles0_.account_id作为account2_9_1_, roles0_.role_id as role1_1_,role1_.role_id as role1_10_0_,role1_.name as name10_0_ from account_role roles0_内部连接角色role1_ on roles0_.role_id = role1_.role_id其中roles0_.account_id =?
Hibernate:选择roles0_.account_id作为account2_9_1_,roles0_.role_id作为role1_1_,role1_.role_id作为role1_10_0_,role1_.name作为name10_0_ from account_role角色0_内部连接角色role1_ on roles0_.role_id = role1_.role_id其中roles0_.account_id = ?
[AbstractBatcher] - 即将打开ResultSet(打开ResultSets:0,全局为0)
[Loader] - 结果集包含(可能为空)collection:[model.Account.roles#104]
[Loader] - 结果行:EntityKey [model.Role#2]
[Loader] - 找到的集合行:[model.Account.roles#104]
[AbstractBatcher] - 即将关闭ResultSet(打开ResultSets:1,全局:1)
[AbstractBatcher] - 即将关闭PreparedStatement(打开PreparedStatements:1,全局:1)
[TwoPhaseLoad] - 解析[model.Role#2 ]
[TwoPhaseLoad] - 完成实体化[model.Role#2]
[loading.CollectionLoadContext] - 在结果集中为角色找到1个集合:model.Account.roles
[loading .CollectionLoadContext] - 集合完全初始化:[model.Account.roles#104]
[loading.CollectionLoadContext] - 为角色初始化的1个集合:model.Account.roles
[Loader] - 完成加载集合
[org.springframework.orm.jp a.JpaTransactionManager] - 初始化事务提交
[org.springframework.orm.jpa.JpaTransactionManager] - 在EntityManager上提交JPA事务[ejb.EntityManagerImpl@6f76dd71]
[transaction.JDBCTransaction] - commit
[event.def.AbstractFlushingEventListener] - 处理刷新级联级联
[event.def.AbstractFlushingEventListener] - 脏检查集合
[Collections] - 找到集合:[model.Account.roles#104],是:[model.Account.roles#104](初始化)
[Collections] - 找到的集合:[model.Account.words#104],是:[model.Account.words#104](未初始化)
[Collections] - 找到的集合:[model.Role.accounts#2],是:[model.Role.accounts#2](未初始化)
[event.def.AbstractFlushingEventListener] - 刷新:0插入,0更新,0删除2个对象
[event.def.AbstractFlushingEventListener] - 刷新:0(重新创建),0更新,0删除到3个集合
[pretty.Printer] - 列表实体:
[pretty.Printer] - model.Role {accounts =< uninitialized>,name = ROLE_REGISTERED,roleId = 2}
[pretty.Printer] - model.Account {username = mgrodek,registrationDate = 2012-01-07 23:15:38.464,accountId = 104,words =< uninitialized>,email=mariusz.grodek@gmail.com,roles = [model.Role#2],password = ffd1245c1e1cd7ed0af442ecc9a019e58ff2cbbe4d465b5dc4dc6b8bee16a2bf}
[transaction.JDBCTransaction] - 重新启用自动提交
[transaction.JDBCTransaction] - 提交的JDBC连接
[ConnectionManager] - 积极释放JDBC连接
[ConnectionManager] - 释放JDBC连接[(open PreparedStatements:0,全局:0)(打开ResultSets:0,全局:0)]
[com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@4ecb36fa [managed: 5,未使用:4,排除:0](例如, com.mchange.v2.c3p0.impl.NewPooledConnection@791d9ad)
[org.springframework.orm.jpa.JpaTransactionManager] - 关闭JPA EntityManager [ejb.EntityManagerImpl@6f76dd71]事务
[org.springframework .orm.jpa.EntityManagerFactoryUtils] - 关闭JPA EntityManager
[org.springframework.security.web.context.SecurityContextPersistenceFilter] - 现在已清除SecurityContextHolder,因为请求处理已完成
[org.springframework.orm.jpa。 support.OpenEntityManagerInViewFilter] - 在OpenEntityManagerInViewFilter中关闭JPA EntityManager
[org.springframework.orm.jpa.EntityManagerFactoryUtils] - 关闭JPA EntityManager
2012-05-10 29 org.apache.catalina.core.StandardWrapperValve调用
SEVERE:servlet [sndServlet]与path [/ snd]上下文中的Servlet.service()抛出异常[Request processing failed;嵌套的异常是LazyInitializationException:无法懒惰地初始化一个角色集合:model.Account.words,没有会话或会话被关闭],其根本原因是
LazyInitializationException:无法懒惰地初始化一个角色集合:model.Account.words ,没有会话或会话被关闭
在collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
在collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
在collection.AbstractPersistentCollection。 initialize(AbstractPersistentCollection.java:368)
at collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
...

但是当我对这个片段取消注释时,控制台日志是:

  [Loader] - 加载集合:[model.Account.words#104] 
[AbstractBatcher] - 即将到来打开PreparedStatement(打开PreparedStatements:0,全局:0)
[SQL] - 选择words0_.account_id为account1_9_1_,words0_.word_id为word2_1_,word1_.word_id为word1_8_0_,word1_.counter为counter8_0_,word1_.word为word8_0_ from account_word words0_ inner join word word1_ on words0_.word_id = word1_.word_id where words0_.account_id =?
Hibernate:select word0_.account_id为account1_9_1_,words0_.word_id为word2_1_,word1_.word_id为word1_8_0_,word1_.counter为counter8_0_,word1_.word为word8_0_ from account_word words0_内连接词word1_在words0_.word_id = word1_。 word_id where words0_.account_id =?
[AbstractBatcher] - 即将打开ResultSet(打开ResultSets:0,全局:0)
[Loader] - 结果集包含(可能为空)collection:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#5]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行: EntityKey [model.Word#6]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#7]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#8]
[Loader] - 找到的行集合:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#9]
[Loader] - 找到的集合行:[model.Account.words #104]
[Loader] - 结果行:EntityKey [model.Word#10]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader ] - 结果行:EntityKey [model.Word#11]
[Loader] - 找到的行集合:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#12]
[Loader] - 找到的集合行:[model.Account.words #104]
[Loader] - 结果行:EntityKey [model.Word#13]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader ] - 结果行:EntityKey [model.Word#14]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model。 Word#15]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#18]
[ Loader] - 找到一行集合:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#19]
[Loader] - 找到的集合行:[ model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#20]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#21]
[Loader] - 找到一行colle ction:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#22]
[Loader] - 找到一行集合:[model.Account.words# 104]
[Loader] - 结果行:EntityKey [model.Word#23]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#24]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word #25]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#26]
[Loader ] - 找到一行集合:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#27]
[Loader] - 找到的集合行:[model .Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#28]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#29]
[Loader] - 找到的集合行:[ model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#30]
[Loader] - 找到的集合行:[model.Account.words#104]
[Loader] - 结果行:EntityKey [model.Word#31]
[Loader] - 找到的集合行:[model.Account.words#104]
[AbstractBatcher] - about关闭ResultSet(打开ResultSets:1,全局:1)
[AbstractBatcher] - 即将关闭PreparedStatement(打开PreparedStatements:1,全局:1)
[TwoPhaseLoad] - 解析[model.Word# 5]
[TwoPhaseLoad] - 完成实体[model.Word#5]
[TwoPhaseLoad] - 解析[model.Word#6]的关联
[TwoPhaseLoad] - 完成实体化实体[ model.Word#6]
[TwoPhaseLoad] - 解析[model.Word#7]的关联
[TwoPhaseLoad] - 完成实体[model.Word#7]
[TwoPhaseLoad] - 解析[model.Word#8]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#8]
[TwoPhas eLoad] - 解析[model.Word#9]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#9]
[TwoPhaseLoad] - 解析[model.Word#10]的关联
[TwoPhaseLoad] - 完成物化实体[model.Word#10]
[TwoPhaseLoad] - 解析[model.word#11]的关联
[TwoPhaseLoad] - 完成物化实体[model.Word #11]
[TwoPhaseLoad] - 解析[model.Word#12]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#12]
[TwoPhaseLoad] - 解析[model.word#13]
[TwoPhaseLoad] - 完成实体化[model.Word#13]
[TwoPhaseLoad] - 解析[model.Word#14]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#14]
[TwoPhaseLoad] - 解析[model.word#15]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#15]
[TwoPhaseLoad] - 解析[model.word#18]的关联
[TwoPhaseLoa d] - 完成物化实体[model.Word#18]
[TwoPhaseLoad] - 解析[model.Word#19]的关联
[TwoPhaseLoad] - 完成物化实体[model.Word#19]
[TwoPhaseLoad] - 解析[model.Word#20]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#20]
[TwoPhaseLoad] - 解析[model.Word #21]
[TwoPhaseLoad] - 完成实体化[model.Word#21]
[TwoPhaseLoad] - 解析[model.Word#22]的关联
[TwoPhaseLoad] - 完成实体化实体[model.word#22]
[TwoPhaseLoad] - 解析[model.word#23]的关联
[TwoPhaseLoad] - 完成实体[model.Word#23]
[TwoPhaseLoad] - 解析[model.Word#24]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#24]
[TwoPhaseLoad] - 解析[model.Word#25]的关联
[TwoPhaseLoad] - 完成物化实体[model.Word#25]
[TwoPhaseLoad] - 为[model.word#26]解析关联
[TwoPhaseLoad] - 完成实体[model.Word#26]
[TwoPhaseLoad] - 解析[model.word#27]的关联
[TwoPhaseLoad] - 完成物化实体[model.word#27]
[TwoPhaseLoad] - 解析[model.word#28]的关联
[TwoPhaseLoad] - 完成物化实体[model.word#28 ]
[TwoPhaseLoad] - 解析[model.word#29]的关联
[TwoPhaseLoad] - 完成实体化[model.Word#29]
[TwoPhaseLoad] - 解析[model .Word#30]
[TwoPhaseLoad] - 完成实体化[model.word#30]
[TwoPhaseLoad] - 解析[model.word#31]的关联
[TwoPhaseLoad] - done materializing entity [model.Word#31]
[loading.CollectionLoadContext] - 在角色结果集中找到1个集合:model.Account.words
[loading.CollectionLoadContext] - 集合完全初始化:[model 。帐号#104]
[loading.C ollectionLoadContext] - 为角色初始化的1个集合:model.Account.words
[StatefulPersistenceContext] - 初始化非懒集合
[Loader] - 完成加载集合
INFO [service.WordServiceImpl] - Word :anetka
INFO [service.WordServiceImpl] - Word:anetka
INFO [service.WordServiceImpl] - Word:anetka
INFO [service.WordServiceImpl] - Word:lolo
INFO [ service.WordServiceImpl] - Word:test
INFO [service.WordServiceImpl] - Word:jazda
INFO [service.WordServiceImpl] - Word:aloza
INFO [service.WordServiceImpl] - Word:tata
INFO [service.WordServiceImpl] - Word:jestok
INFO [service.WordServiceImpl] - Word:test
INFO [service.WordServiceImpl] - Word:tesss
INFO [service。 WordServiceImpl] - Word:słowo
INFO [service.WordServiceImpl] - Word:hmm
INFO [service.WordServiceImpl] - Word:hmh
INFO [service.WordServiceImpl] - Word:ggd
INFO [se yyy
INFO [service.WordServiceImpl] - Word:yyy
INFO [service.WordServiceImpl] - Word:chyba
INFO [service.WordServiceImpl] - Word:sdsad
INFO [service.WordServiceImpl] - Word:pup
INFO [service.WordServiceImpl] - Word:hm
INFO [service.WordServiceImpl] - Word:ateraz
INFO [service。 WordServiceImpl] - Word:gj
INFO [service.WordServiceImpl] - Word:test
INFO [service.WordServiceImpl] - Word:test
[org.springframework.orm.jpa.JpaTransactionManager] - 初始化事务提交
[org.springframework.orm.jpa.JpaTransactionManager] - 在EntityManager上提交JPA事务[ejb.EntityManagerImpl@1c910477]
[transaction.JDBCTransaction] - 提交
[event.def .AbstractFlushingEventListener] - 处理flush-time级联
[event.def.AbstractFlushingEventListener] - 脏检查集合
[Collections] - 找到集合:[model.Account.role s#104],是:[model.Account.roles#104](初始化)
[Collections] - 找到的集合:[model.Account.words#104],是:[model.Account.words#104 ](初始化)
[Collections] - 找到的集合:[model.Role.accounts#2],是:[model.Role.accounts#2](未初始化)
[Collections] - 找到的集合: [model.Word.accounts#5],是:[model.Word.accounts#5](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#6],是:[model。 Word.accounts#6](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#7],是:[model.Word.accounts#7](未初始化)
[Collections ] - 找到的集合:[model.Word.accounts#8],是:[model.Word.accounts#8](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#9],是:[model.Word.accounts#9](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#10],是:[model.Word.accounts#10](未初始化)
[集合] - 找到的集合:[model.Word.ac计数#11],是:[model.Word.accounts#11](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#12],是:[model.Word.accounts#12 ](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#13],是:[model.Word.accounts#13](未初始化)
[Collections] - 找到的集合: [model.Word.accounts#14],是:[model.Word.accounts#14](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#15],是:[model。 Word.accounts#15](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#18],是:[model.Word.accounts#18](未初始化)
[Collections ] - 找到的集合:[model.Word.accounts#19],是:[model.Word.accounts#19](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#20],是:[model.Word.accounts#20](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#21],是:[model.Word.accounts#21](未初始化)
[收藏] - 收藏基础d:[model.Word.accounts#22],是:[model.Word.accounts#22](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#23],是:[ model.Word.accounts#23](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#24],是:[model.Word.accounts#24](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#25],是:[model.Word.accounts#25](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#26 ],是:[model.Word.accounts#26](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#27],是:[model.Word.accounts#27](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#28],是:[model.Word.accounts#28](未初始化)
[Collections] - Collection found:[model。 Word.accounts#29],是:[model.Word.accounts#29](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#30],是:[model.Word.accounts #30](未初始化)
[Collections] - 找到的集合:[model.Word.accounts#31],是:[model.Word.accounts#31](未初始化)
[event.def.AbstractFlushingEventListener] - 刷新:0插入,0更新,0删除到27个对象
[event.def.AbstractFlushingEventListener] - 刷新:0(重新)创建,0更新,0删除到28集合
[pretty.Printer] - 列出实体:
[漂亮.Printer] - model.Account {username = mgrodek,registrationDate = 2012-01-07 23:15:38.464,accountId = 104,words = [model.Word#5,model.Word#6,model.Word#7,模型.Word#8,model.Word#9,model.Word#10,model.Word#11,model.Word#12,model.Word#13,model.Word#14,model.Word#15,model。词#18,model.word#19,model.Word#20,model.Word#21,model.Word#22,model.Word#23,model.Word#24,model.Word#25,model.Word# 26,model.word#27,model.word#28,model.Word#29,model.Word#30,model.Word#31],email=mariusz.grodek@gmail.com,roles = [model.Role# 2],密码= ffd1245c1e1cd7ed0af442ecc9a019e58ff2cbbe4d465b5dc4dc6b8bee16a2bf}
[漂亮.printer] - model.Word {id = 31,accounts = ,counter = 1,word = test}
[pretty.Printer] - model.Word {id = 30,accounts =< uninitialized> ; counter = 1,word = test}
[pretty.Printer] - model.Word {id = 27,accounts = ,counter = 1,word = hm}
[漂亮。打印机] - model.Word {id = 26,accounts = <未初始化>,counter = 1,word = pup}
[pretty.Printer] - model.Word {id = 29,accounts =< uninitialized> ,counter = 1,word = gj}
[pretty.Printer] - model.Word {id = 28,accounts = ,counter = 1,word = ateraz}
[pretty.Printer ] model.Role {accounts =< uninitialized> ;, name = ROLE_REGISTERED,roleId = 2}
[pretty.Printer] - model.Word {id = 5,accounts =< uninitialized> ;, counter = word = anetka}
[pretty.Printer] - model.Word {id = 9,accounts =< uninitialized>,counter = 1,word = test}
[pretty.Printer] - model.Word {id = 8,accounts =< uninitialized>,counter = 1,word = lolo}
[pretty.Printer] - model.Wor d = {id = 7,accounts =< uninitialized>,counter = 1,word = anetka}
[pretty.Printer] - model.Word {id = 6,accounts = word = anetka}
[pretty.Printer] - model.Word {id = 20,accounts =< uninitialized>,counter = 1,word = hmh}
[pretty.Printer] - model.Word {id = 21,accounts = ,counter = 1,word = ggd}
[pretty.Printer] - model.Word {id = 18,accounts = ,counter = 1,word =słowo}
[pretty.Printer] - model.Word {id = 19,accounts = ,counter = 1,word = hmm}
[pretty.Printer] - model.Word { id = 24,accounts =< uninitialized>,counter = 1,word = chyba}
[pretty.Printer] - model.Word {id = 25,accounts = ,counter = 1,word = sdsad}
[pretty.Printer] - model.Word {id = 22,accounts = ,counter = 1,word = yyy}
[pretty.Printer] - model.Word {id = 23,账户= <未初始化>,计数器= 1,字= yyy}
[transaction.JDBCTransaction] - 重新启用aut ocommit
[transaction.JDBCTransaction] - 提交的JDBC连接
[ConnectionManager] - 积极释放JDBC连接
[ConnectionManager] - 释放JDBC连接[(打开PreparedStatements:0,全局:0)(open结果集:0,全局:0)]
[com.mchange.v2.resourcepool.BasicResourcePool] - 跟踪com.mchange.v2.resourcepool.BasicResourcePool@3918d722 [托管:5,未使用:4,排除:0] (例如com.mchange.v2.c3p0.impl.NewPooledConnection@5bb77832)
[org.springframework.orm.jpa.JpaTransactionManager] - 关闭JPA EntityManager [ejb.EntityManagerImpl@1c910477]事务
[org.springframework .orm.jpa.EntityManagerFactoryUtils] - 关闭JPA EntityManager
[org.springframework.security.web.access.ExceptionTranslationFilter] - 正常处理的链
[org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder现已清除,因为请求处理已完成
[org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] - 关闭OpenEntityManagerInViewFilter中的JPA EntityManager
[org.springframework.orm.jpa.EntityManagerFactoryUtils] - 关闭JPA EntityManager

单词传递给视图,一切正确。为什么发生这种情况?有人可以帮我解决这个问题,因为我不想再把这个片段与记录器放在我的代码中了。



也许有一些问题,延迟加载无法正常工作?我在我的服务类中有@Transactional,我使用OpenEntityManagerInViewFilter。最后,这是我的短版web.xml文件:

web.xml

 < web-app version =3.0xmlns =http://java.sun.com/xml/ns/javaee
xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance
xmlns:jee =http://www.springframework.org/schema/jee
xsi:schemaLocation =http ://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\">

< filter>
< filter-name> JpaFilter< / filter-name>
< filter-class> org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter< / filter-class>
< / filter>
< filter-mapping>
< filter-name> JpaFilter< / filter-name>
< url-pattern> / *< / url-pattern>
< / filter-mapping>

< display-name> snd< / display-name>

< listener>
< listener-class> org.springframework.web.context.ContextLoaderListener< / listener-class>
< / listener>

< servlet>
< servlet-name> sndServlet< / servlet-name>
< servlet-class> org.springframework.web.servlet.DispatcherServlet< / servlet-class>
< / servlet>

< servlet-mapping>
< servlet-name> sndServlet< / servlet-name>
< url-pattern> *。html< / url-pattern>
< / servlet-mapping>

< / web-app>


解决方案

您的服务类和方法标记为 @Transactional ,当你在一个事务中执行hibernate操作时,hibernate会话是打开的,你的映射使Lazy加载的 @ManyToMany 当你在一个循环中访问延迟加载对象的属性 word 时,会话是开放的,因此hibernate能够初始化代理并为你获取它们,因此它们也可以在视图上工作,因为它们被正确初始化而不是懒惰objects.



When you comment the code, they are lazy proxies and when you try to access them in view, that too a property of that entity ie word, it tries to fetch it but its too late no hibernate session is active and hence the exception.



Tweak it to FetchType.EAGER and see the difference.



Hope this helps....


I am creating web app with spring-mvc [Layers: Controller -> Service -> DAO -> Entities] and I faced a problem with lazy loading which makes me buffled. In general there is the following relationship. I have an Account [Entity] which can have many Words [Entity] and many Words may be assigned to many Accounts, so this is @ManyToMany.

Account Entity

  @Entity
  @Table(name = "account")
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Account {

  ...

  @ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY, targetEntity = Word.class)
  @JoinTable(name = "account_word", joinColumns = {@JoinColumn(name="account_id")}, inverseJoinColumns = {@JoinColumn(name="word_id")})
  private List<Word> words;

  public List<Word> getWords() {
      return words;
  }

  public void setWords(List<Word> words) {
      this.words = words;
  }
  }

Word Entity

  @Entity
  @Table(name = "word")
  @Inheritance(strategy = InheritanceType.JOINED)
  public class Word {

  ...

  @Basic
  @NotBlank
  @Column(name = "word")
  private String word;

  @ManyToMany(cascade=CascadeType.ALL, fetch = FetchType.LAZY)
  @JoinTable(name = "account_word", joinColumns = {@JoinColumn(name="word_id")}, inverseJoinColumns = {@JoinColumn(name="account_id")})
  private List<Account> accounts;

  public List<Account> getAccounts() {
      return accounts;
  }

  public void setAccounts(List<Account> accounts) {
      this.accounts = accounts;
  }

  ...

  }

The case is that when user logs in I want to show in a view his words. I have:

Controller Class

  @Controller
  @RequestMapping(value="/words")
  public class WordsController {

  @Autowired
  AccountService accountService;

  @Autowired
  WordService wordService;

  @RequestMapping(method=RequestMethod.GET)
  public ModelAndView showWords(Principal principal) {

      ModelAndView model = new ModelAndView("words");
      Word word = new Word();
      List<Word> accountWords = new ArrayList<Word>();

      accountWords.addAll(wordService.listUserWords(principal.getName()));

      model.addObject("word", word);
      model.addObject("accountWords", accountWords);

      return model;
  }

Service Class

  @Transactional
  @Service
  public class WordServiceImpl implements WordService {

  private static final Logger logger = LoggerFactory.getLogger(WordServiceImpl.class);

  @Autowired
  AccountDao accountDao;

  @PersistenceContext
  EntityManager entityManager;

  @Override
  @Transactional(propagation = Propagation.REQUIRED)
  public Collection<Word> listUserWords(String username) {
      try {
          Account foundAccount = accountDao.findUser(username);
          List<Word> userWords = foundAccount.getWords();
          for (Word word : userWords) {
              logger.info("Word: " + word.getWord());
          }
          return userWords;
      } catch (UserNotFoundException unf) {
          logger.error("User not found: " + username);
      }
      return null;
  }
  }

DAO Class

  @Repository
  public class AccountDaoImpl implements AccountDao {

  @PersistenceContext
  private EntityManager entityManager;
  private CriteriaBuilder cb;

  @PostConstruct
  private void init() {
      cb = entityManager.getCriteriaBuilder();
  }

  @Override
  public Account findUser(String username) throws UserNotFoundException {
      CriteriaQuery<Account> c = cb.createQuery(Account.class);
      Root<Account> r = c.from(Account.class);
      try {
          c.select(r).where(cb.equal(r.get("username"), username));
          Account foundAccount = entityManager.createQuery(c).getSingleResult();
          return foundAccount;
      } catch(NoResultException nre){
          throw new UserNotFoundException();
      }
  }
  }

JSP View

  ...
  <ul id="word_list">
      <c:choose>
          <c:when test="${not empty accountWords}">
               <c:forEach items="${accountWords}" var="word" varStatus="status">
                   <li class="word">
                       <span>${word.word}</span>
                   </li>
               </c:forEach>
          </c:when>
      </c:choose>
  </ul>
  ...

In my Service class I want to use Lazy loading but there is very strange behaviour. The fragment:

   for (Word word : userWords) {
        logger.info("Word: " + word.getWord());
   }

is just for testing but it occured crucial! Without it i'm getting LazyInitializationException and I can't find the reason why this is happening. When I comment this fragment I'm getting:

  [StatefulPersistenceContext] - initializing non-lazy collections
  [Loader] - loading collection: [model.Account.roles#104]
  [AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
  [SQL] - select roles0_.account_id as account2_9_1_, roles0_.role_id as role1_1_, role1_.role_id as role1_10_0_, role1_.name as name10_0_ from account_role roles0_ inner join role role1_ on roles0_.role_id=role1_.role_id where roles0_.account_id=?
  Hibernate: select roles0_.account_id as account2_9_1_, roles0_.role_id as role1_1_, role1_.role_id as role1_10_0_, role1_.name as name10_0_ from account_role roles0_ inner join role role1_ on roles0_.role_id=role1_.role_id where roles0_.account_id=?
  [AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
  [Loader] - result set contains (possibly empty) collection: [model.Account.roles#104]
  [Loader] - result row: EntityKey[model.Role#2]
  [Loader] - found row of collection: [model.Account.roles#104]
  [AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
  [AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
  [TwoPhaseLoad] - resolving associations for [model.Role#2]
  [TwoPhaseLoad] - done materializing entity [model.Role#2]
  [loading.CollectionLoadContext] - 1 collections were found in result set for role: model.Account.roles
  [loading.CollectionLoadContext] - collection fully initialized: [model.Account.roles#104]
  [loading.CollectionLoadContext] - 1 collections initialized for role: model.Account.roles
  [Loader] - done loading collection
  [org.springframework.orm.jpa.JpaTransactionManager] - Initiating transaction commit
  [org.springframework.orm.jpa.JpaTransactionManager] - Committing JPA transaction on EntityManager [ejb.EntityManagerImpl@6f76dd71]
  [transaction.JDBCTransaction] - commit
  [event.def.AbstractFlushingEventListener] - processing flush-time cascades
  [event.def.AbstractFlushingEventListener] - dirty checking collections
  [Collections] - Collection found: [model.Account.roles#104], was: [model.Account.roles#104] (initialized)
  [Collections] - Collection found: [model.Account.words#104], was: [model.Account.words#104] (uninitialized)
  [Collections] - Collection found: [model.Role.accounts#2], was: [model.Role.accounts#2] (uninitialized)
  [event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 0 updates, 0 deletions to 2 objects
  [event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 3 collections
  [pretty.Printer] - listing entities:
  [pretty.Printer] - model.Role{accounts=<uninitialized>, name=ROLE_REGISTERED, roleId=2}
  [pretty.Printer] - model.Account{username=mgrodek, registrationDate=2012-01-07 23:15:38.464, accountId=104, words=<uninitialized>, email=mariusz.grodek@gmail.com, roles=[model.Role#2], password=ffd1245c1e1cd7ed0af442ecc9a019e58ff2cbbe4d465b5dc4dc6b8bee16a2bf}
  [transaction.JDBCTransaction] - re-enabling autocommit
  [transaction.JDBCTransaction] - committed JDBC Connection
  [ConnectionManager] - aggressively releasing JDBC connection
  [ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
  [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@4ecb36fa [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@791d9ad)
  [org.springframework.orm.jpa.JpaTransactionManager] - Closing JPA EntityManager [ejb.EntityManagerImpl@6f76dd71] after transaction
  [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
  [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
  [org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] - Closing JPA EntityManager in OpenEntityManagerInViewFilter
  [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
  2012-02-02 29 org.apache.catalina.core.StandardWrapperValve invoke
  SEVERE: Servlet.service() for servlet [sndServlet] in context with path [/snd] threw exception [Request processing failed; nested exception is LazyInitializationException: failed to lazily initialize a collection of role: model.Account.words, no session or session was closed] with root cause
  LazyInitializationException: failed to lazily initialize a collection of role: model.Account.words, no session or session was closed
    at collection.AbstractPersistentCollection.throwLazyInitializationException(AbstractPersistentCollection.java:383)
    at collection.AbstractPersistentCollection.throwLazyInitializationExceptionIfNotConnected(AbstractPersistentCollection.java:375)
    at collection.AbstractPersistentCollection.initialize(AbstractPersistentCollection.java:368)
    at collection.AbstractPersistentCollection.read(AbstractPersistentCollection.java:111)
   ...

But when I have this fragment uncommented the console log is:

  [Loader] - loading collection: [model.Account.words#104]
  [AbstractBatcher] - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
  [SQL] - select words0_.account_id as account1_9_1_, words0_.word_id as word2_1_, word1_.word_id as word1_8_0_, word1_.counter as counter8_0_, word1_.word as word8_0_ from account_word words0_ inner join word word1_ on words0_.word_id=word1_.word_id where words0_.account_id=?
  Hibernate: select words0_.account_id as account1_9_1_, words0_.word_id as word2_1_, word1_.word_id as word1_8_0_, word1_.counter as counter8_0_, word1_.word as word8_0_ from account_word words0_ inner join word word1_ on words0_.word_id=word1_.word_id where words0_.account_id=?
  [AbstractBatcher] - about to open ResultSet (open ResultSets: 0, globally: 0)
  [Loader] - result set contains (possibly empty) collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#5]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#6]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#7]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#8]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#9]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#10]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#11]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#12]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#13]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#14]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#15]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#18]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#19]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#20]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#21]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#22]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#23]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#24]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#25]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#26]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#27]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#28]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#29]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#30]
  [Loader] - found row of collection: [model.Account.words#104]
  [Loader] - result row: EntityKey[model.Word#31]
  [Loader] - found row of collection: [model.Account.words#104]
  [AbstractBatcher] - about to close ResultSet (open ResultSets: 1, globally: 1)
  [AbstractBatcher] - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
  [TwoPhaseLoad] - resolving associations for [model.Word#5]
  [TwoPhaseLoad] - done materializing entity [model.Word#5]
  [TwoPhaseLoad] - resolving associations for [model.Word#6]
  [TwoPhaseLoad] - done materializing entity [model.Word#6]
  [TwoPhaseLoad] - resolving associations for [model.Word#7]
  [TwoPhaseLoad] - done materializing entity [model.Word#7]
  [TwoPhaseLoad] - resolving associations for [model.Word#8]
  [TwoPhaseLoad] - done materializing entity [model.Word#8]
  [TwoPhaseLoad] - resolving associations for [model.Word#9]
  [TwoPhaseLoad] - done materializing entity [model.Word#9]
  [TwoPhaseLoad] - resolving associations for [model.Word#10]
  [TwoPhaseLoad] - done materializing entity [model.Word#10]
  [TwoPhaseLoad] - resolving associations for [model.Word#11]
  [TwoPhaseLoad] - done materializing entity [model.Word#11]
  [TwoPhaseLoad] - resolving associations for [model.Word#12]
  [TwoPhaseLoad] - done materializing entity [model.Word#12]
  [TwoPhaseLoad] - resolving associations for [model.Word#13]
  [TwoPhaseLoad] - done materializing entity [model.Word#13]
  [TwoPhaseLoad] - resolving associations for [model.Word#14]
  [TwoPhaseLoad] - done materializing entity [model.Word#14]
  [TwoPhaseLoad] - resolving associations for [model.Word#15]
  [TwoPhaseLoad] - done materializing entity [model.Word#15]
  [TwoPhaseLoad] - resolving associations for [model.Word#18]
  [TwoPhaseLoad] - done materializing entity [model.Word#18]
  [TwoPhaseLoad] - resolving associations for [model.Word#19]
  [TwoPhaseLoad] - done materializing entity [model.Word#19]
  [TwoPhaseLoad] - resolving associations for [model.Word#20]
  [TwoPhaseLoad] - done materializing entity [model.Word#20]
  [TwoPhaseLoad] - resolving associations for [model.Word#21]
  [TwoPhaseLoad] - done materializing entity [model.Word#21]
  [TwoPhaseLoad] - resolving associations for [model.Word#22]
  [TwoPhaseLoad] - done materializing entity [model.Word#22]
  [TwoPhaseLoad] - resolving associations for [model.Word#23]
  [TwoPhaseLoad] - done materializing entity [model.Word#23]
  [TwoPhaseLoad] - resolving associations for [model.Word#24]
  [TwoPhaseLoad] - done materializing entity [model.Word#24]
  [TwoPhaseLoad] - resolving associations for [model.Word#25]
  [TwoPhaseLoad] - done materializing entity [model.Word#25]
  [TwoPhaseLoad] - resolving associations for [model.Word#26]
  [TwoPhaseLoad] - done materializing entity [model.Word#26]
  [TwoPhaseLoad] - resolving associations for [model.Word#27]
  [TwoPhaseLoad] - done materializing entity [model.Word#27]
  [TwoPhaseLoad] - resolving associations for [model.Word#28]
  [TwoPhaseLoad] - done materializing entity [model.Word#28]
  [TwoPhaseLoad] - resolving associations for [model.Word#29]
  [TwoPhaseLoad] - done materializing entity [model.Word#29]
  [TwoPhaseLoad] - resolving associations for [model.Word#30]
  [TwoPhaseLoad] - done materializing entity [model.Word#30]
  [TwoPhaseLoad] - resolving associations for [model.Word#31]
  [TwoPhaseLoad] - done materializing entity [model.Word#31]
  [loading.CollectionLoadContext] - 1 collections were found in result set for role: model.Account.words
  [loading.CollectionLoadContext] - collection fully initialized: [model.Account.words#104]
  [loading.CollectionLoadContext] - 1 collections initialized for role: model.Account.words
  [StatefulPersistenceContext] - initializing non-lazy collections
  [Loader] - done loading collection
  INFO  [service.WordServiceImpl] - Word: anetka
  INFO  [service.WordServiceImpl] - Word: anetka
  INFO  [service.WordServiceImpl] - Word: anetka
  INFO  [service.WordServiceImpl] - Word: lolo
  INFO  [service.WordServiceImpl] - Word: test
  INFO  [service.WordServiceImpl] - Word: jazda
  INFO  [service.WordServiceImpl] - Word: aloza
  INFO  [service.WordServiceImpl] - Word: tata
  INFO  [service.WordServiceImpl] - Word: jestok
  INFO  [service.WordServiceImpl] - Word: test
  INFO  [service.WordServiceImpl] - Word: tesss
  INFO  [service.WordServiceImpl] - Word: słowo
  INFO  [service.WordServiceImpl] - Word: hmm
  INFO  [service.WordServiceImpl] - Word: hmh
  INFO  [service.WordServiceImpl] - Word: ggd
  INFO  [service.WordServiceImpl] - Word: yyy
  INFO  [service.WordServiceImpl] - Word: yyy
  INFO  [service.WordServiceImpl] - Word: chyba
  INFO  [service.WordServiceImpl] - Word: sdsad
  INFO  [service.WordServiceImpl] - Word: pup
  INFO  [service.WordServiceImpl] - Word: hm
  INFO  [service.WordServiceImpl] - Word: ateraz
  INFO  [service.WordServiceImpl] - Word: gj
  INFO  [service.WordServiceImpl] - Word: test
  INFO  [service.WordServiceImpl] - Word: test
   [org.springframework.orm.jpa.JpaTransactionManager] - Initiating transaction commit
   [org.springframework.orm.jpa.JpaTransactionManager] - Committing JPA transaction on EntityManager [ejb.EntityManagerImpl@1c910477]
   [transaction.JDBCTransaction] - commit
   [event.def.AbstractFlushingEventListener] - processing flush-time cascades
  [event.def.AbstractFlushingEventListener] - dirty checking collections
  [Collections] - Collection found: [model.Account.roles#104], was: [model.Account.roles#104] (initialized)
  [Collections] - Collection found: [model.Account.words#104], was: [model.Account.words#104] (initialized)
  [Collections] - Collection found: [model.Role.accounts#2], was: [model.Role.accounts#2] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#5], was: [model.Word.accounts#5] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#6], was: [model.Word.accounts#6] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#7], was: [model.Word.accounts#7] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#8], was: [model.Word.accounts#8] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#9], was: [model.Word.accounts#9] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#10], was: [model.Word.accounts#10] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#11], was: [model.Word.accounts#11] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#12], was: [model.Word.accounts#12] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#13], was: [model.Word.accounts#13] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#14], was: [model.Word.accounts#14] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#15], was: [model.Word.accounts#15] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#18], was: [model.Word.accounts#18] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#19], was: [model.Word.accounts#19] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#20], was: [model.Word.accounts#20] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#21], was: [model.Word.accounts#21] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#22], was: [model.Word.accounts#22] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#23], was: [model.Word.accounts#23] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#24], was: [model.Word.accounts#24] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#25], was: [model.Word.accounts#25] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#26], was: [model.Word.accounts#26] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#27], was: [model.Word.accounts#27] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#28], was: [model.Word.accounts#28] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#29], was: [model.Word.accounts#29] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#30], was: [model.Word.accounts#30] (uninitialized)
  [Collections] - Collection found: [model.Word.accounts#31], was: [model.Word.accounts#31] (uninitialized)
  [event.def.AbstractFlushingEventListener] - Flushed: 0 insertions, 0 updates, 0 deletions to 27 objects
  [event.def.AbstractFlushingEventListener] - Flushed: 0 (re)creations, 0 updates, 0 removals to 28 collections
  [pretty.Printer] - listing entities:
  [pretty.Printer] - model.Account{username=mgrodek, registrationDate=2012-01-07 23:15:38.464, accountId=104, words=[model.Word#5, model.Word#6, model.Word#7, model.Word#8, model.Word#9, model.Word#10, model.Word#11, model.Word#12, model.Word#13, model.Word#14, model.Word#15, model.Word#18, model.Word#19, model.Word#20, model.Word#21, model.Word#22, model.Word#23, model.Word#24, model.Word#25, model.Word#26, model.Word#27, model.Word#28, model.Word#29, model.Word#30, model.Word#31], email=mariusz.grodek@gmail.com, roles=[model.Role#2], password=ffd1245c1e1cd7ed0af442ecc9a019e58ff2cbbe4d465b5dc4dc6b8bee16a2bf}
  [pretty.Printer] - model.Word{id=31, accounts=<uninitialized>, counter=1, word=test}
  [pretty.Printer] - model.Word{id=30, accounts=<uninitialized>, counter=1, word=test}
  [pretty.Printer] - model.Word{id=27, accounts=<uninitialized>, counter=1, word=hm}
  [pretty.Printer] - model.Word{id=26, accounts=<uninitialized>, counter=1, word=pup}
  [pretty.Printer] - model.Word{id=29, accounts=<uninitialized>, counter=1, word=gj}
  [pretty.Printer] - model.Word{id=28, accounts=<uninitialized>, counter=1, word=ateraz}
  [pretty.Printer] - model.Role{accounts=<uninitialized>, name=ROLE_REGISTERED, roleId=2}
  [pretty.Printer] - model.Word{id=5, accounts=<uninitialized>, counter=1, word=anetka}
  [pretty.Printer] - model.Word{id=9, accounts=<uninitialized>, counter=1, word=test}
  [pretty.Printer] - model.Word{id=8, accounts=<uninitialized>, counter=1, word=lolo}
  [pretty.Printer] - model.Word{id=7, accounts=<uninitialized>, counter=1, word=anetka}
  [pretty.Printer] - model.Word{id=6, accounts=<uninitialized>, counter=1, word=anetka}
  [pretty.Printer] - model.Word{id=20, accounts=<uninitialized>, counter=1, word=hmh}
  [pretty.Printer] - model.Word{id=21, accounts=<uninitialized>, counter=1, word=ggd}
  [pretty.Printer] - model.Word{id=18, accounts=<uninitialized>, counter=1, word=słowo}
  [pretty.Printer] - model.Word{id=19, accounts=<uninitialized>, counter=1, word=hmm}
  [pretty.Printer] - model.Word{id=24, accounts=<uninitialized>, counter=1, word=chyba}
  [pretty.Printer] - model.Word{id=25, accounts=<uninitialized>, counter=1, word=sdsad}
  [pretty.Printer] - model.Word{id=22, accounts=<uninitialized>, counter=1, word=yyy}
  [pretty.Printer] - model.Word{id=23, accounts=<uninitialized>, counter=1, word=yyy}
  [transaction.JDBCTransaction] - re-enabling autocommit
  [transaction.JDBCTransaction] - committed JDBC Connection
  [ConnectionManager] - aggressively releasing JDBC connection
  [ConnectionManager] - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
  [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@3918d722 [managed: 5, unused: 4, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@5bb77832)
  [org.springframework.orm.jpa.JpaTransactionManager] - Closing JPA EntityManager [ejb.EntityManagerImpl@1c910477] after transaction
  [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager
  [org.springframework.security.web.access.ExceptionTranslationFilter] - Chain processed normally
  [org.springframework.security.web.context.SecurityContextPersistenceFilter] - SecurityContextHolder now cleared, as request processing completed
  [org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter] - Closing JPA EntityManager in OpenEntityManagerInViewFilter
  [org.springframework.orm.jpa.EntityManagerFactoryUtils] - Closing JPA EntityManager

And words are passed to a view and everything is correct. Why this is happening? Could someone help me with it because i don't want to have this fragment with logger in my code anymore.

Maybe there are some problems with fetching and lazy loading is not working properly? I have @Transactional in my Service Class and I use OpenEntityManagerInViewFilter. At the end this is my shorted version of web.xml file:

web.xml

      <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:jee="http://www.springframework.org/schema/jee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

      <filter>
          <filter-name>JpaFilter</filter-name>
          <filter-class>org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter</filter-class>
      </filter>
      <filter-mapping>
          <filter-name>JpaFilter</filter-name>
          <url-pattern>/*</url-pattern>
      </filter-mapping>

      <display-name>snd</display-name>

      <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>     

      <servlet>
          <servlet-name>sndServlet</servlet-name>
          <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      </servlet>

      <servlet-mapping>
          <servlet-name>sndServlet</servlet-name>
          <url-pattern>*.html</url-pattern>
      </servlet-mapping>

  </web-app>

解决方案

Your service class and method is marked @Transactional, when you are executing hibernate operations in a transaction, hibernate session is open, your mapping makes @ManyToMany side as Lazy loaded, when you access property word of lazy loaded object in a loop, session is open and hence hibernate is able to intialize proxy and get them for you and hence they work on view too as they are properly initialized not lazy objects.

When you comment the code, they are lazy proxies and when you try to access them in view, that too a property of that entity i.e. word, it tries to fetch it but its too late no hibernate session is active and hence the exception.

Tweak it to FetchType.EAGER and see the difference.

Hope this helps....

这篇关于懒加载奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆