Generate XSD schemas from an existing database or JPA entities - java

I have been using hyperjaxb tool to generate JPA entities and database scripts from XSD schemas. Now I want basically the inverse operation. How can I generate XSD schemas based on JPA anotated entities or database? I am using Maven, so any maven solution would be preferable.

If you're using IntelliJ IDEA, see the following link:
https://www.jetbrains.com/idea/help/generate-xml-schema-from-java-using-jaxb-dialog.html

This maven plugin ( jaxb2-maven-plugin ) might be useful for you.
This post has more information about its usage and the given example uses POJO's to do the transformation
Generate XSD from Java class

Related

can i use jaxb with hibernate hbm2ddl?

I am using jaxb to autogenerate java classes from an xsd file. I will need to persist the data that will be stored in the objects that will be instantiated from the classes. Is there some way that the hibernate code can be autogenerated in the same classes that are autogenerated by JAXB?
I will need to regenerate the classes many times in the course of development. If I have to write the hibernate code by hand, the only reasonable method I can imagine is to write separate classes with hibernate for persistence, and to write connector classes that migrate the autogenerated classes into the persistence classes. Otherwise, my hand-written hibernate code would be over-written every time I re-run jaxb based on fine tuning of the xsd file. If hibernate code were also autogenerated, I could end up using a lot fewer classes.
Hyperjaxb3 is the way to go. It is a JAXB plugin that you include in the build process. When you run your xsd file through xjc, you will not only get xml-related annotations on the generated classes, you will also get JPA annotations. Without writing hardly any code, you will be able to take an XML document, persist it to a database, query the document from the database and get XML text back out. The generated code can be customized either in the xsd file or in an associated binding file (just like with vanilla xjc). We also utilize hbm2ddl on the produced classes to configure hibernate.
We have been actively using this on several schemas for the past couple of years.
As you edit your XML schema/bindings (and thus your DB schema), you will have to manually write a SQL migration script to upgrade any existing databases. It seems like most DBMS have a schema comparison tool that can be leveraged here. We automatically compare the freshly created schema to the migrated (from a baseline) schema on every build.

How to create entity classes using database for all the existing tables?

I am having tables created by a DBA and I intend to have
entity classes for each table for further querying. Can any one suggest how to automatically create the entity classes using Eclipse?
If you use JPA for persistence, you can use "JPA tools" provided by Eclipse to generate java entity class from tables.
Here is the [tutorial][1]
[1]: http://shengwangi.blogspot.com/2014/12/how-to-create-java-classes-from-tables.html on my personal website.
You can generate classes using Hibernate Tools. There are two ways to generate these classes which is called as Reverse Engineering and Code Generation[dead link] -
You can use Hibernate Tools Eclipse Plugin.
You can write Ant Script using Hibernate tools jar.
You can try the Telosys code generator. It can help for this kind of job.
It allows to generate the Java (POJO) classes from any database
with the basic templates and you can also create your own
template (a Velocity file) to generate your DAO classes
For more information see https://www.telosys.org

Is it possible to auto generate annotated-mapped POJO classes using techniques like Hibernate, JIBX etc?

Hibernate can auto-generate schemas from properly annotated POJO classes. And I also know that JIBX can create a data model (set of classes) out of properly structured XML schemas. Is there a way to automaticaly generate annotated-mapped classes from an XML schema? Or is it just possible to run a tool on a set of POJO classes, and expect it to create meaningful annotations on the specified classes? So later on we can create database schemas using these classes. To annotate every class that JIBX produces takes actually more work than manually designing the database schema according to the xml schema.
The Hyperjaxb project will generate JAXB classes from an XML schema that contain JPA annotations that could be used to create a database schema.
http://java.net/projects/hyperjaxb
Nice question! We had the same problem and we ended up developing the POJO generator with Freemarker.
By the way, the requirements to these POJOs may strongly vary, so, if such tool exists, it must have quite bloated configuration.

How to generate model from database

I have an existing database. I need to generate the model classes in Java from it. Are there any tool/library that will allow me to do this. It will be of great help if it can emulate the entity relationships in the database into the model classes as well.
It is acceptable if the tool/library works with only one database vendor. I will create a database there and then generate the model.
Thanks in advance.
EDIT : I will probably use Hibernate as the ORM framework if I manage to generate the model.
The Hibernate Tools project (available as an Eclipse plug-in, as well as an Ant task) allows for "reverse-engineering" of database schemas into appropriate entity classes.
This project is also available in the JBoss Tools project.
The facility allows for reverse-engineering of the database metadata into a Hibernate configuration file. All artifacts (including the .java files) are generated from this config file.
You can control the nature of the reverse engineering process to suit your database structure. In other words, you can specify the schemas that you wish to have the tool reverse-engineer. You could also override the JDBC type mapping, apart from limiting the reverse-engineering process to a selected set of tables.
Obligatory link:
Screencast on Reverse engineering and code generation
Telosys does exactly this job
Let's have a look : http://www.telosys.org/
Minuteproject 4 JPA2 (http://minuteproject.wikispaces.com/JPA2) track does this task.
Minuteproject can be run from console or command line.
You can have a quick result by generating from the console where generate a maven project containing the JPA2 mapping classes in java or groovy.
If you use the command line then you need to fill an xml file that can contain additional customisation of your generated code such as packaging, enum, aliasing etc...
You can also try other track built on top JPA2 such as DAO with spring or EJB; REST; front end with Primefaces or Openxava; etc...
Hibernate has an Eclipse plugin Hibernate Tools http://www.hibernate.org/subprojects/tools.html that has reverse engineering capabilities.
See: http://docs.jboss.org/tools/3.2.0.GA/en/hibernatetools/html/plugins.html#refeng_codegen for more details on how to run and customize the reverse engineering process.

How to generate JPA mapping file from the JPA annotated entity classes?

I am using openjpa runtime of JPA specification.
At development time I am using annotations to configure jpa entities.
At Integration, Pre-Production and and Production environment, I am using orm mapping files to configure entities. Please suggest a tool which can generate mapping files from jpa annotations, so that these mapping files can be manually edited for different environment.
If there is already a opensource maven-plugin; will be great.
I don't really know OpenJPA so there is maybe a better way to do this but one option would be to first generate the XML schema file from annotated entities using the Schema Tool and then the orm.xml file from the schema.xml using the Reverse Mapping Tool. Actually, this process is discussed in this thread.
I've checked the OpenJPA Maven Plugin but it doesn't seem to support the Reverse Mapping part (it only has a openjpa:schema goal that allows to Create a file which contains the schema mapping XML, the first required operation, but nothing for the second part). Extending the plugin to add the missing openjpa:reverse-mapping goal would thus require some development but it shouldn't be an hard task.
There is another option though. OpenJPA provides the following Ant tasks for both operations:
org.apache.openjpa.jdbc.ant.ReverseMappingToolTask
org.apache.openjpa.jdbc.ant.SchemaToolTask
So it should be possible to call them from Maven using the Maven AntRun Plugin. Check the documentation for more details on how to use them.

Categories

Resources