• 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

StaleStateException Hibernate

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

I am trying to implement a many-to-many association in hibernate.
I have a simple scenario where I have two entities three tables.
one is Teacher.java with teacher table containg id and name.
the other is Student.java with student table containing id and name as well.

let me show you my java files, my tables and the hibernate mapping files.





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

<hibernate-mapping>
<class name="com.oxman.entity.Teacher" table="teachers">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="students" table="teachers_students" cascade="all">
<key column="teacher_id"/>
<many-to-many class="com.oxman.entity.Student"/>
</set>
</class>
</hibernate-mapping>


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

<hibernate-mapping>
<class name="com.oxman.entity.Student" table="students">
<id name="id" column="uid" type="int" unsaved-value="null">
<generator class="native"/>
</id>
<property name="name" type="string" length="255"/>
<set name="teachers" table="teachers_students" cascade="all">
<key column="student_id"/>
<many-to-many class="com.oxman.entity.Teacher"/>
</set>
</class>
</hibernate-mapping>



DROP TABLE IF EXISTS `teachers`;
CREATE TABLE `teachers` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `students`;
CREATE TABLE `students` (
`uid` int(11) NOT NULL auto_increment,
`name` varchar(255) default NULL,
PRIMARY KEY (`uid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

DROP TABLE IF EXISTS `teachers_students`;
CREATE TABLE `teachers_students` (
`teacher_id` int(11) NOT NULL default '0',
`student_id` int(11) NOT NULL default '0',
PRIMARY KEY (`teacher_id`,`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;




I have tried to run this code

Here is the Exception stack trace
Hibernate: insert into teachers (name) values (?)
Hibernate: update students set name=? where uid=?
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 com.oxman.test.StudentTeacherTest.main(StudentTeacherTest.java:48)



Does anybody have any idea what went wrong?

thank you

Sura
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It needs to insert the Student first before it can update it. I think you need to map the two relationships as one side inverse="true"

Mark
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry I didn't realize this is a continuation or prequel to other threads created. Please stick with one thread for your issues on this Teacher Student relationship.

Thanks, closing this thread.

Mark
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic