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.  [ 24 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: check for exists
PostPosted: Fri Dec 03, 2004 10:32 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Hello,
I want check existance new created bean (detach) in database
Is there easy way ?

regards


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 11:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
You have to hit the database to know for sure (for all user cases to be covered) so session.get() will provide the answer. If you use case could be narrowed then maybe other options exist.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 03, 2004 11:54 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
it is fine that I have database access, but is there any function that do it, special with composite key or I have to create query

thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 12:58 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
What kind of question is that? You ask if you need to query the database if you need to find out if something exists in the database? Of course. This is covered in the FAQs, by the way.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 7:26 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
I don't ask If I need query database; I know that I have to hit database.My question is :
Is there function like load that I check existance new created object in database like this :

Cat cat = new Cat();
cat.setXxx(yyy);

boolean exists = session.isExists(cat); ???

I want check existance without saving

I can create query and do session.find, but is there better way

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 9:02 am 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Did you read the Javadoc API for the Session before posting this? Why not?


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 10:26 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Christian,
I read Session API, but You don't understand me.

Scenario :

User populate properties of persistent class (and identifier too; yes, identifier have business meaning and can be composite id; idenitifier is assigned)

I haven't session, yet - object is detached

When user populate object properties I want check existence object with this identifier
I can create query and call session.find, but is there easier check ?

I try this :
Code:
try {
     identifier = sessionFactory.getClassMetadata.getIdentifier(object);
            if (identifier == null)
                return null;
            session = sessionFactory.openSession();
            return session.get(object.getClass(), identifier);
} catch (Exception e) {
return null;
}
finaly {
  if (session!=null) {
     try { session.close(); } catch (Exception e) {}
}


It work for simple id, but don't work for composite id with collection

regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 3:24 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
With app assigned IDs, I think you're out of luck. You can ask whether an instance is contained in a Hibernate session, but if it isn't, how is Hibernate going to know whether or not it's in the database without querying for it? Perhaps you need a property in your object that indicates whether or not the object has been saved. You can intercept the CRUD operations to set the property.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 4:01 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Rhasselbaum,
it is fine that I query database;I WANT QUERY database, but is there simple way for check
existence new created object in database ?

regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 4:08 pm 
Beginner
Beginner

Joined: Wed Nov 19, 2003 6:46 pm
Posts: 41
Location: Auckland, New Zealand
If I interpret you correctly, you want to check if any other object exists in the database with the same composite key as the one you're about to save - probably to check for uniqueness, etc. and to avoid the DB throwing an exception if you try to insert a duplicate row.

This is exactly what the find is for.

Good luck :-)

Craig


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 4:31 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
cds,
Yes, I want it almost
I make swing application with spring RCP binding and validation framework
I want that when user type any character in primary key/iidentifier and foreign key he have signal that identifier exists (error) or foreign key don't exists (error too) - it is too many hit to database (I know), but I want make user friendly interface - not web application

regards


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 4:48 pm 
Regular
Regular

Joined: Thu Oct 07, 2004 4:45 pm
Posts: 92
Well, you can either hit the database with a query every time the user types a character (yikes!) or load all of the relevant objects into memory and do the search in code. Neither sounds very attractive to me, but...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Dec 04, 2004 6:29 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Quote:
Well, you can either hit the database with a query every time the user types a character (yikes!) or load all of the relevant objects into memory and do the search in code. Neither sounds very attractive to me, but...


load all of the object in memory isn't possible - it is too big.I make user choose that check only for any key (ctrl SPACE) and LOV (table with list) for foreign key.

what I can make query for object existance ?

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 05, 2004 7:33 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
You can try to use some hash function to save memory.
If entered data hash does not exist then OK
else search database, if exists then erorr else OK. As 'better' hash function as less probability to hit database. You can use "short" for hash code ( or hashCode % someConstant ), array of shorts will waste about 2^17 bytes, but bit set data structure must help, you will have less codes than possible to generate.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 05, 2004 11:08 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Baliukas
Thanks for answer, but perforamance isn't important for me now - I want simple method or
query for checking existance object - It doesn't exists in hibernate, seem and I have to create
query

regards


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 24 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.