I have a bi-directional one-to-many association and I want to call the parent.saveOrUpdate() to save both parent and child. I want Hibernate to distinguish between a save or update but it cannot. Instead, I used a work around --> Parent p = p.load(parentID); and if parent is loaded then update(); else save(). That works but I think I should be able to use the <version> with the unsaved value in my mapping file (see below). I need to keep my assigned identifier.
Also important, when I load the parent the associated children are not being retrieved. The code is as follows:
Book loadedBook = bookDao.load(bookID);
if(loadedBook != null)
{
book = loadedBook;
offer1 = (Offer) loadedBook.getOffers().iterator().next();
offer2 = (Offer) loadedBook.getOffers().iterator().next();
isLoaded = true;
}
else
{
book = new Book();
offer1 = new Offer();
offer2 = new Offer();
}
I am getting the following exception:
[6/17/05 9:05:57:377 CDT] 00000037 SystemErr R java.util.NoSuchElementException
at java.util.HashMap$HashIterator.nextEntry(HashMap.java(Compiled Code))
at java.util.HashMap$KeyIterator.next(HashMap.java(Compiled Code))
at net.sf.hibernate.collection.PersistentCollection$IteratorProxy.next(PersistentCollection.java:437)
at com.test.ecs.persistentservice.BookPersistentService.insertBook(BookPersistentService.java:83)
at com.test.ecs.businessservice.TestServlet.doGet(TestServlet.java:78)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1212)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:629)
at com.ibm.ws.webcontainer.webapp.WebApp.handleRequest(WebApp.java:2837)
at com.ibm.ws.webcontainer.webapp.WebGroup.handleRequest(WebGroup.java:220)
at com.ibm.ws.webcontainer.VirtualHost.handleRequest(VirtualHost.java:204)
at com.ibm.ws.webcontainer.WebContainer.handleRequest(WebContainer.java:1681)
at com.ibm.ws.webcontainer.channel.WCChannelLink.ready(WCChannelLink.java:77)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleDiscrimination(HttpInboundLink.java:421)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.handleNewInformation(HttpInboundLink.java:367)
at com.ibm.ws.http.channel.inbound.impl.HttpInboundLink.ready(HttpInboundLink.java:276)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.sendToDiscriminaters(NewConnectionInitialReadCallback.java:201)
at com.ibm.ws.tcp.channel.impl.NewConnectionInitialReadCallback.complete(NewConnectionInitialReadCallback.java:103)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.requestComplete(WorkQueueManager.java:548)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.attemptIO(WorkQueueManager.java(Compiled Code))
at com.ibm.ws.tcp.channel.impl.WorkQueueManager.workerRun(WorkQueueManager.java:934)
at com.ibm.ws.tcp.channel.impl.WorkQueueManager$Worker.run(WorkQueueManager.java:1021)
at com.ibm.ws.util.ThreadPool$Worker.run(ThreadPool.java(Compiled Code))
Mapping documents:
PARENT MAPPING:
<class
name="Book"
table="book"
>
<id
name="BookID"
type="string"
column="BookID"
>
<generator class="assigned"/>
</id>
<version
column="Version"
name="_version"
type="integer"
unsaved-value="null"
/>
<set name="Offers" inverse="true" cascade="all-delete-orphan">
<key column="bookID"/>
<one-to-many class="com.test.hibernate.Offer"/>
</set>
</class>
CHILD MAPPING:
<class
name="Offer"
table="offer"
>
<id
name="OfferID"
type="string"
column="OfferID"
>
<generator class="assigned"/>
</id>
<version
column="Version"
name="_version"
type="integer"
unsaved-value="null"
/>
<many-to-one name="Book" column="bookID" class="com.test.hibernate.Book" not-null="true" outer-join="true"/>
I am using Hibernate 2 with sql server version 8. Please help! Thanks!
|