Convert classes relationships in Java into database schema - java

What is the best tool/framework to convert classes into database schema automatically without XML mapping files?
My problem is this, I have around 20 classes with different relationships (association, inner classes, etc) to each others and I want to convert all that into a database tables to save all these data along with its relationships.
I tried to see some tutorials to Hibernate and found out that it requires building a mapping XML which is going to be very painful to my case.
Any framework to automate this?

Alternatively, you can annotate your classes instead of using XML and then generate the schema from your entities. Anyway, I strongly recommend writing the classes in a way that is easy to use with an OR mapper. It would have been best to design your model for the use of Hibernate beforehand.

Related

Java XML format to save directly to relational or non-relational DB

Is there a standard XML format to save Java objects into relational or non-relational databases?
I am looking for something in Java akin to how you can save JSON directly into MongoDB (as BSON).
It doesn't have to be a non-relational database, something with an ORM process would do.
So it would be a Java object represented by XML (or something similar) then the XML could be sent over a wire to be saved in a DB. Maybe there's no point in doing this for relational databases.
Is JAXB the only game in town?
I have used HyperJaxb3 with great success. It is a JAXB plugin that takes the XSD for your XML and puts JPA annotations on your generated Java classes. This allows you to map your XML documents to SQL tables such that everything is exposed in your relational database. Round-tripping XML documents through the database is trivial.
The JPA details are highly customizable either in-line in your XSD or in a separate bindings file. HyperJaxb3 is open source and the author has been helpful in answering the few questions that I have had. He is active on Stack Overflow.

Migrating Data accross different DB Schema

I want to migrate my data from one DB to other using Java. Both DBs have different schema structure. I might also need to define some mapping / validation rule. Can anyone please guide me about any strategy, framework or any opensource project.
Thanks
Isn't in this case I have to create all the POJO to match the both schema (even by auto generating). Is there any way to avoid this thing i.e. giving schema mapping and generating POJO on fly in memory ?
Any idea?
Thanks
Yes, you need an Extract-Transform-Load (ETL) tool.
Here are some open source choices:
http://www.google.com/search?gcx=w&sourceid=chrome&ie=UTF-8&q=open+source+etl
ETL is generally used for this as in duffymo's answer.. you could also try ORM tools for this:
There is the Torque project.. http://db.apache.org/torque/
Read the data from your existing schema into java objects, then set them into the other objects for the other schema and then save them into the database. I am pretty sure hibernate also can be used, although I havent used hibernate per se. It works on the same way as torque..

How to generate orm mapping classes from sql schema in Java

I have an existing sql schema file for db. Is it possible to generate and re-generate when needed DAO's entities and all other required helper/client classes to access it? I don't mind what will it be -- hibernate, other jpa or something else.
Asuming you/others are still looking for a solution:
I just got the same problem and got it working in Eclipse (slightly different) as follows:
created JPA Project and downloaded & added user library in the wizard
Also wanted to give a schema-sql-file as input but instead found a way to take an actual db as input. (That was surely much easier for the developers of the tool to process than parsing proprietary sql-script-files)
To do that "rightclick" you jpa project an there "new/other/jpa/entities from tables"
In the following Wizard you have to create a db-connection to the db whose schema you want to get as jpa-annotated POJOs (IMHO It's very intuitive..but you may ask if there is a problem)
After finishing all jpa-classes are generated from the db...saved me from a lot of dummy work :)

Felxibility of data import/export feature using XML-binding

I am going to develop a database import/export feature in a Java EE application.
I plan to use XML-binding solution to convert between Java object and XML file which is import/export file.
Import feature: unmarshal the XML file to Java object in memory representation, then use JDBC to update the database.
Export feature: inverse the import process. retrieve the database to Java object and marshal the object to XML.
I think it can work fine, but it's not flexible enough. Because the XSD of XML is pre-defined, it's impossible to change XML schema and Java object definition at runtime. Say it's dynamic binding. Even I want the feature supports other file formats (You could forget it, if the format is too far at this stage).
What is your advice about the feature? Thanks!
I don't know whether this is correct answer, but if in any case it is helpful:
I have used Spring, Hibernate, JAXB where you can annotate your database entity class and its element with jaxb annotation and you are not required to write any xml schema files. In spring you can use jaxb Marshaller.
I think it should be possible in pure jaxb also, so u can look into jaxb annotation.
I think it can work fine, but it's not
flexible enough. Because the XSD of
XML is pre-defined, it's impossible to
change XML schema and Java object
definition at runtime
If you think it isn't flexible enough, go with a data interchange format which relieves you from all these fixed schema definitions (I know even JSON has a schema specification but you get the point). Is using JSON acceptable?
I would go as far to argue that if "importing to database" and "exporting from database" is the only requirement, you need not even create Java objects for this. Simply pass in a JSON string which contains the schema which would then be processed by a JSON processor which interfaces directly with your DAO layer. Similarly with the data read from the database. The downside is that "date" support in JSON is spotty at best.
Come to think of it, it need not be JSON. You can take a look at other data serialization formats like Apache Avro. But then again, if XML is your requirement which can't be changed, you can get around the "flexibility limitation" by not using a schema at all.
After all, XML is like violence. If it doesn't solve your problem, you're not using enough of it. :-)

XSD creation problem

I have been assigned to create a XSD schema for a proposed XML. i know what tables will be involved and what will be required fields which we always going to expect in the XML as well as optional.
I am very new to XSD and don't know where to and how to start.Can any one suggest me how to proceed so that i can start exploring something as currently i am on ground zero with the requirement.
Thanks in advance
Umesh
Best place to start off when you are at ground zero is w3schools -
http://www.w3schools.com/schema/schema_example.asp
Check this tutorial for beginners, looks good to me -
http://www.codeguru.com/java/article.php/c13529
The simplest way to start would be download a good XML editor, like XMLSpy and create schema visually. Altova has a free 30-day evaluation for their product, that should be enough for you to crank a first draft of your schema.
Depending on what you application is going to do, you might be able to generate one. For example you mention that your data is coming from (database) tables. If you intend to interact with the database using JPA entities, then you could use JAXB to generate an XML schema from the entity classes:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema
If you are using other tools to interact with the database, they may also have schema generation utilities.

Categories