aspose file tools*
The moose likes Object Relational Mapping and the fly likes one to many mapping not using correct key? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "one to many mapping not using correct key?" Watch "one to many mapping not using correct key?" New topic
Author

one to many mapping not using correct key?

Stuart Houston
Greenhorn

Joined: Aug 19, 2004
Posts: 6
Below is the mappings, code and stacktrace of my project

DEpfilesin and DDetails are a one to many relationship
1 Depfilesin could have many DDetails records

3770246 Is the value that is stored in DDetails.id
I want it to use the value that is stored in DDetails.filesin_id when it does the link between the two
How do i make it do this?


Hibernate version: 2.1.3

Mapping documents:

<!--DTRNS MAPPING-->
<hibernate-mapping>
<class name="DTrns" table="d_trns" dynamic-update="true" >
<id name="trnID" column="trn_id" type ="java.lang.Long" unsaved-value="null">
<generator class="native" />
</id>

<bag name="dDetails" inverse="true" lazy="true" cascade="all">
<key column="trn_id"/>
<one-to-many class="DDetails"/>
</bag>
</class>
</hibernate-mapping>


<!--DEPFILESIN MAPPING-->
<hibernate-mapping>
<class name="DEpFilesIn" table="d_epfilesin">
<id name="id" column="id" type ="java.lang.Long" unsaved-value="null">
<generator class="native" />
</id>
<property name="subject" column="subject" not-null="false"/>
<set name="dDetails" inverse="true" lazy="true">
<key column="filesin_id"/>
<one-to-many class="DDetails"/>
</set>
</class>
</hibernate-mapping>

<!--DDETAILS MAPPING -->
<hibernate-mapping>
<class name="DDetails" table="d_details">
<id name="id" column="id" type ="java.lang.Long" unsaved-value="null">
<generator class="native" />
</id>

<property name="filesInID" column="filesin_id" not-null="false"/>
<many-to-one name="dEpFilesIn" class="DEpFilesIn" column="id" not-null="false"/>

</class>
</hibernate-mapping>




Code between sessionFactory.openSession() and session.close():
DTrns dtransObj = (DTrns)normalSession.load(DTrns.class,new Long(2290156));
System.out.println("DTRNS TRN ID " + dtransObj.getTrnID());
//LOADS DDetails
List list = dtransObj.getdDetails();
if(list.size()>0)
{
Iterator it = list.iterator();
while(it.hasNext())
{
DDetails dDetailsObj = (DDetails)it.next();
System.out.println("DDETAILS FILES_IN_ID " + dDetailsObj.getFilesInID());
//LOADS D_EPFilesIn
Set dEpFilesInList = dDetailsObj.getdEpFilesIn();
if(dEpFilesInList!=null && (dEpFilesInList.size()>0))
{
Iterator et = dEpFilesInList.iterator();
while(et.hasNext())
{
DEpFilesIn dEpFilesInObj = (DEpFilesIn)et.next();
System.out.println("DEPFILESIN FILES ID " + dEpFilesInObj.getFilesId() + " ID " + dEpFilesInObj.getId());
}
}
}
}
System.out.println("");

Full stack trace of any exception that occurs:
1516 [main] ERROR collection.PersistentCollection - Failed to lazily initialize a collection
net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 3770246, of class: DEpFilesIn
at net.sf.hibernate.impl.SessionImpl.throwObjectNotFound(SessionImpl.java:1663)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1702)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:62)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1933)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:184)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:492)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:90)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2734)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:140)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:60)
at net.sf.hibernate.collection.Bag.size(Bag.java:194)
at XMLTest.getQuery(XMLTest.java:85)
at XMLTest.main(XMLTest.java:229)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 3770246, of class: DEpFilesIn
at net.sf.hibernate.impl.SessionImpl.throwObjectNotFound(SessionImpl.java:1663)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1702)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:62)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1933)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:184)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:492)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:90)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2734)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:140)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:60)
at net.sf.hibernate.collection.Bag.size(Bag.java:194)
at XMLTest.getQuery(XMLTest.java:85)
at XMLTest.main(XMLTest.java:229)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
rethrown as net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection: No row with the given identifier exists: 3770246, of class: DEpFilesIn
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:152)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:60)
at net.sf.hibernate.collection.Bag.size(Bag.java:194)
at XMLTest.getQuery(XMLTest.java:85)
at XMLTest.main(XMLTest.java:229)
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:324)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:78)
Caused by: net.sf.hibernate.ObjectNotFoundException: No row with the given identifier exists: 3770246, of class: DEpFilesIn
at net.sf.hibernate.impl.SessionImpl.throwObjectNotFound(SessionImpl.java:1663)
at net.sf.hibernate.impl.SessionImpl.internalLoad(SessionImpl.java:1702)
at net.sf.hibernate.type.ManyToOneType.resolveIdentifier(ManyToOneType.java:62)
at net.sf.hibernate.impl.SessionImpl.initializeEntity(SessionImpl.java:1933)
at net.sf.hibernate.loader.Loader.doFind(Loader.java:184)
at net.sf.hibernate.loader.Loader.loadCollection(Loader.java:492)
at net.sf.hibernate.loader.OneToManyLoader.initialize(OneToManyLoader.java:90)
at net.sf.hibernate.impl.SessionImpl.initialize(SessionImpl.java:2734)
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:140)
... 9 more


Name and version of the database you are using:
MYSQL 3.23.38-nt
Sagar Bilgi
Ranch Hand

Joined: Apr 26, 2001
Posts: 37
Try again by removing the inverse = true.if that doent work then try removing both lazy = true and inverse -"true" from the files. I had the same problem and it worked after I removed the inverse=true from the parent side.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one to many mapping not using correct key?
 
Similar Threads
Could not read the data
Unknown entity class: java.lang.Integer
Persistor Error:
query a database table using a foreign key ( where) using Hibernate.
org hibernate exception GenericJDBCException