XML to database with Hibernate - java

I have given a XML file. I want to store a subset of the content of this file in a relational database.
I could use a framework like JAXB to convert the content of the xml file into Java POJOs and write them to database (e.g. with Hibernate).
But is there a built-in opportunity in Hibernate to do this without using a additional framework like JAXB?
Thanks for your help!!

As far as I remember, there was an XML mapping feature in Hibernate 3:
https://docs.jboss.org/hibernate/orm/3.3/reference/en/html/xml.html
However this feature was noted as "experimental". And I don't see it in Hibernate 4 or 5:
https://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/
http://docs.jboss.org/hibernate/orm/5.0/userGuide/en-US/html_single/

Related

Does Hibernate 5 support XML based entity mappings?

Hi my question is not technical. I want to know if hibernate 5 supports XML based entity mapping or not.
My requirement is like below :
I want a backend on Jersey-Hibernate. My front end is Android application.
I want to keep my Entity POJOs in a common project such that they will be shared by both app and server. Gson will serialize/deserialize both side.
But because android application doesn't support all java library in compilation, I don't want my common(POJO) depending on some library; In this case hibernate annotations. So I am planning to use XML based configuration.
Hibernate 5 supports both the legacy HBM mappings as well as the JPA XML mappings too. However, HBM mappings are no longer the recommended approach to map entities. As illustrated in the new User Guide, all examples make use of annotations.
There are more features provided by Hibernate-specific annotations than it is the case with HBM mappings. In Hibernate 6, it is planned to add an extension mechanism to the JPA XML mappings, therefore HBM mappings are deprecated.
Since you are migrating to Hibernate 5, it's a good idea to migrate from HBM to annotations too.
Yes, according to documentation (where authors recommended using annotations for mapping) is said that xml mapping is still possible: docs
So on - you can still us *.hbm.xml for entity mapping.
EDIT: ofc I mean *.hbm.xml

How to insert data from xml document to postgresql?

I have a table in PosgreSQL.
I have an xml schema.
I want to create an xml document with this schema and data from Postgre.
What should I read to work with xsd in java? And what are the best tools to use?
UPDATE
More specific question. Can i create data base in PostgreSql using xml schema?
UPDATE 2
Okey. I create data base from .xsd using XMLSpy.
Now i need load xml document in this data base. What i gonna do?
UPDATE 3
Okay. I generate java classes using JiBX.
Now i want to read xml file and write data from this to data base. What i gonna do?
Probably best to generate java classes for the XML from the XSD, you can read about it here:
Generate Java classes from .XSD files...?
I dont know anything about the postgreSql, but i will suggest you to generate java classes from your Xsd using jaxb and use those classes as domain for your db.
yes, the best way to accomplish the above task would be to generate java class and populate your respective beans.
From there on you can insert your values of beans to the respective tables in posgresql

Validate XML Schema in Android

I need to validate some xml files in the resources of an Android application against an XML Schema. Although there is an available API to create instances of SchemaFactory, there seems to be no implementation for XML Schema, as stated by the answers to these questions: 1, 2 and 3.
Are there any good and lightweight libraries to provide that functionality in the Android platform?
Try Lycia. Its around 270 kb. Here is nice step by step example for schema validation. It creates a schema, a small XML referring to schema (via schemaLocation) and then an example to parse the xml with schema validation

Is there a JPA provider which stores in XML format and not for a RDBMS?

DataNucleues for example does it but I don't like the preprocessing (their JPA implementation is based on JDO). Are there other alternatives? The format of the XML-file doesn't really matter.
We use JPA already and store to a RDBMS. I don't need new design approach but just if there is such a solution to persist in XML. My question is not "Is it a good idea to persist with JPA to XML?"
EclipseLink includes both JPA and JAXB support to allow both database and XML mapping. You can use JAXB to serialize an object model to a file or stream. EclipseLink also has EIS support for non-relational databases including XML data sources (an XML file adapter is included).
In general if you are just looking for a light weight database, I would recommend an embedded Java database such as Derby or HSQL.

Declarative XML -> POJO conversion

I have to write a process (in Java) which periodically hits a URL, reads the returned XML document, and persists that data into the DB. This data is further used by my application, so I have modeled them as Hibernate-mapped POJOs.
I can parse the XML and then create appropriate POJOs, but I was looking for a simpler declarative approach. What libraries are available which can take a input configuration and create the POJOs from the XML document?
Another alternative could be JiBX
Also, although you said you don't want to parse the XML, XPath can be a very concise way of extracting the content you are interested in?
JAXB can automatically create classes based on an XML Schema (assuming you have one for the XML source). At runtime, it can then convert the XML document into POJOs representing the XML. It is declarative in that you can tweak the Schema-to-class mapping, a little.
If I understand your task correctly, this is pretty much the use-case JAXB was designed for (though it can do other things too). It's part of Java 1.6 (maybe 1.5 too?), in packages: javax.xml.bind.*
You can use XStream to deserialize the XML and map it directly to the Hibernate-mapped POJOs.
Cheers.
Using Hibernate you can directly map XML to table. This is experimental feature. Check here
http://www.hibernate.org/hib_docs/v3/reference/en-US/html/xml.html
EclipseLink JAXB (MOXy) has extensions for mapping JPA entities to XML (JPA entities have things like embedded ID classes, lazy loading, and compound key relationships that need special handling), I'm not aware of any other OXM solution that does this.
For more information see:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA

Categories

Resources