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.  [ 3 posts ] 
Author Message
 Post subject: Need help implementing a data-based XML import
PostPosted: Mon Oct 24, 2005 10:05 am 
Newbie

Joined: Mon Oct 24, 2005 9:16 am
Posts: 7
I'm new to hibernate and I'm working on a project where I have to create a new database as well, so I'm using a forward engineering approach from classes to tables.

To create and load the database with test data at the same time, I created a class with a recursive method that reads a XML file, create the database (done automatically with the hbm2ddl.auto property) and populate the tables with data.

Here is the class that load the database:


Code:
public class TourManager {
   private Session s2;
   private Element rootElement;
   private String domain="tour";
   
   public static void main(String[] args) {
      TourManager mgr = new TourManager();
      mgr.loadDatabaseFromXml();
   }
   
   public void loadDatabaseFromXml() {
      Session session = HibernateSessionFactory.currentSession();
      s2 = session.getSession(EntityMode.DOM4J);
      InputStream in;
      try {
         in = new FileInputStream("f:\\tourdb.xml");
      } catch (IOException e) {
         throw new IllegalArgumentException("Could not parse XML document",e);
      }
      SAXReader reader = new SAXReader();
      Document document = null;
      try {
         document = reader.read(in);
      } catch (DocumentException e) {
         e.printStackTrace();
      }
      rootElement = document.getRootElement();
      System.out.println("Root Element="+rootElement.getName());
      recurLoadXml(rootElement);

   }
   public void recurLoadXml(Element elem) {
         Iterator j = elem.elementIterator();
         while (j.hasNext()) {
            Element e=(Element) j.next();
            recurLoadXml(e);
            s2.replicate(domain+"." + elem.getName(), elem, ReplicationMode.OVERWRITE);
         }
   }
}


The XML file looks like this:

Code:
<?xml version ="1.0" encoding="ISO-8859-1"?>
<TourDb>
   <id>1</id>
   <Tour>
      <id>1</id>
      <country>Mexico</country>
      <destination>Cancun</destination>
      <tourname>PARADISE REEF ADVENTURE</tourname>
      <categories>
         <elt>Adventure</elt>
         <elt>Snorkling</elt>
      </categories>
   </Tour>
   <Tour>
      <id>2</id>
      <country>Mexico</country>
      <destination>Cancun</destination>
      <tourname>INDIANA JOE`S ATV AND SPEED BOAT</tourname>
      <categories>
         <elt>Adventure</elt>
         <elt>Boat Ride</elt>
      </categories>
   </Tour>
</TourDb>


The mapping file looks like this:

Code:
<hibernate-mapping package="tour">
  <class name="Tour" table="tour" optimistic-lock="none">
    <id name="id" type="integer" unsaved-value="null" column="id">
      <generator class="native"/>
    </id>
    <property name="tourid" type="long" column="tourid"/>
    <property name="country" type="string" column="country"/>
    <property name="destination" type="string" column="destination"/>
    <property name="tourName" type="string" column="tour_name"/>
    <array name="categories" table="tour_categories" cascade="none">
      <key foreign-key="categories_id" column="categories_id"/>
      <list-index>
        <column name="idx" not-null="true"/>
      </list-index>
      <element type="string" column="elt"/>
    </array>
  </class>
</hibernate-mapping>


This works if categories are in a separate class/table. (Array would be replaced by a one-to-many tag in this case) But I want to store the categories in the tour_category table (generated by hb2ddl).

It looks like the replicate method only replicate entities, not relationships.

Any suggestion ?


Top
 Profile  
 
 Post subject: Problem Solved !
PostPosted: Tue Oct 25, 2005 12:20 pm 
Newbie

Joined: Mon Oct 24, 2005 9:16 am
Posts: 7
I made some modifications to the recursive function:

Code:
public void recurLoadXml(Element elem) {
      if (!elem.isTextOnly()) {
         if (elem.getParent()!=null ) {
            if( !elem.getParent().getName().equals(rootElement.getName())) {
               String sqlst="INSERT INTO  "+elem.getName().toLowerCase()+"_"+elem.getParent().getName().toLowerCase()+" VALUES ("+elem.getParent().elementText("id")+", " +
               elem.elementText("id")+")";
               System.out.println(sqlst);
               Connection con =s2.connection();
               try {
                  PreparedStatement res = con.prepareStatement(sqlst);
                  res.execute();
               } catch (SQLException e) {
                  e.printStackTrace();
               }   
            }
         }
         Iterator j = elem.elementIterator();
         while (j.hasNext()) {
            Element el=(Element) j.next();
            recurLoadXml(el);
         }
         s2.replicate(domain+"." + elem.getName(), elem, ReplicationMode.OVERWRITE);
      }
   }


Replicate does not process relationships so I added a native SQL to insert keys in children tables.


Top
 Profile  
 
 Post subject: Re: Problem Solved !
PostPosted: Tue Oct 25, 2005 12:46 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Yves wrote:
Replicate does not process relationships so I added a native SQL to insert keys in children tables.


Replicate should process relationships if cascade="replicate" is specified.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


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