These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Query by example does not read not null object
PostPosted: Tue Mar 07, 2006 1:37 pm 
Newbie

Joined: Thu Oct 13, 2005 11:43 am
Posts: 11
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.


Top
 Profile  
 
 Post subject: Re: Query by example does not read not null object
PostPosted: Tue Mar 07, 2006 4:58 pm 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
You can setup ExampleQuerys even upon associated objects.
Try this:

Code:
Session session = HibernateUtil.getCurrentSession();
Criteria criteria = session.createCriteria(ImportFile.class);
criteria.add(Example.create(instance));
criteria.add(createCriteria("master")
         .add( Example.create(instance.getMaster()) );
List retVal = criteria.list();



simon


Top
 Profile  
 
 Post subject: Re: Query by example does not read not null object
PostPosted: Wed Mar 08, 2006 3:46 am 
Newbie

Joined: Thu Oct 13, 2005 11:43 am
Posts: 11
Simon,
thanks for your hint but I couldn't get it to work.

[quote="simonwillnauer"]You can setup ExampleQuerys even upon associated objects.
Try this:

Code:
Session session = HibernateUtil.getCurrentSession();
Criteria criteria = session.createCriteria(ImportFile.class);
criteria.add(Example.create(instance));
criteria.add(createCriteria("master")
         .add( Example.create(instance.getMaster()) );
List retVal = criteria.list();


There is no method "createCriteria("master")". I guess you meant session.createCriteria(Master.class).add( ..."?

The problem is also that the method criteria.add() only takes a Criterion and not a Criteria as argument. Could you explain further please?

Until now I was using the generated DAO classes from hibernate.
In my example the method ImportFile.findbyExample() did not work (using only the example instance and having no knowledge about any associated objects).

So I would like to know if this is a general problem and I can never do something like
Code:
List results = HibernateUtil.getCurrentSession().createCriteria(ImportFile.class).add(Example.create(instance)).list();
... to get the right query generated (including all associated objects) or if there is a problem with my mappings or code accessing ImportFile.findbyExample().

Chris


Top
 Profile  
 
 Post subject: Re: Query by example does not read not null object
PostPosted: Wed Mar 08, 2006 8:08 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
:) i posted the wrong code.... (I'm getting code-blind after 10h work) sorry about that.

Code:
List results = session.createCriteria(ImportFile.class)
     .add( Example.create(instance))
     .createCriteria("master")
      .add( Example.create( instance.getMaster() ) )
     .list();


that should do the job...

simon


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.