Hibernate: many-to-many mapping with an order-column - java

I want to use a many-to-many relation between System & Device. I want the system to know its devices order.
I've seen here that I can do it using #OrderColumn.
How can I do it using hibernate xml configuration instead of annotation?

If you're using Hibernate you could try with
sort="unsorted|natural|comparatorClass"
order-by="column_name asc|desc"
as attributes of your relatonship declaration

#OrderColumn is the JPA annotation introduced in JPA 2.0. This works as an additional feature if we work hibernate through JPA. There is no equivalent replacement in hibernate who work with hibernate directly.

Related

Java Framework to maintain entity history automatically

I am working on a project where I need to create history of particular table automatically. I have used #Audited annotation for it but it's creating duplicate data in table.
I am running out of space due to duplicate data.Even EclipseLink is not sloving my problem.
You tagged your question with Hibernate, but if switching to Eclipselink would be an option for you, be sure to check out the History Policy feature of Eclipselink. It allows automatic historization of data.
Please check Hibernate Envers.
This component integrates with JPA and Hibernate, and takes care of maintaing a revision history for any #Audited annotated Entity.
There are some guides on the net:
Maintain the data versioning info with Spring Data — Envers
Spring Boot : How to add JPA and Hibernate Envers Auditing
Spring Boot + Hibernate + Hibernate Envers

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.

EclipseLink 1.2 and BatchFetch

I have run into a EclipseLink issue with multiple queries running against the DB instead of a single query. I found the below SO threads which provides #BatchFetch annotation solution to help run as one master query
How to do join fetching instead of select fectching with EclipseLink?
How to define the EclipseLink annotation for the following?
But I am using EclipseLink 1.2 and the above annotation's classes are only supported by EclipseLink 2+. Appreciate if you could please help with an alternate solution for 1.2. I tried migrating the entire project to EclipseLink 2 but am running into mapping issues which I have no patience to fix right now.
I am using EclipseLink's JPA
The #BatchFetch annotation just set the mapping to use batch reading. You can do this in EclipseLink 1.2 using a DescriptorCustomizer and using the ForeignReferenceMapping API.
One way is to use a query hint, for example:
query.setHint(QueryHints.BATCH, "c.sourceTable");
In this case the sourceTable relation for all c's will be loaded with a single query, not one per c.

How do you tell Hibernate that your entity class mappings are in JPA 1.0 syntax?

This is a follow-up to
Limitation of JPA 1.0 using #IdClass with *nested* composite primary keys?
EclipseLink comes with a javax.persistence_1.0.0.jar which I also put onto the classpath, so it can know about the version of my entity classes. But how does Hibernate know the entity classes are meant to be in JPA 1.0? Can it be done? If so, how?
But how does Hibernate know the entity classes are meant to be in JPA 1.0?
If this can be done, I would bet on the version of the persistence.xml. But I'm really not sure this actually restricts the use of JPA 2.0 features.
Why not using a JPA 1.0 implementation if you want to be sure that you're not using anything from JPA 2.0 (i.e Hibernate EntityManager 3.4 in the case of Hibernate)? Is there anything that prevent you from doing so?

Categories

Resources