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

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

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

Hibernate or JPA annotations to use

I am using Hibernate in our projects and annotation based configuration for Hibernate Domain Pojo Objects. For Annotations based configuration we have two options
JPA based annotations using javax.persistence.*
Use Hibernate Native Annotations org.hibernate.annotations.*
Currently we use JPA based annotation configuration for our POJO files and Hibernate native API like SessionFactory, Session, etc to open session and perform DB operations.
I have these questions:
Is there any problem mixing both JPA annotations and use Hibernate
native API?
This link explains one such issue
(cascade-jpa-hibernate-annotation-common-mistake)
Please provide your expertise, which type of annotations to use
JPA
Hibernate native
Mix both of them?
Hibernate provides one of the JPA implementations. If you use purely JPA in your code, you are free to change to a different implementation if a requirement arises. For example, EclipseLink/TopLink and OpenJPA are implementations which may be required for a different customer. A comprehensive list of implementations is here.
If you are compelled to use any exotic features provided by hibernate which are not in JPA specification, you should go for hibernate specific APIs in your code base. A related discussion from hibernate forum here.
We mixed some of these annotations since with the version of hibernate we were working those days, some features were not available on the JPA spec at that time. For instance to store a boolean value as a 'Y' or 'N' on th DB you have hibernate types you can use. But no such feature was available with the JPA spec at that time. I do not know about the status now. Also for orphan removal also those days JPA did not have the feature, but now i belive they provide an attribute called orphanRemoval on your cascade options. Also you have certain features such as #BatchSize to increase fetching performance with respect to bags. I am unaware if such features are available on the JPA spec yet.
In my experience, mixing and matching plus points from both would be beneficial given that you have no need of switching from one ORM to another.

Why ormlite has its own annotations?

From what I see in ormlite it has implemented its annotations as well as JPA standard annotations. First of all, what was the reason of designing new set of annotations?
Secondly, how one can use standard annotation like #Entity, etc instead of ormlite specific annotations. Right now, I am getting not defined error for those entities. Do I need a jar file?
#DataNucleus is correct. ORMLite is not a fully compliant JPA implementation. There are many features of ORMLite that do not map well with the JPA annotations and it was easier to create my own set. JPA is also a very large specification and I didn't want a large percentage of the annotations to generate UnsupportedOperationException or jut fail quietly. Lastly, I was trying to write a ORM library with 0 dependencies.
All that said, I am interested in improving ORMLite's JPA compatibility so if you have any suggestions on how to make it better, please send them to the developers mailing list. I'd love to improve it.
Because it isn't a real JPA implementation, and just makes use of JPA annotations for convenience. Obviously, by using it, you lose the portability offered by JPA itself, but then it may have some advantages for very specific situations

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.

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/

Categories

Resources