Hello Tenwit,
I really want to figure this out. This is quite bizarre.
Which Hibernate version do you use and which database?
I use
Hibernate 3.1 with PostgreSQL 8.1
EH Cache is configured
The select of course returns the collection without the "non fitting" teams.
So when Hibernate does create the objects directly from the select then my result would occure.
When Hibernate does look for all fitting departments and then select the team collection information, than of course your result should happen.
b% has the following result:
Code:
Hibernate: select department0_.id as id19_0_, teams1_.id as id21_1_, department0_.name as name19_0_, teams1_.name as name21_1_, teams1_.department_id as department3_21_1_, teams1_.department_id as department3_0__, teams1_.id as id0__ from examples.tdepartment department0_ left outer join examples.tteam teams1_ on department0_.id=teams1_.department_id where teams1_.name like ?
10:59:15,265 DEBUG TestQuery:52 - Department: 1 Name: a department
10:59:15,265 DEBUG TestQuery:56 - Team: 2 Name: b
10:59:15,265 DEBUG TestQuery:52 - Department: 2 Name: b department
10:59:15,265 DEBUG TestQuery:56 - Team: 3 Name: b2
Damn, what is really worse, I run a second query to achieve your behaviour directly after the first. This query does return my results but uses the collections from the cache, which is of course wrong. After adding a session.refresh, I got your results.
This would be a dangerous problem you must be aware when working in the same session.
Regards Sebastian
Code:
sets = new HashSet(session
.createQuery(
"from Department d where d.teams.name like :name")
.setString("name", "b%").list());
for (Iterator iter = sets.iterator(); iter.hasNext();) {
Department element = (Department) iter.next();
session.refresh(element);
log.debug(element);
for (Iterator iterator = element.getTeams().iterator(); iterator
.hasNext();) {
Team team = (Team) iterator.next();
log.debug(team);
}
}
Just to verify this with yours:
Hibernate configuration
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="myeclipse.connection.profile">library-web</property>
<property name="connection.url">jdbc:postgresql://localhost:5432/libraryweb</property>
<property name="connection.username">postgres</property>
<property name="connection.password">p</property>
<property name="connection.driver_class">org.postgresql.Driver</property>
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="current_session_context_class">thread</property>
<property name="hibernate.transaction.factory_class">org.hibernate.transaction.JDBCTransactionFactory</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.default_schema">examples</property>
<mapping resource="de/laliluna/example4/Department.hbm.xml" />
<mapping resource="de/laliluna/example4/Member.hbm.xml" />
<mapping resource="de/laliluna/example4/Team.hbm.xml" />
</session-factory>
</hibernate-configuration>