Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Transaction comit problem

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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....
 
You guys wanna see my fabulous new place? Or do you wanna look at this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic