Hi,
when using the QBE interface I got a problem with the following code from my ImportFileHome.findOneByExample method:
Code:
List results = HibernateUtil.getCurrentSession().createCriteria(ImportFile.class).add(Example.create(instance)).list();
The instance object is a new ImportFile object which has the facility set to "fac" and the master to a valid master instance from the database.
See the "Code between ..." section for more details.
The Problem:The generated query (see "The generated SQL") does not contain anything from the master instance and I have no idea why. Please have a look. As a result of the generated query all ImportFiles from the DB are returned.
Hibernate version: 3.1
Mapping documents:Code:
<hibernate-mapping>
<class name="data.ImportFile" table="import_file" schema="public">
<id name="id" type="short">
<column name="id" not-null="true" />
<generator class="sequence">
<param name="sequence">import_file_id_seq</param>
</generator>
</id>
<many-to-one name="master" class="data.Master" foreign-key="fk_master_file">
<column name="master_id" not-null="true" unique-key="uk_import_file" />
</many-to-one>
<property name="facility" type="string" >
<column name="facility" length="10" not-null="true" unique-key="uk_import_file" />
</property>
<property name="lastDayFileName" type="string">
<column name="last_day_file_name" length="50" not-null="true" />
</property>
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Master master = MasterHome.findById(new Short(1));
ImportFile importFile = new ImportFile();
importFile.setFacility("fac");
importFile.setMaster(master);
importFile = ImportFileHome.findOneByExample(importFile);
importFile.setLastDayFileName("FileName");
HibernateUtil.getCurrentSession().save(importFile);
Name and version of the database you are using:PostgreSQL 8.0.4
The generated SQL (show_sql=true):Code:
Hibernate: /* criteria query */ select this_.id as id9_0_, this_.master_id as master2_9_0_, this_.facility as facility9_0_, this_.last_day_file_name as last4_9_0_ from public.import_file this_ where (this_.facility=?)
The "where" clause should contain the id of the master instance or not? I would expect something like "where (this_.facility=? and this_master_id = ?)".
When I use
Code:
String masterId = "master_id =" + instance.getMaster().getId().shortValue();
String facility = "facility = '" + instance.getFacility() + "'";
List results = HibernateUtil.getCurrentSession().createCriteria(ImportFile.class).add(Expression.sql(masterId)).add(Expression.sql(facility)).list();
it works fine!
Any help will be appreciated.