-->
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.  [ 1 post ] 
Author Message
 Post subject: hibernate + xml-to-db mapping
PostPosted: Fri Aug 15, 2008 8:17 am 
Newbie

Joined: Fri Aug 15, 2008 8:05 am
Posts: 1
Hi! I can not save xml tree to db :(

I have xml like <property name="a"><property name="b"><property name="c"/></property></property>

Hibernate3 can map xml to db. I have created class Property with fields name (id) and parent (many to one)
and wrote this mappind:

Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="ru.props">
<class name="Property"
   table="property"
   node="property">
   <id name="name"
      type="string"
      node="@name"
      column="name">
   </id>
        <many-to-one name="parent" class="Property" column="parent" node="." embed-xml="true" />
</class>
</hibernate-mapping>


node="." means parent node as referenced in document...

I've exported schema to db (all is ok)
But when I parse xml and save it to db I got pairs [a, a], [b, b] instead [b, a] (a - parent node)

May be smth wrong with this mapping?

I've tested with Hibernate 3.3.0 CR2 and 3.2.6 GA

---------------------

Property.java
Code:
@Entity
public class Property implements Serializable {
    private static final long serialVersionUID = 1L;
    private String name;
    private Property parent;

    public void setName(String name) {
        this.name = name;
    }

    @Id
    @Column(name="name")
    public String getName() {
        return name;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (name != null ? name.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Property)) {
            return false;
        }
        Property other = (Property) object;
        if ((this.name == null && other.name != null) || (this.name != null && !this.name.equals(other.name))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "ru.props.Property[name=" + name + ";parent=" + parent + "]";
    }

    @ManyToOne
    public Property getParent() {
        return parent;
    }

    public void setParent(Property parent) {
        this.parent = parent;
    }
   
}



Servlet code to process:
Code:
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            Configuration config = new Configuration();           
            config.configure(new File("D:\\My Documents\\NetBeansProjects\\WebApplication1\\web\\WEB-INF\\hibernate.cfg.xml"));
            config.addFile("D:\\My Documents\\NetBeansProjects\\WebApplication1\\web\\WEB-INF\\Property.hbm.xml");
            SessionFactory sessionFactory = config.buildSessionFactory();
            Session session = sessionFactory.openSession();
            org.hibernate.Transaction tx = session.beginTransaction();
            Session dom4jSession = session.getSession(EntityMode.DOM4J);
            SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File("c:\\Hibernate\\property.xml"));                         
            // take second node
            Iterator iter = ((Element)document.getRootElement().elementIterator().next()).elementIterator();
            while (iter.hasNext()) {
                org.dom4j.tree.DefaultElement el =  (DefaultElement) iter.next();
                out.println(el.getParent()+" : "+el);
                dom4jSession.saveOrUpdate("ru.props.Property", el);
            }
            session.flush();
            tx.commit();
            session.close();

        } catch (DocumentException ex) {
            Logger.getLogger(NewServlet.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            out.close();
        }
    }


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

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.