-->
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.  [ 5 posts ] 
Author Message
 Post subject: Hibernate 3.0 export/import xml
PostPosted: Tue Mar 22, 2005 12:03 pm 
Newbie

Joined: Tue Mar 22, 2005 10:44 am
Posts: 8
Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp

I have some problems with xml export/import in Hibernate Version 3.0

...
<class name="admin.usermanagement.User" table="user" node="user">
<id name="userId" column="user_id" type="long">
<generator class="increment"/>
</id>
<property name="firstName" column="first_name" node="firstname"/>
<property name="lastName" column="last_name" node="lastname"/>
...
</class>
...

for xml export, the java code looks like this:

Transaction transaction = session.beginTransaction();
List users = dom4jSession.createQuery("from User").list();
...
Document doc = DocumentHelper.createDocument();
Element root = doc.addElement("usertable");
Iterator it = users.iterator();
while( it.hasNext() ){
Element elem = (Element) it.next();
root.add(elem);
}
...

the xml result of this export looks like this

<usertable>
<user>
<userId>1</userId>
<firstname>John</firstname>
<lastname>Clinton</lastname>
...
</user>
<user>
<userId>2</userId>
<firstname>Marc</firstname>
<lastname>Twain</lastname>
...
</user>
</usertable>

My first question: why do I get userId in the xml output ?
I can't use it for direct import because userId is defined to be auto generated.
I think, if I remove userId, this is the xml which i can import now.
So, for import, the java code looks like

SAXReader reader = new SAXReader();
Document doc = reader.read(file);
...
Session dom4jSession = session.openSession(EntityMode.DOM4J);
Transaction transaction = session.beginTransaction();
Iterator it = doc.selectNodes("//user").iterator();
while ( it.hasNext() ){
dom4jSession.saveOrUpdate(it.next());
}
transaction.commit();

This doesn't work.
I get an Exception: Unknown entity: user !?!?!?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Mar 23, 2005 3:54 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
Quote:
why do I get userId in the xml output ?

because userId is a property of your User mapping.

Quote:
I can't use it for direct import because userId is defined to be auto generated.

No idea what you are talking about. You just loaded it from the database. Why would you want to have it insert a new row with a new id? Is this a different database?

Quote:
I get an Exception: Unknown entity: user !?!?!?

Sure, because user is not the name of the entity !?!?!?!?! Your entity is named "admin.usermanagement.User". So use
Code:
dom4jSession.saveOrUpdate( "admin.usermanagement.User", it.next() );


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 24, 2005 5:05 am 
Newbie

Joined: Tue Mar 22, 2005 10:44 am
Posts: 8
why do I get userId in the xml output ?
because userId is a property of your User mapping.
okay, I thought this property would not be exported if I map no node to it.
How can I define whether a database field should be exported or not ? Only in my java code ?


I can't use it for direct import because userId is defined to be auto generated.
No idea what you are talking about. You just loaded it from the database. Why would you want to have it insert a new row with a new id? Is this a different database?
Yes, I would like to load this data on a different database with autogenerated userId again.

I get an Exception: Unknown entity: user !?!?!?
Sure, because user is not the name of the entity !?!?!?!?! Your entity is named "admin.usermanagement.User". So use
dom4jSession.saveOrUpdate( "admin.usermanagement.User", it.next() );

Thank you, great, now it works ;-)

Thank you very much for your fast reply !!!!!!! ;-)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Mar 31, 2005 2:13 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
I get an Exception: Unknown entity: user !?!?!?
Sure, because user is not the name of the entity !?!?!?!?! Your entity is named "admin.usermanagement.User". So use
dom4jSession.saveOrUpdate( "admin.usermanagement.User", it.next() );

Thank you, great, now it works ;-)


Can you share the code you used to import this XML document ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 04, 2005 4:13 am 
Newbie

Joined: Tue Mar 22, 2005 10:44 am
Posts: 8
pksiv wrote:
I get an Exception: Unknown entity: user !?!?!?
Sure, because user is not the name of the entity !?!?!?!?! Your entity is named "admin.usermanagement.User". So use
dom4jSession.saveOrUpdate( "admin.usermanagement.User", it.next() );

Thank you, great, now it works ;-)


Can you share the code you used to import this XML document ?



Yes, of course:

...
import java.util.Iterator;
import java.util.List;
import java.io.File;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
...

String filename = ...;
File file = new File(filename);
SAXReader reader = new SAXReader();
Document doc = reader.read(file);
List users = doc.selectNodes( "//user" );

try {
Session session = sessionFactory.openSession();
Session dom4jSession = session.openSession(EntityMode.DOM4J);
Transaction transaction = session.beginTransaction();
Iterator it = users.iterator();
while ( it.hasNext() ){
dom4jSession.saveOrUpdate( "admin.usermanagement.dto.User", it.next());
}
transaction.commit();
session.close();
}
catch (HibernateException e) {
...
}

The xml-Document looks like described above. admin.usermanagement.dto.User is my POJO (simple Javabean with properties, getter- and setter-Methods)


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