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.  [ 12 posts ] 
Author Message
 Post subject: Add N Times one object
PostPosted: Mon Oct 17, 2005 11:32 am 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
Hello,

I'm having some trouble to add N times one object. Each time i change an attribute (it's a while).

The ID is generated by the database... and i'm calling session.save N times. But i can only insert ONCE.

I'm guessing there is some kind of protection. Can it be overriden ?

Thanks!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:22 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
I'm not sure I understand your post, but if you want to insert N objects, you should construct N objects. You shouldn't re-use the same object and keep passing it to Session.save().

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:26 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
exactly. That's it... i need to insert an object N times. But... each 'time' i change an attribute. It's basically the same object with a property with a different value.

So... i need to instantiate N objects ??... can't i just override that limitation ?

Thank you!!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:30 pm 
Expert
Expert

Joined: Wed Apr 06, 2005 5:03 pm
Posts: 273
Location: Salt Lake City, Utah, USA
You can probably fool Hibernate by setting the id value to null each time, but I'm not sure what other problems that might cause.

_________________
nathan


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:36 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
thanks Nathan. I'll give it a shot :D


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 12:55 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
I've just solved it the cool way... no need of creating N objects with different hashcode.

In hibernateUtil class, i just had to add session.clear() to empty the session.

Code:
   public static void add(Object o){      
      HibernateUtil.beginTransaction();
      Session session = HibernateUtil.getSession();      
      session.save(o);session.clear();
   }   


Thanks :D


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 7:35 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
You might call this the "cool" way, but it looks kind of weird to me. What kind of id's are being assigned to these objects? Are you nulling out the id of the object when you clear it from the session?

Why *not* just create 10,000 different objects and save them all?

Cheers,
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 8:32 pm 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
RobJellinghaus wrote:
Why *not* just create 10,000 different objects and save them all?


the session would cache every one of them


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 17, 2005 10:09 pm 
Expert
Expert

Joined: Thu Jan 08, 2004 6:17 pm
Posts: 278
dennisbyrne wrote:
RobJellinghaus wrote:
Why *not* just create 10,000 different objects and save them all?

the session would cache every one of them

Loading 10,000 objects from the database in a single query is sometimes a problem. But creating and saving them one at a time, perhaps not so much. (If, that is, you can fit them into memory to start with, which you probably can.)

But I understand the issue better now. You might also want to consider using a sequential hi/lo generator (seqhilo) because that might allow Hibernate to batch the inserts more efficiently.

Cheers!
Rob


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 12:21 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
RobJellinghaus wrote:
... sometimes a problem. But creating and saving them one at a time, perhaps not so much. (If, that is, you can fit them into memory to start with, which you probably can.) ... You might also want to consider using a sequential hi/lo generator (seqhilo) because that might


sometimes ... perhaps ... If ... probably ... might

have you considered a career in politcs ;)


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 18, 2005 12:26 am 
Expert
Expert

Joined: Mon Jul 04, 2005 5:19 pm
Posts: 720
mcorleone - take a look at Session.evict when you get a chance. This way you can remove a single obj instead of all of them.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 19, 2005 11:06 pm 
Beginner
Beginner

Joined: Thu Jul 14, 2005 10:11 am
Posts: 32
thanks A LOT for all your replies.

Sorry i'm a day late with the reply.

I dont want to create N objects, because...

i'm writing a system to sell airplane tickets. It's a project for university. Well.. basically, if you've got to register a flight, you need to store N times the entity flight (a flight can be 'done' N times), so you can map the different passengers to different flights.

But... if you've got to create a flight that is done, let's say, every monday, for the next 12 months, and the ONLY difference between them is the date... it doesn't make sense to create 48 objects with a slight difference.

That's why i don't want to create N objects, and instead, change the 'week' in the first object, and add it N times. No need to clone/change attributes. Way more efficient.



Thank you, again :D. Evict is better !


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