I am getting an exception saying that "create" is not a supported cascade style... yet the Hibernate 3.0 documentation suggests that it is, and that in fact it is how I would acheive what I am trying to do.... specifically create two persistent objects, one which contains the other in a collection, save() the parent object, and have the child object in the collection created/saved as well.
Is "create" no longer a valid cascade style? If so, a) what is the alternative, and b) I'd suggest you update the docs. If it is a supported style, then what am I doing wrong to get this exception?
Thanks,
-
John
Hibernate version: 3.0
Mapping documents:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping
package="com.jpevans.ta.bo">
<class
name="Tournament"
table="tournament">
<id name="id" column="tournament_id" type="long" unsaved-value="null">
<generator class="hilo"/>
</id>
<property name="name" column="name" type="string" length="255" not-null="true"/>
<property name="start" column="start" type="timestamp" not-null="true"/>
<property name="game" not-null="true">
<type name="com.jpevans.ta.db.EnumUserType">
<param name="enumClassName">com.jpevans.ta.enums.Game</param>
</type>
</property>
<property name="gameType" not-null="true">
<type name="com.jpevans.ta.db.EnumUserType">
<param name="enumClassName">com.jpevans.ta.enums.GameType</param>
</type>
</property>
<set name="players" table="trny_player" lazy="false" cascade="create,save-update">
<key column="tournament_id"/>
<many-to-many class="Player" column="player_id"/>
</set>
<map name="places" table="places" lazy="false">
<key column="tournament_id"/>
<map-key column="place" type="integer"/>
<composite-element class="Place">
<property name="prize" column="prize" type="double"/>
<many-to-one name="finisher" column="finisher_id" class="Player" lazy="false" cascade="create,save-update"/>
</composite-element>
</map>
</class>
<query name="tournament_by_id">
from Tournament as t where t.id = :id
</query>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Transaction tx = session.beginTransaction();
session.save(tournament);
tx.commit();
Full stack trace of any exception that occurs:
org.hibernate.MappingException: Unsupported cascade style: create
at org.hibernate.engine.Cascades.getCascadeStyle(Cascades.java:961)
at org.hibernate.mapping.Property.getCascadeStyle(Property.java:92)
at org.hibernate.mapping.Component.getType(Component.java:150)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:231)
at org.hibernate.mapping.Collection.validate(Collection.java:237)
at org.hibernate.mapping.IndexedCollection.validate(IndexedCollection.java:67)
at org.hibernate.cfg.Configuration.validate(Configuration.java:817)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1049)
at com.jpevans.ta.db.Database.<init>(Database.java:26)
at com.jpevans.ta.acceptance.api.TournamentStories.testAddATournament(TournamentStories.java:40)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:474)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:342)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:194)
Name and version of the database you are using:
HSQLDB 1.7.3.0
The generated SQL (show_sql=true):
doesn't get this far
Debug level Hibernate log excerpt:
0 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.0
16 [main] INFO org.hibernate.cfg.Environment - loaded properties from resource hibernate.properties: {hibernate.connection.username=sa, hibernate.connection.password=****, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=org.hibernate.dialect.HSQLDialect, hibernate.connection.pool_size=2, hibernate.connection.url=jdbc:hsqldb:f:/dbs/ta, hibernate.connection.driver_class=org.hsqldb.jdbcDriver}
16 [main] INFO org.hibernate.cfg.Environment - using CGLIB reflection optimizer
16 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
172 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jpevans/ta/bo/Player.hbm.xml
312 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
312 [main] DEBUG org.hibernate.util.DTDEntityResolver - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
578 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.jpevans.ta.bo.Player -> player
594 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: id -> player_id
609 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: first -> first
609 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: last -> last
609 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: nick -> nick
609 [main] DEBUG org.hibernate.cfg.HbmBinder - Named query: player_by_id ->
from Player as p where p.id = :id
625 [main] INFO org.hibernate.cfg.Configuration - Mapping resource: com/jpevans/ta/bo/Tournament.hbm.xml
625 [main] DEBUG org.hibernate.util.DTDEntityResolver - trying to locate
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath under org/hibernate/
625 [main] DEBUG org.hibernate.util.DTDEntityResolver - found
http://hibernate.sourceforge.net/hibern ... ng-3.0.dtd in classpath
703 [main] INFO org.hibernate.cfg.HbmBinder - Mapping class: com.jpevans.ta.bo.Tournament -> tournament
703 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: id -> tournament_id
703 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: name -> name
703 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: start -> start
703 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: game -> game
703 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: gameType -> gameType
719 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: com.jpevans.ta.bo.Tournament.players -> trny_player
719 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: players
719 [main] INFO org.hibernate.cfg.HbmBinder - Mapping collection: com.jpevans.ta.bo.Tournament.places -> places
719 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: places
719 [main] DEBUG org.hibernate.cfg.HbmBinder - Named query: tournament_by_id ->
from Tournament as t where t.id = :id
719 [main] DEBUG org.hibernate.cfg.Configuration - Preparing to build session factory with filters : {}
719 [main] INFO org.hibernate.cfg.Configuration - processing extends queue
719 [main] INFO org.hibernate.cfg.Configuration - processing collection mappings
719 [main] DEBUG org.hibernate.cfg.HbmBinder - Second pass for collection: com.jpevans.ta.bo.Tournament.players
859 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped collection key: tournament_id, element: player_id
859 [main] DEBUG org.hibernate.cfg.HbmBinder - Second pass for collection: com.jpevans.ta.bo.Tournament.places
859 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: prize -> prize
859 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped property: finisher -> finisher_id
859 [main] DEBUG org.hibernate.cfg.HbmBinder - Mapped collection key: tournament_id, index: place, element: prize, finisher_id
859 [main] INFO org.hibernate.cfg.Configuration - processing association property references
859 [main] INFO org.hibernate.cfg.Configuration - processing foreign key constraints
859 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: com.jpevans.ta.bo.Player
859 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: com.jpevans.ta.bo.Tournament
859 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: com.jpevans.ta.bo.Player
859 [main] DEBUG org.hibernate.cfg.Configuration - resolving reference to class: com.jpevans.ta.bo.Tournament