-->
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.  [ 20 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Query deos not retrive any objects
PostPosted: Wed Sep 24, 2003 6:18 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
Hi,

I have an object Coompany with an asociation to an object Compnay info.

I want to perform a query in order to retrive objects from the table company which comply with a condition.

For example.

I want to retrive all the objects whos ids are "123".

String var = "123";
Query q= ses.createQuery("FROM Company as comp WHERE comp.id = '"+var+"'");

Somehow this does not retrive any objects, nor does it raise any HibernateException. (And yes, I'm sure there is at least ony compnay in the test set whose id is 123 :-)

------
Moreover the folloing query works
Query q= ses.createQuery("FROM Company as comp");

(Retriveing all the data in the table)

*-------

I am really at a loss, so I'd apreciate any help yopu can ofer.
yours
Mata


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 6:51 pm 
Beginner
Beginner

Joined: Wed Sep 10, 2003 5:34 pm
Posts: 36
Location: New York, NY
Hi Mata,

A couple suggestions, one is to do your queries like this:

Code:
List comps = sess.find("FROM Company as comp WHERE comp.id = ?", 123, Hibernate.LONG);


The second option, and the one I prefer is to use the new Criteria API, which would look like this:

Code:
List comps = sess.createCriteria(Company.class)
    .add( Expression.eq("id", 123)
    .list();


Try either of those and see how it goes.

Matt


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 7:56 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
Matt2000, thanksfor your reply.

My problem has another twist. I have rewriten the persistence so it uses Hibernate. Therefore, clients to mi persisstence should not notice the diference.

Until now, they were passing me SQL queries (Strings) that is why my use of the createQuery.
No parameters, just pack it all in the String.

To change that, will mean a bigger change in the system (which I-ll propose tomorrow to the team).

So, in short, thanks for your answer. And I would apreciate any more inputs.

Cheers
Mata


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 24, 2003 8:17 pm 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
OK, I have just tried your sujestions and The do not throw any exceptions.

But they do not load any objects....<very puzzled>

Guess I-m aiming to the wrong thing!!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 4:32 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Matauy, can you possibly post your Hibernate mapping file for this object/table, as well as the query (your new one). Would help us give you a better answer. :)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 5:36 am 
Newbie

Joined: Thu Sep 25, 2003 5:03 am
Posts: 8
Hi Matauy,

Are you using DAO objects to add the db entries?

We've expereinced the exact same results when we use hibernate to persist objects and then check that the objects exist in the DB.

If we add the objects using SQL directly, they do then exist when we try to query them, its as if Hibernate is using some cache which is out of date,

We haven't found a solution yet so I await a solution to this with bated breath!

Thanks in advance,
Oisin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 7:34 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Depending on your situation make sure you flush the session and specially commit the session so the objects are persisted to the database. You can set the show-sql option to see what hibernate is doing as well as set the loging level using log4j.properties.


Top
 Profile  
 
 Post subject: Re: Query deos not retrive any objects
PostPosted: Thu Sep 25, 2003 7:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
matauy wrote:

I want to retrive all the objects whos ids are "123".

String var = "123";
Query q= ses.createQuery("FROM Company as comp WHERE comp.id = '"+var+"'");

Somehow this does not retrive any objects, nor does it raise any HibernateException. (And yes, I'm sure there is at least ony compnay in the test set whose id is 123 :-)


I am really at a loss, so I'd apreciate any help yopu can ofer.
yours
Mata


Are you ids a string or an long? The query above shows that 123 is a string id. Conversion to an int will not be 123 hence no match. Drop the single quotes around the number and try again. Obviuously, it is better to use prepared statement arguments to avoid these types of mistakes (and you get caching the query benefits).


Last edited by david on Thu Sep 25, 2003 7:41 am, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 7:40 am 
Newbie

Joined: Thu Sep 25, 2003 5:03 am
Posts: 8
The entries are made in the database, Hibernate just doesn't know about them, we call flush after every save / delete, the only problem is that we don't use transactions at all, so we never call commit, could this be a reason?

I have looked at the log4j logs and it looks like everything is doing what it should, I'll try the show-sql option next, maybe this will show what
matauy and myself are doing wrong,

Oisin


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 7:43 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
oisin wrote:
The entries are made in the database, Hibernate just doesn't know about them, we call flush after every save / delete, the only problem is that we don't use transactions at all, so we never call commit, could this be a reason?
Oisin


Hibernate turns auto commit off by default. If you are not running in a CMT environment then use the transaction api.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 7:58 am 
Newbie

Joined: Thu Sep 25, 2003 5:03 am
Posts: 8
Hi David,

we're using MySQL 4.x here, can you confirm that Hibernate will work using transactions with this DB, in a previous project we used Innodb tables in MySQL and could not get JBoss's CMT to work safely with CMP Entity beans, especially under high load.

Also, is it possible to turn the hibernate cache off to test that this is actually the problem,

Finally thanks, this is better support that we'd get from a *big* commercial company, very clear, very concise!

Thanks for your help,
Oisin


Top
 Profile  
 
 Post subject: A Solution......with serious performance costs.....
PostPosted: Thu Sep 25, 2003 9:27 am 
Newbie

Joined: Thu Sep 25, 2003 5:03 am
Posts: 8
First of all, thanks to David and everyone who helped matauy and myself,
I hope this solution works for matauy too, hopefully he'll let us know so we can find a proper solution, rather than a workaround,

Below is the code we use for saving and deleting objects with Hibernate, I've added a call to Session.evict(object obj) to the methods that save and delete objects in Hibernate, this in effect turns caching off in hibernate, and fixes the problems I've been having with objects not being found, it looks as if the Hibernate is somehow out fo date.

Perhaps this is what matauy has been experiencing too?

Is there a solution to this?

We use only one session object so I'm a bit confused why this would happen, a quick look at my debugger shows that the one session is being used all the time, so thats that out of the equation,

Here's the methods we use for save and deletes...note that we stilldon;t use transactions, I assume this is ok, as we have one vm making db calls and one db which is not updated by anything other than hibernate!

Thanks again, especially to david...

Oisin




/**
* Save the given domain object to the database.
*
* @param obj
* @throws DAOException
*/
protected synchronized void save(Object obj) throws DAOException {
Session ses = null;

try {
ses = ThreadLocalSession.currentSession();
ses.saveOrUpdate(obj);
ses.flush();
ses.evict(obj); //added this line to stop caching
} catch (Exception e) {
try {
ses.connection().rollback();
} catch (Exception ex) {
e.printStackTrace();
};
throw new DAOException(e);
}
}

protected synchronized void delete(Object obj) throws DAOException {
Session ses = null;

try {

ses = ThreadLocalSession.currentSession();

if (ses == null) {
throw new DAOException("No Session.");
}
ses.delete(obj);
ses.flush();
ses.evict(obj);//added this line to stop caching too

} catch (Exception e) {

throw new DAOException(e);

}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 9:43 am 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
Hi all,

Seems all the fun happend while I was sleeping. I just got back to the office and read all yopur posts.

So I'll answer here.
1) My id's are String.

2) My mapping files are:
//Compnay
<hibernate-mapping>
<class name="domainlayer.companies.Company" table="companies">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern. -->
<id name="oid" type="string" unsaved-value="null" >
<column name="OID" sql-type="char(32)" not-null="true"/>
<generator class="uuid.hex"/>
</id>
<!-- A cat has to have a name, but it shouldn' be too long. -->
<property name="id">
<column name="ID" sql-type="varchar(32)" not-null="true" unique="true"/>
</property>
<property name="name">
<column name="NAME" sql-type="varchar(32)" not-null="true" unique="true"/>
</property>
<property name="email">
<column name="EMAIL" sql-type="varchar(32)" not-null="true"/>
</property>
<property name="password">
<column name="PASSWORD" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="username">
<column name="USERNAME" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="contactPerson">
<column name="CONTACTPERSON" sql-type="varchar(16)" not-null="true"/>
</property>
<property name="branch">
<column name="BRANCH" sql-type="varchar(32)" not-null="true"/>
</property>
<many-to-one name="companyInfo" column="COMPINFO_OID"
class="domainlayer.companies.CompanyInfo"
cascade="all" not-null="false" unique="false" outer-join="auto"
update="true" insert="true" />
</class>
</hibernate-mapping>

//CompnayInfo
<hibernate-mapping>
<class name="domainlayer.companies.CompanyInfo" table="companyInfo">
<!-- A 32 hex character is our surrogate key. It's automatically
generated by Hibernate with the UUID pattern.
-->
<id name="oid" type="string" unsaved-value="null">
<column name="OID" sql-type="char(32)" not-null="true" />
<generator class="uuid.hex" />
</id>
<!-- Mapping the rest of the properties -->
<property name="address" unique="false"
update="true" insert="true">
<column name="ADDRESS" sql-type="varchar(32)" />
</property>
<property name="country" unique="false"
update="true" insert="true">
<column name="COUNTRY" sql-type="varchar(32)" />
</property>
<property name="city" unique="false"
update="true" insert="true">
<column name="CITY" sql-type="varchar(32)" />
</property>
<property name="fax" unique="false" update="true"
insert="true">
<column name="FAX" sql-type="varchar(32)" not-null="true" />
</property>
<property name="telephone" unique="false"
update="true" insert="true">
<column name="TELEPHONE" sql-type="varchar(16)"/>
</property>
<property name="homePage" unique="false"
update="true" insert="true">
<column name="HOMEPAGE" sql-type="varchar(255)"/>
</property>
<property name="description" unique="false"
update="true" insert="true">
<column name="DESCRIPTION" sql-type="varchar(255)"/>
</property>
</class>
</hibernate-mapping>

----------

I'm going to try now to read carefully what you guys have sugested.
I also hope it works for me as it did to Oisin.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 9:49 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Hibernate does go to quite some effort to make sure caching is stable. Here is a few suggestions:
1) Don't use the session object for long periods of time. It is a light weight object thus use a new Session for each UnitOfWork.
2) In the majority of cases (depends on system and architecture) the UnitOfWork is wrapped by a Transaction.
3) Should use Transactions - not sure why you don't.
4) There are cases where evicting the object from the session cache is necessary but your use case is not one of them (once you change to Short Sessions).

Here is an outline using a JDBC connection (cut down and rough):

.... Start Service Method
void doServiceRequest() {
Session s = sessions.openSession();
ThreadLocalSession.setSession(s);

doOperationDAO();

s.close();
ThreadLocalSession.setSession(null);
}

void doOperationDAO() {
Session s = null;
Transaction tx = null;
try {
s = ThreadLocalSession.currentSession();
tx = s.beginTransaction();

fooList = s.find(
"select foo from eg.Foo foo where foo.Date = current date"
// uses db2 date function
);
bar = (Bar) s.create(Bar.class);

tx.commit();
}
catch (Exception e) {
if (tx!=null) tx.rollback();
s.close();
throw e;
}
}


Now the Transaction should be higher than this one DAO so you can chain DAOs together but this will give you an idea.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 25, 2003 10:26 am 
Beginner
Beginner

Joined: Thu Sep 04, 2003 11:02 am
Posts: 20
Location: Montevideo, Uruguay
OK,

I'm about to loose my faith in Hibernate <just kidding>

1) I am useing transaction
2) And I have made some changes in the code, and hace tried with
session.find
ses.find("Select comp FROM domainlayer.companies.Company as comp WHERE "+qid);
where qid is a String '123'

I have also tried with
String var = "123";
compCol=ses.find("FROM Company as comp WHERE comp.id = ?",var, Hibernate.STRING);
and

compCol=ses.createCriteria(Company.class)
.add( Expression.eq("id",var))
.list();

------------

I always get the same result. So I guess there must be sth wrong elsewere (the mapping files


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 20 posts ]  Go to page 1, 2  Next

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.