I just figured out what can be happening... but I don't know why...
With an existing Roo environment, I would like to make some customized queries, so I implemented a "DAO" via @PersistenceContext injection the "clasical" way:
Code:
@Repository
public class LoteMateriaPrimaDaoImpl implements LoteMateriaPrimaDao {
@PersistenceContext
private EntityManager entityManager;
private Session hibernateSession = null;
public LoteMateriaPrimaDaoImpl() {
}
public Session getSession() {
if(hibernateSession==null){
hibernateSession = ((Session)entityManager.getDelegate());
}
return hibernateSession;
}
@SuppressWarnings("unchecked")
public List<LoteMateriaPrima> obtenerLotes(Boolean abiertos) {
List<LoteMateriaPrima> l = null;
// lotes sin inicializar, abiertos y cerrados
if(abiertos==null) {
l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima").list();
} else {
if(abiertos) {
l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima where fechaApertura is not null and fechaCierre is null").list();
} else {
l = (List<LoteMateriaPrima>) getSession().createQuery("from LoteMateriaPrima where fechaApertura is null and fechaCierre is not null").list();
}
}
return l;
}
With the following service...
Code:
@Service
@Transactional
public class GestionLotesServiceImpl implements GestionLotesService {
@Autowired
private LoteMateriaPrimaDao loteMateriaPrimaDao;
public List<LoteMateriaPrima> obtenerLotesMateriaPrima(Boolean abiertos) {
return loteMateriaPrimaDao.obtenerLotes(null);
}
(...)
And finally injected in a controller...
Code:
@Controller
public class GestionLotesController {
@Autowired
private GestionLotesService gestionLotesService;
@RequestMapping(value="/gestionarLotesMateriaPrima",method=RequestMethod.GET)
public void listadoLotes(ModelMap model) {
List<LoteMateriaPrima> lotes = gestionLotesService.obtenerLotesMateriaPrima(null);
model.addAttribute("lotesMateriaPrima",lotes);
}
there may be another Session or SessionFactory hanging around??
Is it necessary the @Repository/@Service in a class which accesses to PersistenceContext?
Thanks in advance