Hi!
In my object model Product has one name and zero or more secondary names (synonyms) mapped as unidirectional one-to-many. Using Criteria
API I try to put resriction on synonyms, and I expect products without synonyms at all to be included in results also, but Hibernate makes this impossible by generating "inner join" instead of "left outer join".
Is this behavior by design? Any workarounds?
Hibernate version:
3.02
Mapping documents:
Code:
<class name="mydomain.om.Product" table="products" >
<id name="id" column="id" access="field">
<generator class="assigned"/>
</id>
<property name="name" access="field"/>
<bag name="synonyms" access="field" order-by="id asc" outer-join="true" >
<key column="productId" />
<one-to-many class="ua.spectruminfo.om.Synonym"/>
</bag>
</class>
<class name="mydomain.om.Synonym" table="syns" >
<id name="id" access="field">
<generator class="native"/>
</id>
<property name="name" access="field"/>
</class>
Code between sessionFactory.openSession() and session.close():Code:
Criteria crit = session.createCriteria(Product.class);
crit.createCriteria("synonyms").add(
Restrictions.or(
Restrictions.like("name","%"+query.getSynonym()+"%"),
Restrictions.isNull("name") // <-----this should give me products w/o synonyms too
)
);
List products = crit.list()
Name and version of the database you are using:MySQL 4.0
The generated SQL (show_sql=true):Code:
select this_.id as id2_, this_.name as name1_2_, synonym2_.id as id0_, synonym2_.name as name5_0_ from products this_ inner join syns synonym2_ on this_.id=synonym2_.productId where synonym2_.name like ? or synonym2_.name is null limit ?