Reputation:
I am looking for a solution to save a XML-File in a database using Hibernate.
The problem is, that the structure of the XML-file is different to the Hibernate-beans.
I'm trying to use JAXB to serialize the XML-content to the Hibernate beans.
Please imagine the following scenario: There is this xml file:
<root>
<general>
<property1/>
<property2/>
</general>
<details>
<detail1>
<detail2>
</root>
Now I want to save property1 and detail1 in one bean:
@Entity
@Table(name = "tablename")
class Bean(){
public String property;
public String detail;
//+ getters and setters ...
}
Has anyone an idea, which JAXB annotations I could use to solve that problem?
Upvotes: 3
Views: 7812
Reputation: 1209
I have just done the same kind of of solution what i did was use a mix of hibernate and XStream with xstream i used the annotation XStreamAlias("") on my class and fields and mapped them to the actual xml document and run them via xstream.fromXML() and its creates the beans then the beans are inserted into the database
some code
@XStreamAlias("task")
public class Task {
@XStreamOmitField
private Long id;
@XStreamAlias("subject-type")
private String subject;
@XStreamAlias("body")
private String body;
@XStreamAlias("frame")
private String timeframe;
@XStreamAlias("due-at")
private String due;
@XStreamAlias("alert-at")
private String alarm;
/** Getters/Setters **/
}
<?xml version="1.0"?>
<tasks>
<task>
<subject-type></subject-type>
<body>A task for today</body>
<frame>today</frame>
<due-at type="datetime"></due-at>
<alert-at type="datetime"></alert-at>
</task>
</tasks>
// Test Case
File xml = new File("xmlfile.xml");
XStream xstream = new XStream();
xstream.processAnnotations(new Class[]{ Task.class });
xstream.alias("tasks", ArrayList.class);
List tasks = (List)xstream.fromXML(new FileReader(xml));
Upvotes: 1
Reputation:
It looks like you are trying to solve the wrong problem.
In short databases can store XML as either a CLOB or and XML datatype (basically an extension of a clob)
In which case you would map an appropriate LOB handler to a string or byte[] field (representing the full xml document) in your hibernate object.
In your case however, it looks like you want to take marshal/unmarshal a bean to and from XML... which can be done by a multitude of frameworks/libraries; and really doesnt matter if you are using hibernate or not.
Just create some static util for the conversion process.
Upvotes: 1
Reputation: 11316
Is it mandatory to do it that way? Hibernate is a persistence API which is intented not to care about how data is stored in no matter what DBMS engine or XML files. It is a framework to persist your objects. But it does not stop there, since you can run queries (pseudo SQL or through Criteria API), what results altogether in fact in an objet oriented data storage.
JAXB is a different monster, since it is intended to persist the objects in XML format, defined by a XSD-Schema, so resulting data can be exchanging between systems sharing the same model.
If storing your data in XML format is important for you, since you work with XML, maybe it has more sense to use a XML native database such as eXist, which lets you access and transform your XML data in a XML-friendly way (XSLT, XQuery, XPath).
Upvotes: 1