Dear Members
I have these two DAO method implementation
The first is this, based in
List<> collection because is closely related with
FROM Entidad for the HQL
Quote:
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<Entidad> getAllEntidadByTipo(String tipo) {
return this.sessionFactory.getCurrentSession()
.createQuery("FROM Entidad e WHERE e.tipoEntidad=:tipo")
.setParameter("tipo", tipo).list();
}
and the second, based with
SELECT COUNT(*) and Integer
(I tried even with
createQuery)
Quote:
public Integer getCountAllEntidadByApellidoPaterno(String apellidoPaterno){
return this.sessionFactory.getCurrentSession().
createSQLQuery("SELECT COUNT(*) FROM Entidad e WHERE e.apellidosPaternoEntidad=:apellidoPaterno").
setParameter("apellidoPaterno", apellidoPaterno.trim()).list().size();
}
This is my Test method
Quote:
@Test
public void getCountAllEntidadByApellidoPaternoFailTest(){
@SuppressWarnings("static-access")
Entidad entidad = this.entidadFactory.createEntidadManolito();
this.entidadDaoService.insertarEntidad(entidad);
@SuppressWarnings("static-access")
Entidad entidad01 = this.entidadFactory.createEntidadAlumno01();
this.entidadDaoService.insertarEntidad(entidad01);
@SuppressWarnings("static-access")
Entidad entidad02 = this.entidadFactory.createEntidadAlumno02();
this.entidadDaoService.insertarEntidad(entidad02);
List<Entidad> entidades = this.entidadDaoService.getAllEntidadByTipo("alumno");//First DAO Method
System.out.println("******getCountAllEntidadByApellidoPaternoFailTest() entidades.size(): "+ entidades.size());
Integer count = this.entidadDaoService.getCountAllEntidadByApellidoPaterno("XXX");//Second dao method
System.out.println("+++++++++++++++count: "+count);
assertTrue("Debo ser 0", count == 0 );
}
After to exceture the Test Class, I receive these values for this method
Quote:
******getCountAllEntidadByApellidoPaternoFailTest() entidades.size(): 3
Hibernate: SELECT COUNT(*) FROM Entidad e WHERE e.apellidosPaternoEntidad=?
+++++++++++++++count: 1
The color red work fine like I expected, because I used
entidadFactory three times to insert the values,
BTW these three entities has the same last name 'Jordan', later in the test method I am using XXX (color orange) where is an invalid data or no existsThe blue part is weird and wrong, why return 1?,
it must return 0, not 1!, even Hibernate show the sql output and is correct, what is wrong?
Even worst, if I change
Quote:
Integer count = this.entidadDaoService.getCountAllEntidadByApellidoPaterno("XXX");//Second dao method
(should return 0)
to
Quote:
Integer count = this.entidadDaoService.getCountAllEntidadByApellidoPaterno("Jordan");//Second dao method
(should return 3,
with List<> approach return 3)
I receive the same
+++++++++++++++count: 1What is wrong?
Here arise two questions
1) Why work fine with
List<> approach??, I mean
From Entidad .. and not with
Select Count(*) From ...., I mean without
List<> approach
2) I am trying to use
Select Count(*) because I think is better for performance reasons because it is returning a single
number(Integer) where instead I think if I use
FROM Entidad .. it load a collection of Entities, where consume more resources, knowing that each row item is an independent Java class instance and furthermore even bear in mind considering other factor, the amount of properties defined for any this class, 10 ... 40, 50 fields etc
Am I correct with this idea? about (2) or not exists difference?, and I am complicating unnecessarily myself?
Thanks in advanced