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>