- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 spring-mvc [层: Controller -> 服务 -> DAO -> 实体] 创建 Web 应用程序,我遇到了延迟加载的问题,这让我感到困惑。一般有以下关系。我有一个帐户[实体],它可以有许多单词[实体],并且许多单词可以分配给许多帐户,所以这是@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> words;
public List<Word> getWords() {
return words;
}
public void setWords(List<Word> words) {
this.words = words;
}
}
词实体
@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;
}
...
}
情况是,当用户登录时,我想在 View 中显示他的话。我有:
Controller 类
@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;
}
服务等级
@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 类
@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>
...
在我的服务类中,我想使用延迟加载,但有非常奇怪的行为。片段:
for (Word word : userWords) {
logger.info("Word: " + word.getWord());
}
只是为了测试,但它的发生至关重要!没有它,我会得到LazyInitializationException,并且我找不到发生这种情况的原因。当我评论这个片段时,我得到:
[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)
...
但是当我取消注释该片段时,控制台日志为:
[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
文字被传递到 View ,一切都是正确的。为什么会发生这种情况?有人可以帮助我吗,因为我不想再在我的代码中包含这个带有记录器的片段。
也许抓取存在一些问题,延迟加载无法正常工作?我的服务类中有 @Transactional,并且使用 OpenEntityManagerInViewFilter。最后,这是我的 web.xml 文件的简短版本:
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>
最佳答案
你的服务类和方法被标记为@Transactional
,当你在事务中执行hibernate操作时,hibernate session 是打开的,你的映射使@ManyToMany
侧作为Lazy已加载,当您在循环中访问延迟加载对象的属性 word 时, session 已打开,因此 Hibernate 能够初始化代理并为您获取它们,因此它们也可以在 View 上工作,因为它们已正确初始化不是惰性对象。
当您注释代码时,它们是惰性代理,当您尝试在 View 中访问它们时,这也是该实体的属性,即 word,它会尝试获取它,但为时已晚 hibernate session 处于 Activity 状态,因此出现异常。
将其调整为FetchType.EAGER
并查看差异。
希望这有帮助......
关于java - 延迟加载奇怪的行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9105099/
我想要显示正在加载的 .gif,直到所有内容都已加载,包括嵌入的 iframe。但是,目前加载 gif 会在除 iframe 之外的所有内容都已加载后消失。我怎样才能让它等到 iframe 也加载完毕
首先,这是我第一次接触 Angular。 我想要实现的是,我有一个通知列表,我必须以某种方式限制 limitTo,因此元素被限制为三个,在我单击按钮后,其余的应该加载。 我不明白该怎么做: 设置“ V
我正在尝试在我的设备上运行这个非常简单的应用程序(使用 map API V2),并且出于某种原因尝试使用 MapView 时: 使用 java 文件: public class MainMap e
我正在使用 Python 2.6、Excel 2007 Professional 和最新版本的 PyXLL。在 PyXLL 中加载具有 import scipy 抛出异常,模块未加载。有没有人能够在
我想做这个: 创建并打包原始游戏。然后我想根据原始游戏中的蓝图创建具有新网格/声音/动画和蓝图的其他 PAK 文件。原始游戏不应该知道有关其他网格/动画/等的任何信息。因此,我需要在原始游戏中使用 A
**摘要:**在java项目中经常会使用到配置文件,这里就介绍几种加载配置文件的方法。 本文分享自华为云社区《【Java】读取/加载 properties配置文件的几种方法》,作者:Copy工程师。
在 Groovy 脚本中是否可以执行条件导入语句? if (test){ import this.package.class } else { import that.package.
我正在使用 NVidia 视觉分析器(来自 CUDA 5.0 beta 版本的基于 eclipse 的版本)和 Fermi 板,我不了解其中两个性能指标: 全局加载/存储效率表示实际内存事务数与请求事
有没有办法在通过 routeProvider 加载特定 View 时清除 Angular JS 存储的历史记录? ? 我正在使用 Angular 创建一个公共(public)安装,并且历史会积累很多,
使用 Xcode 4.2,在我的应用程序中, View 加载由 segue 事件触发。 在 View Controller 中首先调用什么方法? -(void) viewWillAppear:(BOO
我在某些Django模型中使用JSONField,并希望将此数据从Oracle迁移到Postgres。 到目前为止,当使用Django的dumpdata和loaddata命令时,我仍然没有运气来保持J
创建 Nib 时,我需要创建两种类型:WindowNib 或 ViewNib。我看到的区别是,窗口 Nib 有一个窗口和一个 View 。 如何将 View Nib 加载到另一个窗口中?我是否必须创建
我想将多个env.variables转换为静态结构。 我可以手动进行: Env { is_development: env::var("IS_DEVELOPMENT")
正如我从一个测试用例中看到的:https://godbolt.org/z/K477q1 生成的程序集加载/存储原子松弛与普通变量相同:ldr 和 str 那么,宽松的原子变量和普通变量之间有什么区别吗
我有一个重定向到外部网站的按钮/链接,但是外部网站需要一些时间来加载。所以我想添加一个加载屏幕,以便外部页面在显示之前完全加载。我无法控制外部网站,并且外部网站具有同源策略,因此我无法在 iFrame
我正在尝试为我的应用程序开发一个Dockerfile,该文件在初始化后加载大量环境变量。不知何故,当我稍后执行以下命令时,这些变量是不可用的: docker exec -it container_na
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我刚刚遇到一个问题,我有一个带有一些不同选项的选择标签。 现在我想检查用户选择了哪些选项。 然后我想将一个新的 html 文件加载到该网站(取决于用户选中的选项)宽度 javascript,我该怎么做
我知道两种保存/加载应用程序设置的方法: 使用PersistentStore 使用文件系统(存储,因为 SDCard 是可选的) 我想知道您使用应用程序设置的做法是什么? 使用 PersistentS
我开始使用 Vulkan 时偶然发现了我的第一个问题。尝试创建调试报告回调时(验证层和调试扩展在我的英特尔 hd vulkan 驱动程序上可用,至少它是这么说的),它没有告诉我 vkCreateDeb
我是一名优秀的程序员,十分优秀!