I have 2 tables
Xydataa and Fm4featuress
Mapping
Xydataa.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Xydataa" table="xydataa" catalog="navl">
<id name="id" type="java.lang.Integer">
<column name="ID" />
<generator class="identity" />
</id>
<property name="longi" type="string">
<column name="Longi" />
</property>
<property name="lat" type="string">
<column name="Lat" />
</property>
<property name="altitude" type="int">
<column name="Altitude" not-null="true" />
</property>
<property name="angle" type="int">
<column name="Angle" not-null="true" />
</property>
<property name="satellite" type="int">
<column name="Satellite" not-null="true" />
</property>
<property name="speed" type="int">
<column name="Speed" not-null="true" />
</property>
<property name="objectId" type="int">
<column name="ObjectId" not-null="true" />
</property>
<property name="clientId" type="int">
<column name="ClientId" not-null="true" />
</property>
<property name="gpsTime" type="string">
<column name="GpsTime" length="19" not-null="true" />
</property>
<property name="location" type="string">
<column name="Location" />
</property>
<property name="city" type="string">
<column name="City" />
</property>
<property name="state" type="string">
<column name="State" />
</property>
<property name="distance" type="string">
<column name="Distance" length="100" />
</property>
<property name="times" type="timestamp">
<column name="Times" length="19" not-null="false" />
</property>
<set name="fm4featuresses" cascade="all" inverse="true" lazy="true">
<key>
<column name="ID" not-null="true"/>
</key>
<one-to-many class="pip.Fm4featuress" />
</set>
</class>
</hibernate-mapping>
and
Fm4featuress.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated Apr 2, 2010 3:36:14 PM by Hibernate Tools 3.2.1.GA -->
<hibernate-mapping>
<class name="pip.Fm4featuress" table="fm4featuress" catalog="navl">
<id name="id" type="int" unsaved-value="null">
<column name="ID" />
<generator class="assigned" />
</id>
<many-to-one name="xydataa" class="pip.Xydataa" update="false" insert="false" fetch="select">
<column name="ID" not-null="true"/>
</many-to-one>
<property name="dataId" type="java.lang.Integer">
<column name="DataId" not-null="true"/>
</property>
<property name="value" type="java.lang.Integer">
<column name="Value" not-null="true"/>
</property>
</class>
</hibernate-mapping>
Pojo class
Code:
package pip;
// Generated Apr 2, 2010 3:36:13 PM by Hibernate Tools 3.2.1.GA
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
/**
* Xydataa generated by hbm2java
*/
public class Xydataa implements java.io.Serializable {
private Integer id;
private String longi;
private String lat;
private int altitude;
private int angle;
private int satellite;
private int speed;
private int objectId;
private int clientId;
private String gpsTime;
private String location;
private String city;
private String state;
private String distance;
private Timestamp times;
private Set fm4featuresses = new HashSet();
protected static Session sess = null;
public Xydataa() {
}
public Xydataa(int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, Timestamp times) {
this.altitude = altitude;
this.angle = angle;
this.satellite = satellite;
this.speed = speed;
this.objectId = objectId;
this.clientId = clientId;
this.gpsTime = gpsTime;
this.times = times;
}
public Xydataa(String longi, String lat, int altitude, int angle, int satellite, int speed, int objectId, int clientId, String gpsTime, String location, String city, String state, String distance, Timestamp times, Set fm4featuresses) {
this.longi = longi;
this.lat = lat;
this.altitude = altitude;
this.angle = angle;
this.satellite = satellite;
this.speed = speed;
this.objectId = objectId;
this.clientId = clientId;
this.gpsTime = gpsTime;
this.location = location;
this.city = city;
this.state = state;
this.distance = distance;
this.times = times;
this.fm4featuresses = fm4featuresses;
}
public Integer getId() {
return this.id;
}
public void setId(Integer id) {
this.id = id;
}
public String getLongi() {
return this.longi;
}
public void setLongi(String longi) {
this.longi = longi;
}
public String getLat() {
return this.lat;
}
public void setLat(String lat) {
this.lat = lat;
}
public int getAltitude() {
return this.altitude;
}
public void setAltitude(int altitude) {
this.altitude = altitude;
}
public int getAngle() {
return this.angle;
}
public void setAngle(int angle) {
this.angle = angle;
}
public int getSatellite() {
return this.satellite;
}
public void setSatellite(int satellite) {
this.satellite = satellite;
}
public int getSpeed() {
return this.speed;
}
public void setSpeed(int speed) {
this.speed = speed;
}
public int getObjectId() {
return this.objectId;
}
public void setObjectId(int objectId) {
this.objectId = objectId;
}
public int getClientId() {
return this.clientId;
}
public void setClientId(int clientId) {
this.clientId = clientId;
}
public String getGpsTime() {
return this.gpsTime;
}
public void setGpsTime(String gpsTime) {
this.gpsTime = gpsTime;
}
public String getLocation() {
return this.location;
}
public void setLocation(String location) {
this.location = location;
}
public String getCity() {
return this.city;
}
public void setCity(String city) {
this.city = city;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getDistance() {
return this.distance;
}
public void setDistance(String distance) {
this.distance = distance;
}
public Timestamp getTimes() {
return this.times;
}
public void setTimes(Timestamp times) {
this.times = times;
}
public Set getFm4featuresses() {
return this.fm4featuresses;
}
public void addFm4featuresses(Fm4featuress fm4featuress) {
this.fm4featuresses.add(fm4featuress);
}
public void setFm4featuresses(Set fm4featuresses) {
this.fm4featuresses = fm4featuresses;
}
public void save() throws Exception{
sess.save(this);
sess.getTransaction().commit();
}
}
and
Code:
package pip;
// Generated Apr 2, 2010 3:36:13 PM by Hibernate Tools 3.2.1.GA
import org.hibernate.Session;
/**
* Fm4featuress generated by hbm2java
*/
public class Fm4featuress implements java.io.Serializable {
private int id;
private Xydataa xydataa;
private Integer dataId;
private Integer value;
private Session sess=null;
public Fm4featuress() {
}
public Fm4featuress(int id, Xydataa xydataa) {
this.id = id;
this.xydataa = xydataa;
}
public Fm4featuress(int id, Xydataa xydataa, Integer dataId, Integer value) {
this.id = id;
this.xydataa = xydataa;
this.dataId = dataId;
this.value = value;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Xydataa getXydataa() {
return this.xydataa;
}
public void setXydataa(Xydataa xydataa) {
this.xydataa = xydataa;
}
public Integer getDataId() {
return this.dataId;
}
public void setDataId(Integer dataId) {
this.dataId = dataId;
}
public Integer getValue() {
return this.value;
}
public void setValue(Integer value) {
this.value = value;
}
public void save() throws Exception{
sess.save(this);
sess.getTransaction().commit();
}
}
In between opensession and closesession
Code:
sess = HibernateUtil.getSessionFactory().openSession();
Date resultdate = new Date(times);
xx = new DecimalNumber(x,7);
yy = new DecimalNumber(y,7);
System.out.println("Inserting xydataa");
Xydataa xy=new Xydataa();
Set set = new HashSet();
xy.setLongi(xx+"");
xy.setLat(yy+"");
xy.setAltitude(altitude);
xy.setAngle(angle);
xy.setSatellite(satellites);
xy.setSpeed(speed);
xy.setObjectId(objectId);
xy.setClientId(clientId);
xy.setGpsTime(sdf.format(resultdate));
xy.setLocation(location);
xy.setCity(city);
xy.setState(state);
xy.setDistance(distance);
System.out.println("Done");
Fm4featuress fm = new Fm4featuress();
for (int i = 0; i < obj.length && obj[i] != 0; i++)
{
int all = obj[i] - 1;
System.out.println("Inserting Fm4Featuress");
fm.setDataId(all);
fm.setValue(obja[i]);
fm.setXydataa(xy);
// fm.setXydataa(xy);
// xy.getFm4featuresses().add(fm);
// xy.addFm4featuresses(fm);
}
set.add(fm);
xy.setFm4featuresses(set);
Transaction transaction = null;
try {
transaction = sess.beginTransaction();
sess.save(xy);
sess.save(fm);
transaction.commit();
System.out.println("Transaction commited");
} catch (Exception e) {
System.out.println("Exception Here");
if (transaction != null) {
transaction.rollback();
throw e;
}
} finally {
if (sess!=null)
{
sess.close();
}
}
I got the following error
the stack Trace
INFO: Inserting xydataa
INFO: Done
INFO: Inserting Fm4Featuress
INFO: Inserting Fm4Featuress
INFO: Hibernate: insert into navl.xydataa (Longi, Lat, Altitude, Angle, Satellite, Speed, ObjectId, ClientId, GpsTime, Location, City, State, Distance, Times) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
INFO: Hibernate: update navl.fm4featuress set DataId=?, Value=? where ID=?
SEVERE: Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at tcplistener.Alldatabase.Database(Alldatabase.java:173)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
SEVERE: Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at tcplistener.Alldatabase.Database(Alldatabase.java:173)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
INFO: Exception Here
SEVERE: null
org.hibernate.StaleStateException: Batch update returned unexpected row count from update [0]; actual row count: 0; expected: 1
at org.hibernate.jdbc.Expectations$BasicExpectation.checkBatched(Expectations.java:61)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:46)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:68)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:142)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at tcplistener.Alldatabase.Database(Alldatabase.java:173)
at tcplistener.ModuleHandler.run(ModuleHandler.java:155)
at java.lang.Thread.run(Thread.java:619)
What i am doing wrong
Please reply me