-->
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.  [ 9 posts ] 
Author Message
 Post subject: Import/Export of XML data with a database
PostPosted: Thu Sep 22, 2005 12:57 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
I'm using Hibernate 3.1 with Annotations.

Anybody know of a quick, elegant solution for exporting database data to XML? I would prefer to use my existing POJO -> HBM mappings to transparently describe the XML entities. The inverse would be useful as well (importing XML data into a database).

I've heard of XMLDatabinder which AFAIK is for Hibernate 2 only. Is there anything similar in Hibernate 3.1?

Thanks in advance...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 2:14 pm 
Expert
Expert

Joined: Fri Aug 19, 2005 2:11 pm
Posts: 628
Location: Cincinnati
I've never looked in hibernate for this kind of functionality, but the dbunit project on sourceforge exports and imports data to and from (in xml documents) the database.

It does not use your hbm files, but it has worked ok for me.

_________________
Chris

If you were at work doing this voluntarily, imagine what you'd want to see to answer a question.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 2:15 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
You can switch Hibernate to EntityMode.DOM4J and export the resulting documents to a file.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 3:46 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Trying this, and I'm getting a "No tuplizer found for entity-mode [DOM4J]" error.

I suspect I need to add XML-entity annotations to my annotated POJO???? I've tried searching but I can't find where the XML-entity annotation extensions are (if they in fact exist).

Here's the annotated POJO:
Code:
@Entity (access = AccessType.FIELD)
public class State
  implements java.io.Serializable
{
@Id (generate = GeneratorType.NONE)
@Column (length = 2)
protected String key ;

@Column (length = 64)
protected String name ;

public State (String key, String name)
{
  this.key = key ;
  this.name = name ;
}   
   
public String getKey () { return this.key ; }
public void setKey (String p) { this.key = p ; }
...
     
}   


Here's the data access code:
Code:
public static void main (String [] args)
{               
  Transaction tx      = null ;
  Session     session = null ;
               
  try {     
    Document document = DocumentHelper.createDocument();
    Element root = document.addElement ("root");
           
    Configuration  configuration = new AnnotationConfiguration() ;
    configuration.configure() ;
    SessionFactory factory = configuration.buildSessionFactory();
           
    session = factory.openSession();
    Session dom4jSession = session.getSession (EntityMode.DOM4J) ;
    tx = dom4jSession.beginTransaction () ;
               
    Element element = (Element) dom4jSession.get (State.class, "NY") ;
    root.add (element) ;
   
    document.write (new FileWriter ("test.xml")) ;
  }
  catch (Exception e) {
    e.printStackTrace () ;
  }
  finally {
    if (tx != null)
      tx.commit () ;

    if (session != null)
      session.close () ;
  }
}


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 4:01 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
It's in the documentation...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 22, 2005 4:49 pm 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Hrm... maybe I'm looking in the wrong places?

So far, I've only seen examples of
Code:
session.getSession(EntityMode.DOM4J)
in the context of Hibernate 3.0.x using hbm.xml files. I haven't seen any documentation on XML import/export using Hibernate 3.1 and Annotations.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 10, 2007 2:15 pm 
Newbie

Joined: Fri Aug 31, 2007 4:35 pm
Posts: 4
Has anyone seen how to do this?

The only documentation I've seen is

http://www.hibernate.org/hib_docs/reference/en/html/xml.html

which uses a mapping file.

I'm about to the point of using JAXB instead... :-(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 12:05 pm 
Newbie

Joined: Sat Jan 05, 2008 12:03 pm
Posts: 1
Hey,

Anything new about this thread?



Thanks,

Asaf.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jan 05, 2008 10:19 pm 
Newbie

Joined: Wed Aug 08, 2007 11:22 pm
Posts: 2
I've been struggling with this problem for a while but had to give up for the several reasons:

  • Memory. I could not figure out how to hook up SAX; DOM approach blows up memory really fast on large tables.
  • Formatting issues. Suppose you export your database on one server, import on another, with a different locale - then you get in trouble with date/time formats, number formats, encodings - all that information better be saved with the XML file and travel with it, or at least, with some accompanying file which isn't too good because you have to manage more than one piece.

It takes insane amount of time to find any relevant information in the hibernate documentation, even in those hibernate books many of which are misleading, and even at 800+ pages still very basic. In any case you are obliged to roll at least some of this infrastructure of your own, why not roll the whole thing out to be safe.


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