-->
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.  [ 5 posts ] 
Author Message
 Post subject: How to store serveral objects?
PostPosted: Fri Feb 03, 2006 7:42 am 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
Hi! I need some help here. I want to store in the DB several objects of type Operator. So i did a cicle 'for' where it opens a transaction and at the end makes the commit. My problem is that it only inserts the 1st object. When it trys to commit the second says that the transaction was not successfully started. What I'm I missing here?

The code looks like this:

protected void createAndStoreOperator(ConfigurationDataDocument rootDoc) {

Session session = HibernateUtil.getSessionFactory().getCurrentSession();
Operator myOperator = new Operator();
TypeOperator[] operator = rootDoc.getConfigurationData().getOperatorArray();

for (int i=0; i < operator.length; ++i){
try{
session.beginTransaction();
System.out.println("\n\n=========Operator Contents==========\n");
myOperator.setOperatorId(operator[i].getId());
System.out.println("Operator ID: " + operator[i].getId());
myOperator.setName(operator[i].getName());
System.out.println("Operator Name: " + operator[i].getName());
myOperator.setPassword(operator[i].getPassword());
System.out.println("Operator Password: " + operator[i].getPassword());
System.out.println("\n### Going to save now...");
session.saveOrUpdate(myOperator);
session.getTransaction().commit();

}
catch (Exception ex) {
session.getTransaction().rollback();
System.out.println("Could not insert operator node: " + ex);
}
}
}


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 9:34 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
Sometime the session is closed after the first commit.
Depends on Hibernate version and hibernate.cfg.xml.
Can post both?

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 10:21 am 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
steckemetz wrote:
Sometime the session is closed after the first commit.
Depends on Hibernate version and hibernate.cfg.xml.
Can post both?


Well i managed to do it. I create now a new session inside the cicle. You're right, the session is closed after the first commit. So is this the best way to store more than an instance of the same object in a DB?

protected void createAndStoreOperator(ConfigurationDataDocument rootDoc) {

Operator myOperator = new Operator();
TypeOperator[] operator = rootDoc.getConfigurationData().getOperatorArray();

for (int i=0; i < operator.length; ++i){
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
try{
session.beginTransaction();
System.out.println("\n\n=========Operator Contents==========\n");
myOperator.setOperatorId(operator[i].getId());
System.out.println("Operator ID: " + operator[i].getId());
myOperator.setName(operator[i].getName());
System.out.println("Operator Name: " + operator[i].getName());
myOperator.setPassword(operator[i].getPassword());
System.out.println("Operator Password: " + operator[i].getPassword());
System.out.println("\n### Going to save now...");
session.saveOrUpdate(myOperator);
session.getTransaction().commit();
}
catch (Exception ex) {
session.getTransaction().rollback();
System.out.println("Could not insert operator node: " + ex);
}
}
}


hibernate.cfg.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost/kobas_maintenance</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">ifsdsr</property>

<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>

<!-- SQL dialect -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>

<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>

<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>

<property name="hibernate.cglib.use_reflection_optimizer">false</property>

<!-- Drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">update</property>


<!-- mapping files -->
<mapping resource="maintenance/Alarmnotification.hbm.xml"/>
<mapping resource="maintenance/Ambience.hbm.xml"/>
<mapping resource="maintenance/Ambiencemparameter.hbm.xml"/>
<mapping resource="maintenance/Componentreason.hbm.xml"/>
<mapping resource="maintenance/Componentreplacement.hbm.xml"/>
<mapping resource="maintenance/Componenttoreplace.hbm.xml"/>
<mapping resource="maintenance/Ltpredictionvalue.hbm.xml"/>
<mapping resource="maintenance/Maintenanceobject.hbm.xml"/>
<mapping resource="maintenance/Maintenancereason.hbm.xml"/>
<mapping resource="maintenance/Maintenancetrainingmodule.hbm.xml"/>
<mapping resource="maintenance/Module.hbm.xml"/>
<mapping resource="maintenance/Monitorizationparameter.hbm.xml"/>
<mapping resource="maintenance/Mreasongroup.hbm.xml"/>
<mapping resource="maintenance/Mwo.hbm.xml"/>
<mapping resource="maintenance/Mwoobject.hbm.xml"/>
<mapping resource="maintenance/Mwooperator.hbm.xml"/>
<mapping resource="maintenance/Mworeason.hbm.xml"/>
<mapping resource="maintenance/Mwosparepart.hbm.xml"/>
<mapping resource="maintenance/Mwotool.hbm.xml"/>
<mapping resource="maintenance/Ocorrencyvalue.hbm.xml"/>
<mapping resource="maintenance/Operator.hbm.xml"/>
<mapping resource="maintenance/Process.hbm.xml"/>
<mapping resource="maintenance/Processmparameter.hbm.xml"/>
<mapping resource="maintenance/Product.hbm.xml"/>
<mapping resource="maintenance/Productmparameter.hbm.xml"/>
<mapping resource="maintenance/Reasongroup.hbm.xml"/>
<mapping resource="maintenance/Reasonmparameter.hbm.xml"/>
<mapping resource="maintenance/Sensor.hbm.xml"/>
<mapping resource="maintenance/Sparepart.hbm.xml"/>
<mapping resource="maintenance/Sparepartreason.hbm.xml"/>
<mapping resource="maintenance/Tool.hbm.xml"/>
<mapping resource="maintenance/Toolreason.hbm.xml"/>
<mapping resource="maintenance/Warning.hbm.xml"/>

</session-factory>
</hibernate-configuration>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 11:18 am 
Pro
Pro

Joined: Mon Jan 24, 2005 5:39 am
Posts: 216
Location: Germany
There are certainly other options:

1. You do not need to commit after each save.
One commit may be enough after the for loop.

2. You can set

Code:
<property name="hibernate.connection.release_mode">
   on_close
</property>
<!-- <property name="hibernate.current_session_context_class">thread</property>-->
<!--              <property name="hibernate.transaction.auto_close_session">false</property>-->


Then the session and connection stays alive after commit.

This certainly depends on what you want to do.

_________________
dont forget to rate !


Top
 Profile  
 
 Post subject:
PostPosted: Fri Feb 03, 2006 2:10 pm 
Newbie

Joined: Tue Dec 27, 2005 2:55 pm
Posts: 18
steckemetz wrote:
There are certainly other options:

1. You do not need to commit after each save.
One commit may be enough after the for loop.

2. You can set

Code:
<property name="hibernate.connection.release_mode">
   on_close
</property>
<!-- <property name="hibernate.current_session_context_class">thread</property>-->
<!--              <property name="hibernate.transaction.auto_close_session">false</property>-->


Then the session and connection stays alive after commit.

This certainly depends on what you want to do.


Alright!! Thanks for the tip, I'll try!!

[[]]


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