| 
					
						 Hi,
 This is a newbie question, any help would be appreciated.  I've read over the quickstart guide, FAQ, chapter 2 of the book, etc, and I must be missing something simple because I just can't get this association to work.  The complete details are below: basically the classes and Hibernate access to them is working fine individually, but when I try to associate one to the other it a fails.
 
 I have two database tables, Car and Trim, like so:
 CREATE TABLE `cars` (
   `id` int(10) unsigned NOT NULL auto_increment,
   `make_id` int(10) unsigned NOT NULL default '0',
   `model_id` int(10) unsigned NOT NULL default '0',
   `year` int(10) unsigned NOT NULL default '0' COMMENT 'e.g. 1996'
 )
 
 CREATE TABLE `trims` (
   `id` int(10) unsigned NOT NULL auto_increment,
   `car_id` int(10) unsigned NOT NULL default '0',
   `name` text NOT NULL,
   `description` text NOT NULL COMMENT,
   PRIMARY KEY  (`id`),
   KEY `FK_trims_1` (`car_id`),
   CONSTRAINT `FK_trims_1` FOREIGN KEY (`car_id`) REFERENCES `cars` (`id`)
 )
 
 I have simple Java POJO classes for these, com.mycompany.Car and com.mycompany.Trim, following the JavaBean conventions as shown in the Hibernate reference documentation.  I have Managers and unit tests for these classes that all work fine.  The complete mapping files are below.
 
 I'd like a unidirectional association from Car to Trims, i.e. one car has many trims.  If I understand the Hibernate terminology correctly, Car is the parent, Trim is a child.  This is a Set association, as there is some number of trims and the order does not matter.
 
 So I added the following methods to thr Car class:
 private Set trims = new HashSet();
 public Set getTrims() { return trims; }
 public void setTrims(Set trims) { this.trims = trims; }
 
 *** This is where I need help: ***
 What do I add to the Car.hbm.xml mapping files to make this work?  I tried this and got the error shown below:
   <set name="trims" inverse="true">
     <key column="car_id"/>
     <one-to-many class="com.mycompany.Trim"/>
   </set> 
 
 Hibernate version: 3.1.2  
 
 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>
   <class name="com.mycompany.Car" table="CARS">
     <id name="id" column="ID">
       <generator class="native" />
     </id>
     <property name="makeId" column="MAKE_ID" />
     <property name="modelId" column="MODEL_ID" />
     <property name="year" />
   </class>
 </hibernate-mapping>
 
 <?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>
   <class name="com.mycompany.Trim" table="TRIMS">
     <id name="id" column="ID">
       <generator class="native" />
     </id>
     <property name="carId" column="CAR_ID" />
     <property name="name" />
     <property name="description" />
   </class>
 </hibernate-mapping>
 
 Full stack trace of any exception that occurs:
 Mar 14, 2006 1:39:01 PM org.hibernate.LazyInitializationException <init>
 SEVERE: failed to lazily initialize a collection of role: com.nimalex.domain.Car
 .trims, no session or session was closed
 org.hibernate.LazyInitializationException: failed to lazily initialize a collect
 ion of role: com.nimalex.domain.Car.trims, no session or session was closed
         at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
 lizationException(AbstractPersistentCollection.java:358)
         at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
 lizationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
         at org.hibernate.collection.AbstractPersistentCollection.initialize(Abst
 ractPersistentCollection.java:343)
         at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPe
 rsistentCollection.java:86)
         at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:25
 3)
         at java.lang.String.valueOf(String.java:2577)
         at java.lang.StringBuilder.append(StringBuilder.java:116)
         at com.nimalex.domain.Car.toString(Car.java:147)
         at java.lang.String.valueOf(String.java:2577)
         at java.lang.StringBuilder.append(StringBuilder.java:116)
         at com.nimalex.domain.CarManagerTest.testGet(CarManagerTest.java:115)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.apache.maven.surefire.battery.JUnitBattery.executeJUnit(JUnitBatt
 ery.java:242)
         at org.apache.maven.surefire.battery.JUnitBattery.execute(JUnitBattery.j
 ava:216)
         at org.apache.maven.surefire.Surefire.executeBattery(Surefire.java:215)
         at org.apache.maven.surefire.Surefire.run(Surefire.java:163)
         at org.apache.maven.surefire.Surefire.run(Surefire.java:87)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.apache.maven.surefire.SurefireBooter.runTestsInProcess(SurefireBo
 oter.java:285)
         at org.apache.maven.surefire.SurefireBooter.run(SurefireBooter.java:201)
 
         at org.apache.maven.test.SurefirePlugin.execute(SurefirePlugin.java:366)
 
         at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
 nManager.java:415)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
 ultLifecycleExecutor.java:531)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi
 fecycle(DefaultLifecycleExecutor.java:472)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
 ltLifecycleExecutor.java:451)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
 dleFailures(DefaultLifecycleExecutor.java:303)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
 ts(DefaultLifecycleExecutor.java:270)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
 fecycleExecutor.java:139)
         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:322)
         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:115)
         at org.apache.maven.cli.MavenCli.main(MavenCli.java:249)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
         at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
         at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
 
         at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
 Mar 14, 2006 1:39:01 PM com.nimalex.domain.CarManagerTest testGet
 SEVERE: Failed test
 org.hibernate.LazyInitializationException: failed to lazily initialize a collect
 ion of role: com.nimalex.domain.Car.trims, no session or session was closed
         at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
 lizationException(AbstractPersistentCollection.java:358)
         at org.hibernate.collection.AbstractPersistentCollection.throwLazyInitia
 lizationExceptionIfNotConnected(AbstractPersistentCollection.java:350)
         at org.hibernate.collection.AbstractPersistentCollection.initialize(Abst
 ractPersistentCollection.java:343)
         at org.hibernate.collection.AbstractPersistentCollection.read(AbstractPe
 rsistentCollection.java:86)
         at org.hibernate.collection.PersistentSet.toString(PersistentSet.java:25
 3)
         at java.lang.String.valueOf(String.java:2577)
         at java.lang.StringBuilder.append(StringBuilder.java:116)
         at com.nimalex.domain.Car.toString(Car.java:147)
         at java.lang.String.valueOf(String.java:2577)
         at java.lang.StringBuilder.append(StringBuilder.java:116)
         at com.nimalex.domain.CarManagerTest.testGet(CarManagerTest.java:115)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.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 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.apache.maven.surefire.battery.JUnitBattery.executeJUnit(JUnitBatt
 ery.java:242)
         at org.apache.maven.surefire.battery.JUnitBattery.execute(JUnitBattery.j
 ava:216)
         at org.apache.maven.surefire.Surefire.executeBattery(Surefire.java:215)
         at org.apache.maven.surefire.Surefire.run(Surefire.java:163)
         at org.apache.maven.surefire.Surefire.run(Surefire.java:87)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.apache.maven.surefire.SurefireBooter.runTestsInProcess(SurefireBo
 oter.java:285)
         at org.apache.maven.surefire.SurefireBooter.run(SurefireBooter.java:201)
 
         at org.apache.maven.test.SurefirePlugin.execute(SurefirePlugin.java:366)
 
         at org.apache.maven.plugin.DefaultPluginManager.executeMojo(DefaultPlugi
 nManager.java:415)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoals(Defa
 ultLifecycleExecutor.java:531)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalWithLi
 fecycle(DefaultLifecycleExecutor.java:472)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoal(Defau
 ltLifecycleExecutor.java:451)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeGoalAndHan
 dleFailures(DefaultLifecycleExecutor.java:303)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.executeTaskSegmen
 ts(DefaultLifecycleExecutor.java:270)
         at org.apache.maven.lifecycle.DefaultLifecycleExecutor.execute(DefaultLi
 fecycleExecutor.java:139)
         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:322)
         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:115)
         at org.apache.maven.cli.MavenCli.main(MavenCli.java:249)
         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
 java:39)
         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
 sorImpl.java:25)
         at java.lang.reflect.Method.invoke(Method.java:585)
         at org.codehaus.classworlds.Launcher.launchEnhanced(Launcher.java:315)
         at org.codehaus.classworlds.Launcher.launch(Launcher.java:255)
         at org.codehaus.classworlds.Launcher.mainWithExitCode(Launcher.java:430)
 
         at org.codehaus.classworlds.Launcher.main(Launcher.java:375)
 
 Name and version of the database you are using:
 MySQL 5.0.18-nt
 
 The generated SQL (show_sql=true):
 Hibernate: select car0_.ID as ID0_, car0_.MAKE_ID as MAKE2_0_, car0_.MODEL_ID as
  MODEL3_0_, car0_.year as year0_ from CARS car0_ where car0_.MAKE_ID=2 and car0_
 .MODEL_ID=2 and car0_.year=1964
 
 Again, any help would be appreciated.  Thank you in advance,
 
 Yoav Shapira 
					
  
						
					 |