Hi,
i am using Hibernate Criteria with Query by Example and property "nullable=false" for the associations inside
my Entities. The generated SQL query is not the one i expected
Query generated by Hibernate
Code:
select
this_.bp_personenid as bp1_5_1_
from
personen this_
left outer join
personenstichworte personenst1_
on this_.bp_personenid=personenst1_.bp_personenid
where
(
personenst1_.bp_personenid=?
)
and (
1=1
)
Hibernate adds the bp_personenid property to the where clause. This behaviour only occurs when nullable is set to "false". Is there any possibility to prevent Hibernate from adding bp_personenid to the query? (I have already tested Example.create(personenStichwort).excludeProperty("bp_personenid"))
Here are my Entity + Dao - Classes:
Person.java:
Code:
@Entity @Table(name="personen")
public class Person {
@Id
private Integer bp_personenid;
@OneToMany(fetch=FetchType.EAGER)
@JoinColumn(name="bp_personenid", nullable=false)
@Cascade({CascadeType.SAVE_UPDATE, CascadeType.DELETE_ORPHAN})
private Set<PersonenStichwort> personenStichworte = new HashSet<PersonenStichwort>();
public Set<PersonenStichwort> getPersonenStichworte() {
return personenStichworte;
}
PersonenStichwort.java:
Code:
@Entity
@Table(name="personenstichworte")
public class PersonenStichwort {
@Id
private Integer bp_personenid;
private String stichwort;
@Version @Generated(GenerationTime.ALWAYS) @Temporal(TemporalType.TIMESTAMP)
private Date la;
public String getStichwort() {
return stichwort;
}
public void setStichwort(String stichwort) {
this.stichwort = stichwort;
}
}
and this DAO:
Code:
public class PersonenDaoImpl extends GenericDaoImpl<Person, Integer> implements PersonenDao {
public List<Person> findPersonen(Person person, PersonenStichwort personenStichwort) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(Person.class);
criteria.createCriteria("personenStichworte",CriteriaSpecification.LEFT_JOIN)
.add(Example.create(personenStichwort).excludeProperty("bp_personenid"));
criteria.add(Example.create(person).ignoreCase().enableLike());
criteria.setResultTransformer(CriteriaSpecification.DISTINCT_ROOT_ENTITY);
return criteria.list();
}
}
Best regards,
Peter