@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"applicationContext-test.xml"})
public class LojaServiceImplTest {
@Autowired
protected LojaService lojaService;
public LojaServiceImplTest() {
}
@Test
public void testMostraListBairro(){
System.out.println("---MostraListBairro---");
Loja loja = lojaService.getById(1);
List<LojaBairro> lojaBairroList = loja.getLojaBairroList();
for (LojaBairro lojaBairro : lojaBairroList) {
System.out.println("Bairro: "+lojaBairro.getBairro().getNome());
}
assertTrue(lojaBairroList.size() > 0);
}
}
quando eu faço o getById me retorna o objeto loja, mas logo abaixo todas as listas de relacionamento, no caso a lista de bairro vem em lazy, mas ao tentar acessar ja dispara o lazyException, mas porque logo abaixo vindo de um select ele ja vem com a sessao fechada? A cada requisicao o Spring ja fecha a sessao?
segue o meu service@Override
@Transactional(readOnly = true, propagation = Propagation.REQUIRED, rollbackFor = RuntimeException.class)
public Loja getById(int id) {
return lojaDao.findById(id);
}
public T findById(ID id, boolean lock) {
Object entity;
if (lock) {
entity = getEntityManager().find(getEntityBeanType(), id);
this.em.lock(entity, LockModeType.WRITE);
} else {
entity = getEntityManager().find(getEntityBeanType(), id);
}
return (T) entity;
}
mas isso nao acontece apenas com o byId, se eu utilizar o merge logo depois do findById, ele ativa um dos bags mas nao todos, alguem poderia me explicar melhor o que acontece com o Spring, pois sempre utilizei em Desktop e no desktop eu faço o em.clear para zerar a sessao e nao fica cache, mas no caso da web pensei que seria diferente.