Haven't been able to get this simple example to work.
Ideally I want the parent primary key and children primary keys generated by database but save parent/children with one session.save command.
Hibernate version:3.0
Mapping documents:
<!-- Team.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" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">
<class name="Team" table="Team">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="assigned"/>
</id>
<property name="name" column="name" type="java.lang.String" not-null="true" />
<set name="players" table="player" lazy="true" cascade="save-update" inverse="true" >
<key column="team_id"/>
<one-to-many class="Player"/>
</set>
</class>
</hibernate-mapping>
<!--Player.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" >
<!-- DO NOT EDIT: This is a generated file that is synchronized -->
<!-- by MyEclipse Hibernate tool integration. -->
<!-- Created Wed Jun 15 12:17:48 EDT 2005 -->
<hibernate-mapping package="com.cvs.rx.batch.hibernate">
<class name="Player" table="Player">
<id name="id" column="id" type="java.lang.Long" unsaved-value="null">
<generator class="native"/>
</id>
<many-to-one name="teamId" class="Team" column="team_id" cascade="save-update"/>
<property name="firstname" column="firstName" type="java.lang.String" not-null="true" />
<property name="lastname" column="lastName" type="java.lang.String" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Team team = new Team(new Long(1));
team.setName("Pistons");
try {
s = SessionManager.currentSession();
//s.saveOrUpdate(team);
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
Set players = new HashSet();
Player p1 = new Player();
p1.setFirstname("Chucky");
p1.setLastname("Atkins");
p1.setTeam(team);
team.addToplayers(p1);
/* i don't think this is needed but it doesn't work either way */
p1.setTeamId(team.getId());
try {
s.save(team);
s.connection().commit();
s.flush();
} catch (Exception e) {
System.out.println(e.getStackTrace());
}
Full stack trace of any exception that occurs:
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.cvs.rx.batch.hibernate.AbstractTeam.id
java.lang.IllegalArgumentException: object is not an instance of declaring class
org.hibernate.PropertyAccessException: IllegalArgumentException occurred calling getter of com.cvs.rx.batch.hibernate.AbstractTeam.id
org.hibernate.exception.NestableDelegate@b0ede5
IllegalArgumentException occurred calling
class com.cvs.rx.batch.hibernate.AbstractTeam
id
[org.hibernate.property.BasicPropertyAccessor$BasicGetter.get(BasicPropertyAccessor.java:119), org.hibernate.tuple.AbstractTuplizer.getIdentifier(AbstractTuplizer.java:103), org.hibernate.persister.entity.BasicEntityPersister.getIdentifier(BasicEntityPersister.java:2944), org.hibernate.persister.entity.BasicEntityPersister.isTransient(BasicEntityPersister.java:2705), org.hibernate.engine.ForeignKeys.isTransient(ForeignKeys.java:181), org.hibernate.event.def.AbstractSaveEventListener.getEntityState(AbstractSaveEventListener.java:409), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:82), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468), org.hibernate.engine.Cascades$5.cascade(Cascades.java:154), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascade(Cascades.java:847), org.hibernate.event.def.AbstractSaveEventListener.cascadeBeforeSave(AbstractSaveEventListener.java:332), org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:214), org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160), org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:95), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.performSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:96), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.saveOrUpdate(SessionImpl.java:468), org.hibernate.engine.Cascades$5.cascade(Cascades.java:154), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:771), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascadeCollection(Cascades.java:895), org.hibernate.engine.Cascades.cascadeAssociation(Cascades.java:792), org.hibernate.engine.Cascades.cascade(Cascades.java:720), org.hibernate.engine.Cascades.cascade(Cascades.java:847), org.hibernate.event.def.AbstractSaveEventListener.cascadeAfterSave(AbstractSaveEventListener.java:363), org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:265), org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:160), org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:108), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:184), org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:33), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:173), org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:27), org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:69), org.hibernate.impl.SessionImpl.save(SessionImpl.java:481), org.hibernate.impl.SessionImpl.save(SessionImpl.java:476), com.cvs.rx.batch.hibernate.TestHB.testTeam(TestHB.java:69), com.cvs.rx.batch.hibernate.TestHB.main(TestHB.java:37)]
false
Name and version of the database you are using:
SQL Server 2000
[
|