-->
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.  [ 8 posts ] 
Author Message
 Post subject: classcastexception saving xml node with a set
PostPosted: Mon Dec 05, 2005 6:41 pm 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
hi there,

i cannot get the dom4jsession to load/import xml file into my database.

Hibernate version:
3

i have a class user that has a collection of roles.
i have an xml file with a bunch of users in it like this:

<Users>
<User>
<userId>admin</userId>
<firstName>adminFirst</firstName>
<lastName>adminLast</lastName>
<password>sof</password>
<roles>
<Role>admin</Role>
<Role>manager</Role>
</roles>
</User>

Session session = HibernateUtil.currentSession();
Session dom4jSession = session.getSession(EntityMode.DOM4J);
SAXReader reader = new SAXReader();
InputStream is = ...
Document document = reader.read(is);
Element root = document.getRootElement();

for ( Iterator i = root.elementIterator("User"); i.hasNext(); ) {
Element userElement = (Element) i.next();
dom4jSession.save("com.foo.User", userElement);
}

session.commit();

Full stack trace of any exception that occurs:
java.lang.ClassCastException: org.dom4j.tree.DefaultElement
at org.hibernate.event.def.DirtyCollectionSearchVisitor.processCollection(DirtyCollectionSearchVisitor.java:51)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:101)
at org.hibernate.event.def.AbstractVisitor.processValue(AbstractVisitor.java:61)
at org.hibernate.event.def.AbstractVisitor.processEntityPropertyValues(AbstractVisitor.java:55)
at org.hibernate.event.def.DefaultFlushEntityEventListener.hasDirtyCollections(DefaultFlushEntityEventListener.java:404)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:392)
at org.hibernate.event.def.DefaultFlushEntityEventListener.isUpdateNecessary(DefaultFlushEntityEventListener.java:181)
at org.hibernate.event.def.DefaultFlushEntityEventListener.onFlushEntity(DefaultFlushEntityEventListener.java:104)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEntities(AbstractFlushingEventListener.java:195)
at org.hibernate.event.def.AbstractFlushingEventListener.flushEverythingToExecutions(AbstractFlushingEventListener.java:76)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:26)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:870)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:349)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)


any help is greatly appreciated.

thanks.


Top
 Profile  
 
 Post subject: saving xml
PostPosted: Mon Dec 05, 2005 11:06 pm 
Newbie

Joined: Tue Nov 29, 2005 2:43 am
Posts: 9
I am using hibernate 3.0.5 and I can import/export sucessfully. Firstly, I imagine your database is empty when you are importing?
Map the user-role joint table to be as follows;

Code:
<many-to-one name="role" column="role_id" not-null="true"/>

<many-to-one name="user" column="user_id" not-null="true"/>


This will include the parent user & role entities into your user-role joint object xml.
In User and Role object mapping make sure you have turned off the xml using embed-xml="false".
When import, make sure that you import in the sequence of User, Role and User-Role. Also, user the session.replicate() to do the insert or update to the database. Hope this help you.

Quote:
Don't forget to rate if this solution works


Top
 Profile  
 
 Post subject: Re: saving xml
PostPosted: Tue Dec 06, 2005 11:28 am 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
yes, my db is empty when importing. i am using hibernate 3.1rc3. not sure if that makes a difference. i looked at the source code where the exception was thrown, it's looking for a persistent collection and my object is a dom4j element. maybe the code is broken somehow?

i am loading roles first, followed by session flush, then loading users.

below is my mapping file:

user:

<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.User"
table="fos_user"
>

<id
name="userId"
column="user_id"
type="java.lang.String"
length="256"
>
<generator class="assigned">
</generator>
</id>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="first_name"
length="64"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="last_name"
length="64"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
length="64"
/>

<set
name="roles"
table="fos_user_roles"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="user_id"
>
</key>

<many-to-many
class="com.foo.Role"
column="user_role"
outer-join="auto"
embed-xml="false"
/>

</set>
</class>

</hibernate-mapping>


role:

<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.Role"
table="fos_role"
lazy="false"
>
<id
name="name"
column="name"
type="java.lang.String"
length="64"
>
<generator class="assigned">
</generator>
</id>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
length="256"
/>
</class>
</hibernate-mapping>

firebug wrote:
I am using hibernate 3.0.5 and I can import/export sucessfully. Firstly, I imagine your database is empty when you are importing?
Map the user-role joint table to be as follows;

Code:
<many-to-one name="role" column="role_id" not-null="true"/>

<many-to-one name="user" column="user_id" not-null="true"/>


This will include the parent user & role entities into your user-role joint object xml.
In User and Role object mapping make sure you have turned off the xml using embed-xml="false".
When import, make sure that you import in the sequence of User, Role and User-Role. Also, user the session.replicate() to do the insert or update to the database. Hope this help you.

Quote:
Don't forget to rate if this solution works


Top
 Profile  
 
 Post subject: Re: dom4j session failed to save xml with optimistic locking
PostPosted: Tue Dec 06, 2005 6:46 pm 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
i found the problem: my classes use optimistic locking. that's why DirtyCollectionSearchVisitor is in the stack trace. i tried to remove optimistic locking. the code works now. but i really need versioned objectsl. could someone provide a quick fix?

ps in the hbm snippet in my original post, i deleted optimistic locking 'cause i thought it's noise. apparently it's not. [/quote]


Top
 Profile  
 
 Post subject: saving xml
PostPosted: Tue Dec 06, 2005 8:34 pm 
Newbie

Joined: Tue Nov 29, 2005 2:43 am
Posts: 9
Can't you use the <version> tag in the mapping file.

Code:
<version name="version" unsaved-value="null" node="version"/>


This will version your objects automatically.


Top
 Profile  
 
 Post subject: Re: saving xml
PostPosted: Wed Dec 07, 2005 11:18 am 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
that's right. but with a versioned object, saving xml will fail. you can try it with your application where there is a class with a one-to-many (collection) relationship to another class, both are versioned.

my mapping files are the following:

<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.User"
table="fos_user"
optimistic-lock="version"
>

<id
name="userId"
column="user_id"
type="java.lang.String"
length="256"
>
<generator class="assigned">
</generator>
</id>

<version
name="version"
column="version"
type="int"
/>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="first_name"
length="64"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="last_name"
length="64"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
length="64"
/>

<set
name="roles"
table="fos_user_roles"
lazy="false"
cascade="none"
sort="unsorted"
>

<key
column="user_id"
>
</key>

<many-to-many
class="com.foo.Role"
column="user_role"
outer-join="auto"
/>

</set>
</class>

</hibernate-mapping>


<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.Role"
table="fos_role"
lazy="false"
optimistic-lock="version"
>
<id
name="name"
column="name"
type="java.lang.String"
length="64"
>
<generator class="assigned">
</generator>
</id>
<version
name="version"
column="version"
type="int"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
length="256"
/>
</class>
</hibernate-mapping>


firebug wrote:
Can't you use the <version> tag in the mapping file.

Code:
<version name="version" unsaved-value="null" node="version"/>


This will version your objects automatically.


Top
 Profile  
 
 Post subject: Re: saving xml
PostPosted: Wed Dec 07, 2005 7:38 pm 
Newbie

Joined: Tue Nov 29, 2005 2:43 am
Posts: 9
No, I have the same situation as yours but it works fine. However, I had a problem with many-tomany relationship when generating the xml. So, I changed it to include the extra mapping for user-role and put two many-to-one relationships for Role and the User. Have a look at the other changes I have done in your following mapping. Take note that I have removed your many-to-many relationship.
By the way, I thought you got rid of "optimistic-lok". Anyway,


weiqingh wrote:
that's right. but with a versioned object, saving xml will fail. you can try it with your application where there is a class with a one-to-many (collection) relationship to another class, both are versioned.

my mapping files are the following:

<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.User"
entity-name="com.foo.User"
table="fos_user"
node="User"
optimistic-lock="version"
>

<id
name="userId"
column="user_id"
type="java.lang.String"
node="userId"
length="256"
>
<generator class="assigned">
</generator>
</id>

<version
name="version"
column="version"
type="int"
node="version"
/>

<property
name="firstName"
type="java.lang.String"
update="true"
insert="true"
column="first_name"
length="64"
node="firstName"
/>

<property
name="lastName"
type="java.lang.String"
update="true"
insert="true"
column="last_name"
length="64"
node="lastName"
/>

<property
name="password"
type="java.lang.String"
update="true"
insert="true"
column="password"
length="64"
node="password"
/>

<set
name="roles"
table="fos_user_roles"
lazy="false"
cascade="none"
sort="unsorted"
embed-xml="false"
>

<key
column="user_id" embed-xml="false"
>
</key>

</set>
</class>

</hibernate-mapping>


<?xml version="1.0" encoding="UTF-8"?>

<!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.foo.Role"
entity-name="com.foo.Role"
table="fos_role"
lazy="false"
optimistic-lock="version"
node="Role"
>
<id
name="name"
column="name"
type="java.lang.String"
length="64"
node="name"
>
<generator class="assigned">
</generator>
</id>
<version
name="version"
column="version"
type="int"
node="version"
/>
<property
name="description"
type="java.lang.String"
update="true"
insert="true"
column="description"
length="256"
node="description"
/>
</class>
</hibernate-mapping>


firebug wrote:
Can't you use the <version> tag in the mapping file.

Code:
<version name="version" unsaved-value="null" node="version"/>


This will version your objects automatically.


Top
 Profile  
 
 Post subject: Re: saving xml
PostPosted: Thu Dec 08, 2005 5:08 pm 
Beginner
Beginner

Joined: Wed Mar 16, 2005 4:07 pm
Posts: 22
thanks for the reply. i meant in my post that it failed with a many-to-many relationship, which is what i have. i was able to export to xml with no problem. but importing the xml back to hibernate gave me the classcastexception in DirtyCollectionSearchVisitor. like i said, if i comment out the optimistic locking/version part, then it would work.

i think i will file a bug when i get some time: importing xml to a versioned class that has a many-to-many relationship to another class throws class cast exception.


[quote="firebug"]No, I have the same situation as yours but it works fine. However, I had a problem with many-tomany relationship when generating the xml. So, I changed it to include the extra mapping for user-role and put two many-to-one relationships for Role and the User. Have a look at the other changes I have done in your following mapping. Take note that I have removed your many-to-many relationship.
By the way, I thought you got rid of "optimistic-lok". Anyway,


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 8 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.