Generate Hibernate Mapping Files (*.hbm.xml) from POJOS? - java

What's the best/easiest way to generate *.hbm.xml files from pojos?

Pojo's don't have an inherent ORM mapping. Mapping files (or mapping annotations) are the 'value added' of hibernate. If you really wanted to try something like this you could annotate all your classes with #Entity and try to get hibernate to generate schema based on this.

You can use xdoclet to do this. Or you can use Hibernate's JPA implementation (Hibernate Annotations), add annotations to your POJOs, and get rid of the .hbm.xmls once and for all.

The way I get around this short coming is to use
1) Grails to generate the pojos. Any scaffolding framework would suffice.
2) Then Eclipse to reverse engineer the database tables into pojos.
Works everytime.

Related

Use Entity in non JPA module

I am working on a multi module java project and I ended up creating a module for the shared model and services.
I have a problem because one module (using Spring Data REST) uses JPA entities and they need to be shared with other modules in the form of simple POJO (without database sync). At the moment I have duplicated code without any JPA/Hibernate annotations but this is surely not the way to go. Any clue how I can proceed please ?
JPA allows to define the mapping in XML using orm.xml. A real life exmaple can be found at here and here.
For me , though the POJO become more clean when the mapping is defined in orm.xml , it seems to require more effort when compared to annotation and also make the codes look like less readable and maintainable . I would be more pragmatic to just let them annotated with JPA annotations. If a module only need to use them as the plain POJO but not JPA entity , just not include the JPA implementation and the JPA annotation will simply be ignored.

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

Is there something similar to Nhibernate "Mapping by code" for Hibernate

In Nhibernate we have Fluent Nhibernate and, now, the built-in "Mapping by code" feature in Nhibernate 3.2. Both allow you to programmatically construct the mappings for your Domain and we could either write some conventions to map all the domain or we could write individual classes for each corresponding domain object.
Anything similar for Hibernate?
You should be able to configure Hibernate without XML and Annotations by using the Hibernate Configuration API, see
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/cfg/Configuration.html
Table mappings can be created through the Mappings API:
http://docs.jboss.org/hibernate/orm/4.1/javadocs/org/hibernate/cfg/Mappings.html
I've never used the latter one as I found annotations the best way to go - but maybe the pointer helps.
Yes, mapping by annotations, check this link

Convert XML to database with Java

Need: take in XML and save data to database.
Currently using: JAXB to convert the XML Schema to java classes. Then I intend to use JPA to persist the objects marshalled by JAXB.
Problem: I want something to bridge the gap. After JAXB generates the Java classes I have to manually annotate all java.util.Date fields with #Temporal; I have to put #Entity on the top of every generated class...etc.
I came across Hyperjaxb. But I can find little documentation on it, and can't get it to work.
I am open to completely different approaches. This seems like it would be a common problem, so maybe there is a generic solution.
Note: I'm the EclipseLink JAXB (MOXy) lead, and a member of the JAXB 2 (JSR-222) expert group.
If you already have an existing database schema, the you could use the Dali tool in Eclipse (part of the Web Tools Project) to generate your JPA entities form the database:
http://www.eclipse.org/webtools/dali/
JAXB is configuration by exception, this means you only need to add annotations where you want to override the default behaviour. Dali also has tooling to make adding JAXB annotations easier:
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/GettingStarted/TheBasics
JPA entities sometimes use bidirectional relationships and composite keys, these can be tricky to map to XML. EclipseLink JAXB (MOXy) contains some extensions to make this easier (note EclipseLink also offers a JPA implementation):
http://blog.bdoughan.com/2010/07/jpa-entities-to-xml-bidirectional.html
http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JPA
Hyperjaxb does exactly what you're trying to achieve. Here's the documentation:
http://confluence.highsource.org/display/HJ3/Home
Here's a tutorial to get you started:
http://confluence.highsource.org/display/HJ3/Purchase+Order+Tutorial
I can suggest two options:
Option 1. Define your entity types separately, having the relevant JPA annotations, generate your JAXB types from the schema, and at runtime, map one to the other. If it is a simple mapping you can use Apache BeanUtils to just copy over the attributes from one bean to the other, if it is a more complex mapping then you can use a framework like dozer
Option 2: Start from entity types, generate the schema from your entity types or manually keep the entity types and the schema in synch. More like the option that you have described, except that the authoritative source is the java code than the schema.

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.

Categories

Resources