I have a fairly straightforward relationship that when i create a new object and save it, only 2 out of 3 objects get perisisted. Forgive me if i've missed something, but i've gone over and over the mapping file. Mapped one object the same as the other, and it doesn't appear to work the same.
Vehicle contains one each of Title and Registration. Vehicle can have 0-1 Titles or 0-1 Registrations. Both Title and Registration cannot belong to more than one Vehicle, and cannot exist alone. Title and Registration are extended and there is an additional mapping file. Forgive me, i carved out 80% of the mapping file and other objects to recreate this. I left the simple extended classes there. Who knows, maybe it relates.
Using the suggested approach in the doc, I opted to map the Title and Registration ID such that they use the Vehicle's primary key as their primary key. I implemented Title 1st, and it worked wonderfully.
I then mapped Registration in (what i think is) the exact same way. However when i create the vehicle object and save vehicle, it saves vehicle and title but will not save the registration. You can see in the 1st trace below that Vehicle and Title are inserted. Look at the excerpt of the second trace when i specifically save Registration, and you will see that it is saved.
The code segment (which exists below as well) is:
Code:
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Vehicle v = nyobj.populateVehicle();
System.out.println("Registration references Vehicle:" + v.getRegistration().getVehicle().hashCode());
System.out.println("Title references Vehicle:" + v.getTitle().getVehicle().hashCode());
s.save(v);
tx.commit();
s.close();
If you look in the trace below you will see that both Registration and Title exist and reference vehicle. Moreover, if i add
Code:
s.save(v.getRegistration());
just before s.save() then registration is saved perfectly. It's as though the cascading attribute is not being picked up for Registration but it is for Title.
Not sure what i'm doing wrong, or if there is a problem.
Hibernate version:2.12 Mapping documents:
<hibernate-mapping default-cascade="save-update">
<class name="com.trivin.bo.vehicle.Vehicle" table="VEHICLE">
<id name="vehicleID" column="vehicleID" type="long">
<generator class="identity"/>
</id>
<property name="vin" />
<property name="year" column="modelYear" />
<one-to-one name="title" class="com.trivin.bo.vehicle.Title"/>
</class>
<class name="com.trivin.bo.vehicle.Title" table="TITLE">
<id name="titleID" column="VehicleID">
<generator class="foreign">
<param name="property">vehicle</param>
</generator>
</id>
<discriminator column="JavaSource" type="string"/>
<one-to-one name="vehicle"
class="com.trivin.bo.vehicle.Vehicle"
constrained="true"
/>
<property name="titleNumber" />
</class>
<class name="com.trivin.bo.vehicle.Registration" table="REGISTRATION">
<id name="registrationID" column="VehicleID">
<generator class="foreign">
<param name="property">vehicle</param>
</generator>
</id>
<discriminator column="JavaSource" type="string"/>
<one-to-one name="vehicle"
class="com.trivin.bo.vehicle.Vehicle"
constrained="true"
/>
<property name="plateType" />
<property name="expirationDate" />
<property name="grossWeight" />
<property name="unladenWeight" />
</class>
</hibernate-mapping>
<hibernate-mapping default-cascade="save-update">
<subclass name="com.trivin.bo.vehicle.ny.NYTitle" extends="com.trivin.bo.vehicle.Title" discriminator-value="NYTitle" >
<property name="threeOfName"/>
<property name="exemptFromTitlePlate"/>
</subclass>
<subclass name="com.trivin.bo.vehicle.ny.NYRegistration" extends="com.trivin.bo.vehicle.Registration" discriminator-value="NYRegistration" >
<property name="tempRegIssuedBy"/>
<property name="PIDDealerNumber"/>
</subclass>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():Code:
Session s = sf.openSession();
Transaction tx = s.beginTransaction();
Vehicle v = nyobj.populateVehicle();
System.out.println("Registration references Vehicle:" + v.getRegistration().getVehicle().hashCode());
System.out.println("Title references Vehicle:" + v.getTitle().getVehicle().hashCode());
s.save(v);
tx.commit();
s.close();
Full stack trace of any exception that occurs:Name and version of the database you are using:MS SQL 2000
Debug level Hibernate log excerpt:log4j.rootLogger=warn, stdout
log4j.logger.net.sf.hibernate=debug
### log just the SQL
#log4j.logger.net.sf.hibernate.SQL=debug
### log JDBC bind parameters ###
log4j.logger.net.sf.hibernate.type=debug
Trace output:13:30:54,860 INFO Environment:462 - Hibernate 2.1.2
13:30:54,890 INFO Environment:496 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.HashtableCacheProvider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hibernate.connection.url=jdbc:hsqldb:., hibernate.connection.password=jeffall, hibernate.connection.pool_size=3}
13:30:54,900 INFO Environment:518 - using java.io streams to persist binary types
13:30:54,900 INFO Environment:519 - using CGLIB reflection optimizer
13:30:54,910 INFO Configuration:854 - configuring from resource: /hibernate.cfg.xml
13:30:54,910 INFO Configuration:826 - Configuration resource: /hibernate.cfg.xml
13:30:55,110 INFO Configuration:311 - Mapping resource: ertbo-1.0-hibernate.xml
13:30:55,311 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Vehicle -> VEHICLE
13:30:55,521 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Title -> TITLE
13:30:55,531 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Registration -> REGISTRATION
13:30:55,551 INFO Configuration:311 - Mapping resource: nyertbo-1.0-hibernate.xml
13:30:55,681 INFO Binder:169 - Mapping subclass: com.trivin.bo.vehicle.ny.NYTitle -> TITLE
13:30:55,681 INFO Binder:169 - Mapping subclass: com.trivin.bo.vehicle.ny.NYRegistration -> REGISTRATION
13:30:55,691 INFO Configuration:1017 - Configured SessionFactory: null
13:30:55,691 INFO Configuration:595 - processing one-to-many association mappings
13:30:55,691 INFO Configuration:604 - processing one-to-one association property references
13:30:55,691 INFO Configuration:629 - processing foreign key constraints
13:30:55,711 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
13:30:55,721 INFO SettingsFactory:58 - Maximim outer join fetch depth: 1
13:30:55,721 INFO SettingsFactory:62 - Use outer join fetching: true
13:30:55,721 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:30:55,721 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 3
13:30:55,741 INFO DriverManagerConnectionProvider:71 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:30:55,741 INFO DriverManagerConnectionProvider:72 - connection properties: {user=sa, password=jeffall}
13:30:55,751 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
13:30:55,751 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
13:30:56,012 INFO SettingsFactory:102 - Use scrollable result sets: true
13:30:56,012 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
13:30:56,012 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
13:30:56,012 INFO SettingsFactory:114 - echoing all SQL to stdout
13:30:56,012 INFO SettingsFactory:117 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
13:30:56,012 INFO SettingsFactory:128 - cache provider: net.sf.hibernate.cache.HashtableCacheProvider
13:30:56,062 INFO Configuration:1080 - instantiating and configuring caches
13:30:56,232 INFO SessionFactoryImpl:119 - building session factory
13:30:56,663 INFO ReflectHelper:160 - reflection optimizer disabled for: com.trivin.bo.vehicle.Vehicle, NullPointerException: null
13:30:56,943 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
13:30:56,963 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
13:30:56,963 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:30:56,963 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 3
13:30:56,963 INFO DriverManagerConnectionProvider:71 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:30:56,983 INFO DriverManagerConnectionProvider:72 - connection properties: {user=sa, password=jeffall}
13:30:56,983 INFO SchemaUpdate:102 - Running hbm2ddl schema update
13:30:56,983 INFO SchemaUpdate:110 - fetching database metadata
13:30:57,063 INFO SchemaUpdate:124 - updating schema
13:30:57,063 INFO Configuration:595 - processing one-to-many association mappings
13:30:57,063 INFO Configuration:604 - processing one-to-one association property references
13:30:57,063 INFO Configuration:629 - processing foreign key constraints
13:30:57,564 INFO SchemaUpdate:143 - schema update complete
13:30:57,564 INFO DriverManagerConnectionProvider:137 - cleaning up connection pool: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:30:57,574 INFO UpdateTimestampsCache:35 - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
13:30:57,574 INFO QueryCache:39 - starting query cache at region: net.sf.hibernate.cache.QueryCache
Finished Initializing Hibernate
Aug 25, 2004 1:30:57 PM com.trivin.bo.BOFactory <clinit>
WARNING: Unable to Load Factory:com.trivin.bo.ct.CTBOFactory
Registration references Vehicle:3551336
Title references Vehicle:3551336
Hibernate: insert into VEHICLE (vin, modelYear) values (?, ?) select SCOPE_IDENTITY()
Hibernate: insert into TITLE (threeOfName, exemptFromTitlePlate, titleNumber, JavaSource, VehicleID) values (?, ?, ?, 'NYTitle', ?)
[/code]
below is the trace when i specifically save the registration object within Vehicle
Code:
13:40:50,207 INFO Environment:462 - Hibernate 2.1.2
13:40:50,237 INFO Environment:496 - loaded properties from resource hibernate.properties: {hibernate.connection.driver_class=org.hsqldb.jdbcDriver, hibernate.cglib.use_reflection_optimizer=true, hibernate.cache.provider_class=net.sf.hibernate.cache.HashtableCacheProvider, hibernate.cache.use_query_cache=true, hibernate.max_fetch_depth=1, hibernate.dialect=net.sf.hibernate.dialect.SQLServerDialect, hibernate.jdbc.use_streams_for_binary=true, hibernate.jdbc.batch_size=0, hibernate.query.substitutions=true 1, false 0, yes 'Y', no 'N', hibernate.proxool.pool_alias=pool1, hibernate.connection.username=sa, hibernate.connection.url=jdbc:hsqldb:., hibernate.connection.password=jeffall, hibernate.connection.pool_size=3}
13:40:50,287 INFO Environment:518 - using java.io streams to persist binary types
13:40:50,287 INFO Environment:519 - using CGLIB reflection optimizer
13:40:50,297 INFO Configuration:854 - configuring from resource: /hibernate.cfg.xml
13:40:50,297 INFO Configuration:826 - Configuration resource: /hibernate.cfg.xml
13:40:50,487 INFO Configuration:311 - Mapping resource: ertbo-1.0-hibernate.xml
13:40:50,798 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Vehicle -> VEHICLE
13:40:50,998 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Title -> TITLE
13:40:51,018 INFO Binder:229 - Mapping class: com.trivin.bo.vehicle.Registration -> REGISTRATION
13:40:51,038 INFO Configuration:311 - Mapping resource: nyertbo-1.0-hibernate.xml
13:40:51,158 INFO Binder:169 - Mapping subclass: com.trivin.bo.vehicle.ny.NYTitle -> TITLE
13:40:51,168 INFO Binder:169 - Mapping subclass: com.trivin.bo.vehicle.ny.NYRegistration -> REGISTRATION
13:40:51,168 INFO Configuration:1017 - Configured SessionFactory: null
13:40:51,168 INFO Configuration:595 - processing one-to-many association mappings
13:40:51,168 INFO Configuration:604 - processing one-to-one association property references
13:40:51,168 INFO Configuration:629 - processing foreign key constraints
13:40:51,198 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
13:40:51,198 INFO SettingsFactory:58 - Maximim outer join fetch depth: 1
13:40:51,198 INFO SettingsFactory:62 - Use outer join fetching: true
13:40:51,208 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:40:51,208 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 3
13:40:51,228 INFO DriverManagerConnectionProvider:71 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:40:51,228 INFO DriverManagerConnectionProvider:72 - connection properties: {user=sa, password=jeffall}
13:40:51,228 INFO TransactionFactoryFactory:31 - Transaction strategy: net.sf.hibernate.transaction.JDBCTransactionFactory
13:40:51,228 INFO TransactionManagerLookupFactory:33 - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
13:40:51,478 INFO SettingsFactory:102 - Use scrollable result sets: true
13:40:51,478 INFO SettingsFactory:105 - Use JDBC3 getGeneratedKeys(): false
13:40:51,478 INFO SettingsFactory:108 - Optimize cache for minimal puts: false
13:40:51,478 INFO SettingsFactory:114 - echoing all SQL to stdout
13:40:51,478 INFO SettingsFactory:117 - Query language substitutions: {no='N', true=1, yes='Y', false=0}
13:40:51,478 INFO SettingsFactory:128 - cache provider: net.sf.hibernate.cache.HashtableCacheProvider
13:40:51,528 INFO Configuration:1080 - instantiating and configuring caches
13:40:51,699 INFO SessionFactoryImpl:119 - building session factory
13:40:52,109 INFO ReflectHelper:160 - reflection optimizer disabled for: com.trivin.bo.vehicle.Vehicle, NullPointerException: null
13:40:52,369 INFO SessionFactoryObjectFactory:82 - no JNDI name configured
13:40:52,389 INFO Dialect:82 - Using dialect: net.sf.hibernate.dialect.SQLServerDialect
13:40:52,399 INFO DriverManagerConnectionProvider:41 - Using Hibernate built-in connection pool (not for production use!)
13:40:52,399 INFO DriverManagerConnectionProvider:42 - Hibernate connection pool size: 3
13:40:52,399 INFO DriverManagerConnectionProvider:71 - using driver: com.microsoft.jdbc.sqlserver.SQLServerDriver at URL: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:40:52,399 INFO DriverManagerConnectionProvider:72 - connection properties: {user=sa, password=jeffall}
13:40:52,399 INFO SchemaUpdate:102 - Running hbm2ddl schema update
13:40:52,399 INFO SchemaUpdate:110 - fetching database metadata
13:40:52,459 INFO SchemaUpdate:124 - updating schema
13:40:52,459 INFO Configuration:595 - processing one-to-many association mappings
13:40:52,459 INFO Configuration:604 - processing one-to-one association property references
13:40:52,459 INFO Configuration:629 - processing foreign key constraints
13:40:52,910 INFO SchemaUpdate:143 - schema update complete
13:40:52,910 INFO DriverManagerConnectionProvider:137 - cleaning up connection pool: jdbc:microsoft:sqlserver://localhost:1433;SelectMethod=Cursor;DatabaseName=ertbo
13:40:52,910 INFO UpdateTimestampsCache:35 - starting update timestamps cache at region: net.sf.hibernate.cache.UpdateTimestampsCache
13:40:52,920 INFO QueryCache:39 - starting query cache at region: net.sf.hibernate.cache.QueryCache
Finished Initializing Hibernate
Aug 25, 2004 1:40:53 PM com.trivin.bo.BOFactory <clinit>
WARNING: Unable to Load Factory:com.trivin.bo.ct.CTBOFactory
Registration references Vehicle:3551336
Title references Vehicle:3551336
Hibernate: insert into VEHICLE (vin, modelYear) values (?, ?) select SCOPE_IDENTITY()
Hibernate: insert into TITLE (threeOfName, exemptFromTitlePlate, titleNumber, JavaSource, VehicleID) values (?, ?, ?, 'NYTitle', ?)
Hibernate: insert into REGISTRATION (tempRegIssuedBy, PIDDealerNumber, plateType, expirationDate, grossWeight, unladenWeight, JavaSource, VehicleID) values (?, ?, ?, ?, ?, ?, 'NYRegistration', ?)