I have mapped photo album and photos. In this moment in database exists two album with two photos each one.
When the collection in album.class is LAZY, the DAO return two albums (correct) but when I transform the collection in EAGER, the DAO return four albums.
I understand that it is a real behavior but I need that the collection is EAGER and the DAO return two albums.
album.classCode:
@OneToMany(mappedBy="album",cascade=CascadeType.ALL,orphanRemoval=true)
private Collection<Foto> fotos=new ArrayList<Foto>();
/**
* return total photos
* @return numfotografias
*/
public Integer getContarFotografias(){
return this.fotos.size();
}
/**
* Return random index photo
* @return indexFotografia
*/
public Integer getFotografiaAleatoria(){
Random rand=new Random();
ArrayList<Foto> listaFotos=(ArrayList<Foto>)this.fotos;
// (MAX - MIN + 1) + MIN
return rand.nextInt(listaFotos.get(listaFotos.size()).getIdFoto() - listaFotos.get(0).getIdFoto() +1 ) + listaFotos.get(0).getIdFoto();
}
photo.classCode:
@ManyToOne
@JoinColumn(name="IDALBUM")
private Album album;
albumDAOImpl.classCode:
@SuppressWarnings("unchecked")
public List<Album> getAlbumnes(Integer idAsociacion,boolean esMiembro) throws DataAccessException{
logger.trace("AlbumDAOImpl.getAlbumnes");
Criteria criteria=getSession().createCriteria(Album.class);
if(esMiembro==false)
criteria.add(Restrictions.eq("privado",false));
Criteria criteriaAso=criteria.createCriteria("asociacion");
criteriaAso.add(Restrictions.eq("idAsociacion",idAsociacion));
return (List<Album>)criteria.list();
}
Really, I dont understand because in EAGER collection return four albums. Someone can me explain this behavior?
Kinds regards.