This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Object Relational Mapping and the fly likes Transaction comit problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Transaction comit problem" Watch "Transaction comit problem" New topic
Author

Transaction comit problem

Gaurav Chawla
Greenhorn

Joined: Jun 23, 2010
Posts: 1
Hi,
I'm new to hibernate.
Implementing many to many relationship for employee to benefits, following is the code for implementing class.

package many2many;

import org.hibernate.*;
import org.hibernate.cfg.*;
import java.util.*;
//import java.io.*;

public class EmpBenefit {

/**
* @param args
*/
public static void main(String[] args) {

try
{
SessionFactory sesfry = new Configuration().configure().buildSessionFactory();
Session session = sesfry.openSession();
Transaction tx = session.beginTransaction();

Employee emp1 = new Employee();
Employee emp2 = new Employee();

emp1.setName("Ram");
HashSet<Benefit> b1 = new HashSet<Benefit>();
b1.add(new Benefit(50));
b1.add(new Benefit(40));
emp1.setBenefits(b1);
session.save(emp1);


emp2.setName("Shyam");
HashSet<Benefit> b2 = new HashSet<Benefit>();
b2.add(new Benefit(150));
b2.add(new Benefit(140));
emp1.setBenefits(b2);
System.out.println("Step 1");
session.save(emp2);

System.out.println("Step 2");

System.out.println("Step 2");

System.out.println(" "+tx.isActive());
System.out.println("Step 3"+tx.toString());
tx.commit();
System.out.println(" "+tx.isActive());
System.out.println("Step 3");
session.flush(); // problem
session.close();
System.out.println("Step 4");

}catch(Exception e)
{
e.printStackTrace();
}

}


hibernate mapping xml file is:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>

<class name="many2many.Employee" table="Employee_dtl" >
<id name="e_id" column="E_id">
<generator class="hilo"/>
</id>
<property column="Name" name="name" type="string"/>
<set name="benefits" table="employee_benefit" cascade="all">
<key column="parent_id"/>
<many-to-many column="benefit_id" class="many2many.Benefit"/>
</set>

</class>

<class name="many2many.Benefit" table="Benefit4emp">

<id name="b_id" column="B_id">
<generator class="hilo"/>
</id>

<property name="cost" column="Cost" type="int"/>
</class>

</hibernate-mapping>

}

ERROR :

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Step 1
Step 2
true
Step 3org.hibernate.transaction.JDBCTransaction@adb1d4
Hibernate: insert into Employee_dtl (Name, E_id) values (?, ?)
Hibernate: insert into Employee_dtl (Name, E_id) values (?, ?)
Hibernate: update Benefit4emp set Cost=? where B_id=?
Hibernate: update Benefit4emp set Cost=? where B_id=?
Hibernate: update Benefit4emp set Cost=? where B_id=?
Hibernate: update Benefit4emp set Cost=? where B_id=?
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1007)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:354)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at many2many.EmpBenefit.main(EmpBenefit.java:44)

Kindly suggest the possible errror....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Transaction comit problem