-->
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.  [ 2 posts ] 
Author Message
 Post subject: Saving a generated in memory pair list
PostPosted: Wed Jul 02, 2008 2:02 pm 
Newbie

Joined: Wed Jul 02, 2008 1:45 pm
Posts: 3
I have a simple entity object:

@Entity
@Table
public class Equipment {
@Id @GeneratedValue
@Column(name = "EQUIPMENT_ID")
private int equipmentId

@Column
private String name
}

The way I am going about this is I am storing a list in memory of these objects (incrementing the id every time one is added). At the end I save to the database. This works fine pre hibernate.

I am now refactoring my code to use hibernate. I have the loads for other objects working fine. I and am cycling through the equipment list doing a session.save(equipment). Looking at the log this gives:
Hibernate: insert into Equipment (name) values (?)
and nothing gets saved. I thought it might be because the
@GeneratedValue have been generated by me but when I removed @GeneratedValue there was no change.

My requirements are:
- I need to built up equipment list in memory and not save to database until the end
- I need to have an id generated in memory (prior to saving) as my list will get accessed literally 100's of thousands of times.

Is there a smarter way to go about this?

Thanks, Tom.

Hibernate version:
hibernate-3.2.6.ga / hibernate-annotations-3.3.1.GA

Mapping documents:
done in hibernate.cfg.xml
<mapping class="mypackage.Equipment"/>

Code between sessionFactory.openSession() and session.close():
equipmentList.each { equipment ->
session.save(equipment)
}

Full stack trace of any exception that occurs:
No exception

Name and version of the database you are using:
MySql 5.0

The generated SQL (show_sql=true):
Hibernate: insert into Equipment (name) values (?)


Debug level Hibernate log excerpt:

<<Inserting entity: mypackage.Equipment (native id)>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractEntityPersister.java:2149)
<<about to open PreparedStatement (open PreparedStatements: 0, globally: 0)>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractBatcher.java:366)
<<insert into Equipment (name) values (?)>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractBatcher.java:401)
<<preparing statement>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractBatcher.java:484)
<<Dehydrating entity: [mypackage.Equipment#<null>]>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractEntityPersister.java:1997)
<<binding '"my equipment"' to parameter: 1>> Jul 3, 2008 12:03:11 PM DEBUG [main] (NullableType.java:133)
<<Natively generated identity: 409>> Jul 3, 2008 12:03:11 PM DEBUG [main] (IdentifierGeneratorFactory.java:72)
<<about to close PreparedStatement (open PreparedStatements: 1, globally: 1)>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractBatcher.java:374)
<<closing statement>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractBatcher.java:533)
<<allowing proxied method [beginTransaction] to proceed to real session>> Jul 3, 2008 12:03:11 PM DEBUG [main] (ThreadLocalSessionContext.java:300)
<<allowing proxied method [save] to proceed to real session>> Jul 3, 2008 12:03:11 PM DEBUG [main] (ThreadLocalSessionContext.java:300)
<<saving transient instance>> Jul 3, 2008 12:03:11 PM DEBUG [main] (DefaultSaveOrUpdateEventListener.java:158)
<<saving [mypackage.Equipment#<null>]>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractSaveEventListener.java:153)
<<executing insertions>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractSaveEventListener.java:244)
<<executing identity-insert immediately>> Jul 3, 2008 12:03:11 PM DEBUG [main] (AbstractSaveEventListener.java:297)


Top
 Profile  
 
 Post subject: Groovy SSCCE
PostPosted: Thu Jul 03, 2008 11:06 am 
Newbie

Joined: Wed Jul 02, 2008 1:45 pm
Posts: 3
A simple groovy SSCCE demonstrates my lack of understanding with this issue:

Using the Equipment entity above:

Code:
       
       List equipmentList = [
           new Equipment(equipmentId:1, name:'one'),                 
           new Equipment(equipmentId:2, name:'two')                 
       ]

          sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
       Session session = sessionFactory.getCurrentSession();
session.beginTransaction()

   equipmentList.each { equipment ->
        session.save(equipment)
   }

        session.getTransaction().commit()


As reported I get:

Hibernate: insert into Equipment (name) values (?)
Hibernate: insert into Equipment (name) values (?)

but nothing is written to the database. If I remove the @GeneratedValue nothing gets saved but I don't get the above inserts.

I would really appreciate someone pointing this Hibernate rookie in the right direction.

Tom.


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