-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Saving child nodes with DOM4J
PostPosted: Fri Aug 11, 2006 1:54 am 
Newbie

Joined: Fri Aug 11, 2006 1:39 am
Posts: 2
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hey all,

Am using Hibernate3's new feature to populate XML into db. I can save the root tag but not able to save child tags.

The xml which am trying to save is:
<tns:Trade id="123" quantity="100.0" security="912828BK5"

xmlns:tns="http://mymachine/TRP/Trade"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://mymachine/TRP/Trade Trade.xsd ">

<Allocation id="124" portfolio="ABC" quantity="10.0" />

<Allocation id="125" portfolio="ABD" quantity="20.0" />

<Allocation id="126" portfolio="ABE" quantity="20.0" />

<Allocation id="127" portfolio="ABF" quantity="50.0" />

</tns:Trade>

Mapping docs for these tags (Trade and Allocation are given below).

I dont have Trade Id as the instance member in Allocation, nor does my Allocation.hbm.xml has it.
Am new to Hibernate, so dont know if this is the right way to do it.

Pls help!!!

Hibernate version: 3.1

Mapping documents:

This is the Trade.hbm.xml:

<hibernate-mapping>

<class name="demo.Trade" entity-name="Trade">
<id name="id" node="@id">
<generator class="native"></generator>
</id>
<property name="Quantity" column="Quantity" node="@quantity" />
<property name="Security" column="Security" node="@security"></property>

<set name="allocations" table="Allocation" cascade="save-update">
<key column="TradeId"></key>
<one-to-many
class="demo.Allocation" node="Allocation" entity-name="Allocation"/>
</set>
</class>
</hibernate-mapping>

Mapping for Allocation
<hibernate-mapping>

<class name="demo.Allocation" entity-name="Allocation">
<id name="id" node="@id">
<generator class="native"></generator>
</id>
<property name="Quantity" column="Quantity" node="@quantity"></property>
<property name="Portfolio" column="Portfolio" node="@portfolio"></property>
</class>

</hibernate-mapping>

Code between sessionFactory.openSession() and session.close():
session = HibernateUtil.getSessionFactory().openSession();

transaction = session.beginTransaction();
dom4jSession = session.getSession(EntityMode.DOM4J);

Iterator iter = trades.iterator();
while (iter.hasNext())
{
Object next = iter.next();
dom4jSession.save(next);

}// end while



Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 5:49 am 
Newbie

Joined: Fri Aug 11, 2006 1:39 am
Posts: 2
Now i added node="." to my my Trade.hbm.xml, so it saves Trade and tries to update Allocation.

The trace:
Saving trades
Hibernate:
insert
into
Trade
(Quantity, Security)
values
(?, ?) select
scope_identity()
Done
Hibernate:
update
Allocation
set
Quantity=?,
Portfolio=?
where
id=?
Aug 11, 2006 3:17:46 PM org.hibernate.event.def.AbstractFlushingEventListener performExecutions
SEVERE: Could not synchronize database state with session
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at demo.HibernateWriter.saveTrade(HibernateWriter.java:55)
at demo.HibernateWriter.main(HibernateWriter.java:105)
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: SQL Warning: 0, SQLState:
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: [Microsoft][SQLServer 2000 Driver for JDBC]Database changed to TPstatus
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: SQL Warning: 5701, SQLState: 01000
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed database context to 'TPstatus'.
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: SQL Warning: 0, SQLState:
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: [Microsoft][SQLServer 2000 Driver for JDBC]Language changed to us_english
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: SQL Warning: 5703, SQLState: 01000
Aug 11, 2006 3:17:46 PM org.hibernate.util.JDBCExceptionReporter logWarnings
WARNING: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]Changed language setting to us_english.
org.hibernate.StaleStateException: Unexpected row count: 0 expected: 1
at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java:27)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2204)
at org.hibernate.persister.entity.AbstractEntityPersister.updateOrInsert(AbstractEntityPersister.java:2118)
at org.hibernate.persister.entity.AbstractEntityPersister.update(AbstractEntityPersister.java:2374)
at org.hibernate.action.EntityUpdateAction.execute(EntityUpdateAction.java:91)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:248)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:232)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:140)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at demo.HibernateWriter.saveTrade(HibernateWriter.java:55)
at demo.HibernateWriter.main(HibernateWriter.java:105)


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.