Autogenerating Hibernate annotations from XSD - java

I have been using jaxb to autogenerate Java classes from XSD files. I am seeking a list the various ways that I can also autogenerate the Hibernate annotations for persisting the data from the classes into a relational database. If the Hibernate annotations are autogenerated, then creating the database structures should be automatic using hbm2ddl.
I need to import XML data into a relational structure which can then be used by other apps. I have been experimenting with hyperjaxb, and I may stick with it, but there seem to be a lot of picky settings, for example its default settings can result in cripplingly verbose SQL statements. See this posting and this other posting.
I have read about Dali and EclipseLink, but it seems that neither of these other tools use Hibernate. I would like to stick with Hibernate. What are the options?

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 jpa achieves schema independence

I am a beginner to jpa and hibernate, I understood that how jpa achieves database independence and persistence provider independence.
I came across the sentence which states
jpa can achieve schema independence
how does jpa achieves this?
JPA is an API, this API is completely abstracted from your data representation and DDL. Also, it is abstracted from the API implementation (unlike going directly for Hibernate).
This means that you can:
Use differing JPA implementations such as Hibernate or Toplink
Use different DB dialects such as Oracle, T-SQL, PostgreSQL
Implement your own JPA implementation
These can be done through annotations AND/OR configuration. So in theory you can switch from Oracle to PostgreSQL without having to recompile your application.
This also means you can do other funky things like:
Use a free, Open Source DB for development, and then Oracle/SQL Server for Production
Use Hypersonic for automated tests, so everything is in memory and torn down automatically.
Use other data-sources such as XML transparently.
You get other niceties like SQL injection protection out of the box, very quick startup time (rather than having to code everything twice), automatic DDL generation when you've defined your entities etc. which get small projects started quicker than the traditional route.
IN REALITY:
Nobody chooses to go from Hibernate to Toplink
Very few people ever implement JPA for a project
Very few people rapidly switch DB backends as each Database performs very differently
Also, the differing dialects can drive out weird bugs.
The configuration aspects and the ability to use things like Hypersonic are useful though.
The other "flip side" to the lovely API abstraction is that the implementation can be too constraining, resulting in very inefficient SQL being produced. This ends up with the developer having to add in native SQL anyway.
Schema independence can be achieved in the same way as DB and provider independence - it is possible to change the schema of all tables simply in configuration. You can change default schema in an orm.xml file included in persistence.xml via persistence-unit-defaults.
You may also specify schema via #Table annotations.

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.

Java Database API requiring no XML configuration

I am recently back in Javaland from Ruby and Activerecord and was wondering if there were any database solutions that do not require me to set up XML files to use them, and if possible supply any configuration in pure Java?
If you like ActiveRecords in Ruby, you might be interested in jOOQ (Java Object Oriented Querying, a database abstraction library that I wrote). jOOQ requires only little configuration for its source code generator. Your database schema is mapped 1:1 to Java classes, which can then be used in a fluent API very similar to SQL itself.
Also, jOOQ doesn't manage transactions, sessions, caches, etc like JPA or Hibernate might do. So there is no additional runtime configuration required.
http://www.jooq.org
You can configure Hibernate without using any XML via the Configuration class (it doesn't have to read XML documents). It's easier to avoid the mapping of .hbm.xml files if you're using annotated classes and AnnotationConfiguration instead.
See this for more details: http://docs.jboss.org/hibernate/core/3.3/reference/en/html/session-configuration.html#configuration-programmatic
There are many. Though hibernate is the most famous one. It has annotation for most of the configuration and that too is not required, if you use naming strategies.
http://www.hibernate.org/

Auto-Generating EJB3 Entity Beans

I would like to know if there are any tools to automatically generate EJB3 Entity Beans(for JPA) from a database schema.
Thanks.
Dali supports top-down, bottom-up (this is what you're looking for), and meet-in-the-middle development approaches.
Some of IDs have such feature, for example NetBeans
OpenJPA has a tool which will generate your Entity definitions.
From the OpenJPA user manual:
OpenJPA includes a reverse mapping tool for generating persistent class definitions, complete with metadata, from an existing database schema. You do not have to use the reverse mapping tool to access an existing schema; you are free to write your classes and mappings yourself, as described in Section 3, “ Meet-in-the-Middle Mapping ”. The reverse mapping tool, however, can give you an excellent starting point from which to grow your persistent classes.
No IDE required!
-Rick
I have a solution for you i.e to create auto generate domain objects with all table relationship properly mapped in class ...Try Dal4j yes you can find it in sourceforge.net/p/dal4j/wiki/ DAL4j is a Command Line and Framework tool that can be used to reverse engineer a MySQL or SQLServer database schema into a set of JPA Entity Beans.
DAL4j can be useful for scenarios where there is an existing database schema but a technology other that JPA is used by applications to interact with the database. DAL4j can provide an easy way to migrate your code base from other technologies such as JDBC or Hibernate to JPA.
The beans generated can be 1 or two types: Simple or Framework. Simple beans are standard pojo classes managed by your application using JPA semantics. Framework generated pojos use the DAL4j framework DAO generic to simplify CRUD operations.
DAL4j provides optional hooks to allow you integrate encryption/decryption of data fields that must be encrypted in the database.
Last, DAL4j provides a set of Generic classes that can be used to simplify creation of Session Beans which perform CRUD operations using generated Entities.
I think you will find this article feasible....

Categories

Resources