I have three entities - Reservation , Disc , AudioDisc, VideoDisc (both extending Disc class). My Mapping Files are as follows :
Reservation.hbm.xml
Code:
<?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="tableperconcretesubclass.Reservation" table="DB2ADMIN.RES_Test">
<id name="id" type="integer" column="ID" unsaved-value="0">
<generator class ="increment"/>
</id>
<property name="description" type="string" column="DESCRIPTION"></property>
<set name="discs" lazy="true" cascade="all" >
<key column="DISC_ID" />
<one-to-many class="tableperconcretesubclass.Disc"/>
</set>
</class>
</hibernate-mapping>
Disc.hbm.xml :
Code:
<?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="tableperconcretesubclass.Disc">
<id name="id" type="integer" column="ID" >
<generator class ="assigned"/>
</id>
<union-subclass name="tableperconcretesubclass.AudioDisc" lazy="false" table="DB2ADMIN.AUDIODISC_Test">
<property name="singer" type="string" column="SINGER" />
<property name="numOfSongs" type="int" column="NUM_OF_SONGS"/>
<set name="songs" lazy="true" cascade="all" >
<key column="AUDIODISC_ID" />
<one-to-many class="tableperconcretesubclass.Song"/>
</set>
</union-subclass>
<union-subclass name="tableperconcretesubclass.VideoDisc" lazy="true" table="DB2ADMIN.VIDEODISC_Test">
<property name="director" type="string" column="DIRECTOR" />
<property name="language" type="string" column="LANG"/>
</union-subclass>
</class>
</hibernate-mapping>
Song.hbm.xml :
Quote:
<?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="tableperconcretesubclass.Song" table="DB2ADMIN.SONG_Test">
<id name="id" type="integer" column="ID" >
<generator class ="assigned"/>
</id>
<property name="title">
<column name="TITLE" />
</property>
<property name="movie">
<column name="MOVIE" />
</property>
</class>
</hibernate-mapping>
Code:
public void createEntities(SessionFactory sessionFactory){
Session session= sessionFactory.openSession();
try {
Transaction trans = session.beginTransaction();
Reservation r= new Reservation();
r.setId(1);
r.setDescription("aaaaa");
Song s = new Song();
s.setId(1);
s.setMovie("1");
s.setTitle("1");
Set<Song>songs = new HashSet<Song>();
songs.add(s);
Disc disc = new AudioDisc();
disc.setId(1);
AudioDisc aDisc = (AudioDisc)disc ;
aDisc.setNumOfSongs(1);
aDisc.setSinger("1");
aDisc.setSongs(songs);
Set<Disc>discs = new HashSet<Disc>();
discs.add(aDisc);
r.setDiscs(discs);
session.save(s);
session.save(disc);
session.save(r);
trans.commit();
} catch (Exception e) {
e.printStackTrace();
}
}
Now when i am trying to run the createEntity() method with property
Code:
<property name="hibernate.hbm2ddl.auto">create</property>
in hibernate.cfg.xml file i am getting following exceptions :
Code:
Hibernate: insert into DB2ADMIN.SONG_Test (TITLE, MOVIE, ID) values (?, ?, ?)
Hibernate: insert into DB2ADMIN.AUDIODISC_Test (SINGER, NUM_OF_SONGS, ID) values (?, ?, ?)
Hibernate: insert into DB2ADMIN.RES_Test (DESCRIPTION, ID) values (?, ?)
Hibernate: update DB2ADMIN.SONG_Test set AUDIODISC_ID=? where ID=?
Hibernate: update Disc set DISC_ID=? where ID=?
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:85)
at org.hibernate.jdbc.Expectations$BasicExpectation.verifyOutcome(Expectations.java:70)
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:47)
at org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1168)
at org.hibernate.action.CollectionRecreateAction.execute(CollectionRecreateAction.java:58)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:171)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1027)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:365)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at tableperconcretesubclass.TestApp.createEntities(TestApp.java:140)
at tableperconcretesubclass.TestApp.main(TestApp.java:113)
I am not able to find what i am doing wrong here ?? Can anybody please help me ?
Thanks.