2 Replies Latest reply on May 26, 2007 4:57 AM by pattchen

    Hibernate and XML persistence implementation

    pattchen

      Hi,
      I would like to know if Hibernate can persist objects to XML using the same pojo classes as database persistence classes?
      In fact,I want to persist/retrieve plain old java objects to/from an xml store which conforms to a given schema. This schema defines foreign key,but objects use aggregation like in hibernate mapping.

      Let me explain you the problem behind this problem.I've used hibernate 3.2 to persist some objects to a relational database.Now I want to persist/retrieve these objects to/from a xml store which is an instance of a schema. This xml schema is in fact an xml version of the database schema (so foreign and primary keys are also defined). Of course, I don't want to modify existing pojo classes. I just want another persistence(an xml one) implementation.

      Does Hibernate provide this?If not,is there another solution out there?

      Thanks.

        • 1. Re: Hibernate and XML persistence implementation
          andydale

          Hi,

          You will need to use a Hibernate session, with the EntityMode set to DOM4J. You should hopefully be able to extract the data and store it into an xml document, but you will have to set up all the mappings in hbm.xml files (I think)

          Thanks,

          Andy

          • 2. Re: Hibernate and XML persistence implementation
            pattchen

            Thanks for your answer Andy,

            I've explored documentation about Hibernate and XML.It seems that I must open a database session before open a DOM4J session. Moreover,It is not possible to retrieve objects from an xml storage.
            Here is an example of what i want to achieve:

            /**XMLSession is an imaginary class(or interface) which is used to manipulate an xml doc.
            The factory is configured in properties file where we specify the xml file to handle.Just like we specify database parameters in hibernate.properties
            */
            
            //Store an object to an XML file
            XMLSession xmlSess = factory.getSession();
            Employee e = new Employee();
            e.setName("Andy");
            e.setAge(25); //too much or too low?? :-)
            Employee e1 = new Employee();
            e1.setName("King");
            e1.setAge(35); //too much or too low?? :-)
            
            Company c = new Company ();
            c.setName("Future Software");
            c.setLocation("Cameroon");
            c.add(e);
            c.add(e1);
            
            xmlSess.store(c);//persist company object and his employees
            
            //Retrieve an object and update it
            Employee who = xmlSess.load(Employee.class, 1);//1=id
            who.setAge(30);
            xmlSess.update(who);
            
            xmlSess.close();//certainly release i/o resource??


            So,I just want to replace database persistence by an xml file and create a mapping file between classes and the xml schema.

            Is it possible with Hibernate? if not,is there another solution out there?

            Thanks again.