Left outer join não esta funcionando no Hibernate

1 resposta
marciobarroso

Dae Pessaol,

Estou com um problema que já repassei várias vezes outras alternativas, mas não consegui êxito:

Tenho 2 classes simples:

@Entity
@Table(name = "t_user", uniqueConstraints = { @UniqueConstraint(columnNames = {"id"}) })
@SequenceGenerator(name = "seq_user", sequenceName = "seq_user")
public class User implements Serializable {

private static final long serialVersionUID = 8008607787929383158L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_user")
@Column( unique = true, insertable = false, updatable = false, precision = 8, scale = 0 )
private Long id;

@Column( nullable = false, length = 256, unique = false, insertable = true, updatable = true, precision = 10, scale = 0 )
private String fullName;

@Column( nullable = false, length = 6, unique = true, insertable = true, updatable = true, precision = 10, scale = 0 )
private String login;

@Column( nullable = false, length = 6, unique = false, insertable = true, updatable = true, precision = 10, scale = 0 )
private String password;

@ManyToOne
@JoinColumn(name="id_profile")
private Profile profile;

// Constructor, getters and setters
}

@Entity
@Table(name = "t_profile", uniqueConstraints = { @UniqueConstraint(columnNames = {"id_profile"}) })
@SequenceGenerator(name = "seq_profile", sequenceName = "seq_profile")
public class Profile implements Serializable {

private static final long serialVersionUID = -1544895999197247370L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "seq_profile")
@Column( name = "id_profile", unique = true, insertable = false, updatable = false, precision = 8, scale = 0 )
private Long id;

@Column( name = "description", nullable = false, unique = true, insertable = true, updatable = true, precision = 10, scale = 0 )
private String description;

@OneToMany(mappedBy="profile")
private Set<User> users;

// Constructor, getters and setters
}

E eu estou usando o suporte ao hibernate do spring framework:

@Transactional(propagation = Propagation.REQUIRED, timeout = 20)
public abstract class GenericDaoImpl<T> implements GenericDao<T>,
PropertySelector {

private static Logger logger = Logger.getLogger(GenericDaoImpl.class);

protected SessionFactory sessionFactory;

protected HibernateTemplate hibernateTemplate;

private Class<T> persistentClass;

@SuppressWarnings("unchecked")
public GenericDaoImpl() {
super();
logger.debug("Building ...");
this.persistentClass = (Class<T>) ((ParameterizedType) getClass()
.getGenericSuperclass()).getActualTypeArguments()[0];
}

public abstract void setSessionFactory(SessionFactory sessionFactory);

public abstract Session createHibernateSession();

/*
* (non-Javadoc)
*
* @see net.cinesystem.persistence.GenericDao#findByExample(java.lang.Object)
*/
@SuppressWarnings("unchecked")
public List<T> findByExample(T example) {
return this.hibernateTemplate.findByCriteria(this.mountCriteria(example));
}

/**
* This method is responsable for mount a criteria using the param object
*
* @param example
* @return DetachedCriteria
*/
private DetachedCriteria mountCriteria(T example) {
return DetachedCriteria.forClass(this.persistentClass).add(
Example.create(example).enableLike(MatchMode.ANYWHERE)
.ignoreCase().setPropertySelector(this));
}

// Another methods
}

Eu tenho uma tela de filtro, onde eu tenho os campos nome, login e o combo de perfil.
Quando eu preencho o campo nome ou login, o filtro funciona normalmente, mas se eu selecionar o perfil, o hibernate esta trazendo todos os registros que tem na base.

Este é o select que o hibernate está printando:

Hibernate:
/* criteria query */ select
this_.id as id0_1_,
this_.fullName as fullName0_1_,
this_.login as login0_1_,
this_.password as password0_1_,
this_.id_profile as id5_0_1_,
profile2_.id_profile as id1_1_0_,
profile2_.description as descript2_1_0_
from
t_user this_
left outer join
t_profile profile2_
on this_.id_profile=profile2_.id_profile
where
(
1=1
)

Sei que este where ( 1=1 ) é o motivo do erro, mas não estou conseguindo contornar a situação.

Alguém sebe me dizer o que fazer?

Mais detalhes do problema

[]'s

1 Resposta

marciobarroso

Resolvido …

Como eu já usava um dao generico que criei me baseando em alguns posts no blog da caelum e do urubatan, eu lembrei já ter visto em algum lugar a citação do problema que o hibernate tem ao incluir em um criteria umas relacionamento com outras entidades.

Aí foi só procurar qual o blog que eu tinha lido isso, e encontrei aqui : blog caelum e então complementando a leitura no post do urubatan eu matei a charada.

[]'s

Criado 26 de setembro de 2007
Ultima resposta 26 de set. de 2007
Respostas 1
Participantes 1