-->
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.  [ 7 posts ] 
Author Message
 Post subject: Seeking advice for large insert
PostPosted: Wed Oct 19, 2005 8:54 am 
Just getting started with using Hibernate, and I have a question. I am going to receive a large spread sheet of information that I would like to insert into a table.

The data will be very simple and have something like this:

name attribute1 attribute2

I understand how to persist the data to the database, but upon further thinking, I need to ensure that I do not insert the same name more than once.

I am seeking some advice, as to the best way for me to begin this simple task.

My thought was to grab a record, try and see if it exists in the table, if not save it, and just keep going. Or when using Hibernate, is there a more robust, or succinct solution.


Thanks,


Top
  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 11:38 am 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
set unique index in database for name field


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 1:53 pm 
Newbie

Joined: Sun May 22, 2005 6:09 am
Posts: 10
snpesnpe wrote:
set unique index in database for name field


I agree, let your DB do the heavy lifting for you - create a unique constraint (which creates an index/unique index in most DBs) on 'name'.

But you'll have to watch out for inserts though... remember that hibernate does not immediately execute SQL calls on a session.save() -> you'll need to do session.flush() to have it do that. At that point you can get constraint violated exceptions, so don't forget to catch those and continue.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 3:12 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
spurcell , how many records are we talking about?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 3:46 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
for this way only, I think that is better use pure jdbc - jdbc save sql command direct and user can catch exception (without commit after every insert)


Top
 Profile  
 
 Post subject: Consider session.replicate()
PostPosted: Wed Oct 19, 2005 6:32 pm 
Newbie

Joined: Wed Sep 17, 2003 3:31 am
Posts: 10
Location: Napoli (IT)
Consider using session.replicate().

This method will check for you if an instance with the same id exists in the database. If so, an update() will take place, otherwise it will perform a save() (insert).

Other comments about how many records are you actually going to process are relevant. However, if they are really much, you can clear the session every, say, 1000 records in order to avoid OutOfMemory.

With batch update enabled and replicate() strategy I can't see how a raw JDBC solution could perform better than Hibernate.

_________________
--- stefano
Don't forget to rate.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 9:55 pm 
Expert
Expert

Joined: Sat Jun 12, 2004 4:49 pm
Posts: 915
Quote:
With batch update enabled and replicate() strategy I can't see how a raw JDBC solution could perform better than Hibernate.


I don't think that jdbc will be quicker (this case is trivial and it isn't important, but I can do quicker with jdbc with insert direct to srollable resultset - hibernate can't do direct insert/update/delete to scrollable resultset, for now) .
I think that I do quick job with jdbc - no POJO, no mapping and if I do it once then I will use JDBC


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