DOM4J and Hibernate: Dealing with the XML - java

So my web application is primarily using XML for client to server interaction and I'm currently persisting most of my backend using hibernate. I know there are XML databases and there that you can save XML using hibernate by invoking Sessions with the DOM4J entity but I'm not sure what the most efficient way of serving up the XML really is. At the moment each time an object is request I generate an XML document from the object fields then serve it up. So for each new request I generate a whole new XML document. So I could generate the XML for each document during each runtime cycle the first time it is requested then store it in a field for the object so I can then run XSLT command against it but this seems kind of inefficient. I'm guessing its more efficient to generate a new Document object each time the resource is request and then drop it after the request has been served(and use Hibernate Query Language for selection)... Or should I persist xml using Hibernate or eXist?(I really don't want to use an xml database!)

You can store the XML as a CLOB or BLOB in the database. If you don't need to look inside the document when you query, you can just externalize the key fields and query for the XML based on those.

One of the main purposes of a relational database is to avoid duplication. If you have objects that are shared between documents, and you store that in XML in each document, you'd have to update all documents when you change the shared object.
It is pretty standard practice to store your document object fields in a normal relational way using hibernate, and use some XML marshaller to convert it to XML and back, e.g. xstream or CXF.

Related

How to properly generate xml in Hibernate framework

I am using MS SQL database server for data and I have Hibernate framework for db mapping. I need to generate specific data (select ... from...) to the xml but have no idea how. I tried searching online but nothing...
I have 2 ideas but you may advice more experienced approach to this problem.
Generate xml at db layer with usage of FOR XML within the select. -> here I dont know what would be the result from select in hibernate...? String? dont know.
Retrieve list from DB and then use java to convert this list into xml, for example using this> https://www.javatpoint.com/jaxb-marshalling-example
What do you suggest? Thanks
There are many different parameters that could be considered as a basis to decide about the best approach (e.g. Is it important to be database independent? Is there any xml schema provided as a basis to generate the output? How the output is going to be used/accessed?, etc.)
If the output you're going to generate is something like the plain dump of the data and it would be used in future to import data into the database (or similar usages), then maybe just generating the output in database layer would be enough. But most of the time this is not the case. I highly recommend to generate the output in the application layer so that you would have a better control over the way it's going to be produced and customized later. You can fetch data using normal hibernate query and use (as you mentioned) JAX-B to serialize it into XML.
I wouldn't generate the XML at the database level. Since you're already using Hibernate, you can turn your entity object into a data transfer object (DTO), which is basically the same object, but this time intended to be marshalled and unmarshalled by library. Rather than JAXB, you may want to look at Jackson, which is a bit easer to use (Google for xml jackson), and if somebody ever decides that the output should be in JSON or YAML instead, it's very easy to change.

How to persist the change in JAXB object to its corresponding XML file?

I am new to JAXB.
Server is sending a few xmls which need to be replaced in the client. My requirement is to back up some element's data from previous XML tables' JAXB objects and update into the new xmls' object once they are successfully replaced in the client.
I don't have problem in backing up the data but while updating the data, only JAXB object is updated while I need to persist in the corresponding XMLs too. Is there any way JAXB supports to accomplish this?

Conversion of Java Objects to XML for OptaPlanner Input

I need to convert java objects being imported from the dB to the XML so that I could user it with Xstream in OptaPlanner. Is there any alternate way other than Hibernate to access the data from the dB. How to add more attributes for job scheduling.
optaplanner-core works based on POJO's (javabeans). It's oblivious to the fact that in optaplanner-examples, those POJO's are being read/written to XML files by XStream (and it doesn't care). Similarly, you can use any other technology to store those POJO's:
JPA (for example Hibernate-ORM, OpenJPA, ...) to store them into a database
JDBC to store them into a database. Note: JDBC works with SQL statements, so you 'll need to manually map SQL records to POJO's.
JAXB to store them in XML
XStream to store them in XML (as the examples do it)
Infinispan, mongodb, ... to store them in to a big data cloud. Note: might require manually mapping too, unless you use hibernate-ogm
...
OptaPlanner doesn't care, so it doesn't restrict you :)

insert database(DB2) tuples as XML elements in XML file using java?

how to insert database(DB2) tuples as XML elements in XML file using java?
Is there any possibility to retrieve XML elements which were entered earlier as database tuples?? or can they be used to provide a view customized to different users.
Although it would help to see a bit of an example of what you're trying to accomplish, I am fairly certain that a couple of different XML features in DB2 (referred to collectively as pureXML) can help your application convert smoothly between XML documents and relational data.
Publishing tuples/rows as XML is done with SQL/XML functions such as XMLELEMENT, XMLATTRIBUTE, XMLFOREST, XMLAGG, and XMLSERIALIZE, to name a few. These functions have been available since DB2 V8.1, when they were introduced as part of the SQL:2003 spec. Other DBMS vendors support these functions in their products, too. To produce more sophisticated XML constructs, such as hierarchical data relationships and repeating elements, you will probably want to exploit common table expressions that use XMLAGG or XMLGROUP.
XML data can be stored natively in DB2 v9.1 and newer by using the XML datatype, which produces a column that accepts any well-formed XML input. If you instead want to decompose/shred the inbound XML into one or more columns of a relational table, the XMLTABLE function takes in an XML document and your XPath expressions to convert the relevant nodes into a traditional result set that can be referenced by a SQL insert statement.

create fixture using hibernate(not import.sql method)

Is there a way to use java code and those hibernate entity classes to create fixture data.
I find using import.sql is not good when it comes to database migration
Since you are using hibernate, the way to create sample data is to use hibernate itself to create it. You have two options:
generate random data - look from 1 to X, instantiate entities and set random values, then save each entity
predefined data - use some meta format, like .properties, .json or .xml to define field=value pairs, parse that file, instantiate entities for each record, set the fields, and save it.
That way your data generation is tied to your entity model, and when the model changes, your data won't become out-of-sync.

Categories

Resources