How to separate class declaration from its database relation definition - java

Context
I have a case in a kotlin project, where I generate classes using protocol buffers.
I'd like to create for those classes mapping definitions, so I could store them in db.
However, because protobuf classes are generated,
I cannot add Entity annotations to them.
Therefore I am looking a way to annotate those classes outside of their definitions.
What I tried
I was reading documentation for hibernate,
as I remembered it could create mapping definitions separated from class declarations:
Hibernate in 3.3 had an option to create xml mapping definitions:
https://docs.jboss.org/hibernate/core/3.3/reference/en/html/mapping.html
However, it seems missing from newer documentation and in 5.6 the closest I saw was:
https://docs.jboss.org/hibernate/orm/5.6/userguide/html_single/Hibernate_User_Guide.html#dynamic-model
Questions
Is it possible to still use XML mapping definitions in hibernate?
Why XML approach was removed from the Hibernate documentation?
OR
Are there any other solutions that would allow me to store protobuf objects in database without using hibernate?

Related

In Hibernate 3.5, what are the annotation equivalents of constrained and outer-join?

I'm currently working to convert an application from using hibernate mapping files (.hbm.xml) to hibernate/jpa annotations (with a jpa annotation preference).
I am running into these two attributes in some of the hibernate mapping files on one-to-one relationships:
constrained="true"|"false"
outer-join="true"|"false"
I have been heavily researching these two but I cannot find a straight answer.
It seems that optional="true"|"false" can handle the both of them, but it feels wrong to accept this as the answer without a reference from documentation or an official source.

Is it possible to automatically generate Hibernate mappings for Java classes?

I'd like to automatically generate Hibernate mappings for some Java classes I have. Basically, I'd like all objects in the class to have the equivalent of the #OneToOne annotation, and all collections to have the equivalent of the #OneToMany annotation by default. I'd like to be able to fine-tune this later, but 99% of my data works this way, and it would take a very long time to go through all the classes and add the annotations manually.
Is this possible?
edit: Sorry, I think omitted something very important: I'd actually like to generate these default annotations from classes, not from a database. I would still have to design about 40 tables if I were to start with the database, but I already have a package containing all those classes. Is it possible to generate the proper mappings based on existing Java classes?
Yes, in eclipse there is a plugin called hibernate tools from jboss, which can generate mapping from a existing database.
You can generate all hibernate files both xml and annotations variant.
Just Google Hibernate Tools to know how to use it.
Thanks !!
If you are creating new classes then you can create the database structure first and then create classes using database. In eclipse, hibernate provides Hibernate code generation feature. You can use that.

Separating JPA information from POJO

I am working on a project that will have entities persisted to a database using JPA. We will be using Maven as the project management framework. I am wondering if it would be possible to create one project for the POJOs and another for the persistence definitions and then "combine" the two into a single output that contains the POJOs and their persistence information.
Basically I am trying to separate the code that POJOs from the persistence definition. Because the POJOs may be reused by several different projects that may or may not need to persist them and may or may not want to change the persistence information. (Similar but not quite the same as Is it possible to build a JPA entity by extending a POJO?)
I have two ideas on how I might be able to do it. If I were to use the POJOs in a web application I could provide persistence.xml and map the classes in that project and just add a dependency to the project containing the POJOs. But if I wanted to create a single jar file containing the persistence information and the POJOs, I think I could use the shade plugin?
Is there any other way to essentially merge two maven projects into a single output and is this a reasonable thing to want to do?
If I remember correctly, then annotations do not have to be on the classpath if you're not using them. The annotated classes can still be loaded.
So my recommendation would be:
Stick with the JPA annotations, as this is the easiest way to define the mappings and tooling support is usually better.
Declare the JPA dependencies as optional and probably also as provided.
If you need to override the mappings defined by the annotations, it should be possible to do this using the persistence.xml, AFAIK (never tried).
I do appreciate the input. In the end, the solution for me was to create two projects. The first provided the definition of the POJOs without any JPA information. Yes there are some JPA related members such as id but I will address those. The second project contained the JPA metadata (orm and persistence XML files).
As for the members related to persistence (e.g. id) I could probably live with those in the model classes but using the suggestion in this post (Is it possible to build a JPA entity by extending a POJO?) I extended my POJO classes and declared id in the "entity" sub classes. This does require some consideration when defining the POJO in terms of access to members.
One thing to note, this solution runs into trouble when you have a class hierarchy (inheritance in your model). The classes in your "pure" model inherit from some common class. That class is then extended in the "persistence" model to provide the id and other persistence related members. Now if the persistent subclasses inherit from the classes in the "pure" model, they do not inherit the id and other persistent members.
There may be workarounds in different inheritance mapping such as table per concrete class.

Hibernate : mapping files or annotations?

As I started working with Hibernate a few days ago, I was just wondering : suppose you're starting a project from scratch. Would you use annotation-based mapping or Hibernate mapping files, to generate the database schema.
It is my understanding that Hibernate mapping files offer some features that you won't find (at least, not the exact equivalent) with annotations. But still, I have the feeling that nowadays, projects using Hibernate would rather go for annotation-based than Hibernate mapping files.
Has anybody ever chosen mapping files over annotations, and if so, for what reasons?
What advantages I see in using #Annotations:
compiler-safe
based on #Entity you can easily distinguish entity from no-entity
with packagesToScan Spring's feature entites are easily scannable
moving entites from packages to packages or class renaming is easy
What advantages I see in using XML:
it does not litters java classes with unnecessary vendor-specific
annotations (imagine java model class with JPA, JAXB, SOLRJ
annotations)
configuration in one place
easier to maintain as a whole
We use annotations, but keep XML as an option.
Go ahead with annotations any day. The XML configurations where really over used and saving the metadata inside the class is a good viable option.
Annotations will help you map the relationships better and it will align you to the JPA standard as hibernate uses mostly JPA annotations. There are no real problems using annotations and there is not much trade-off either. It has superior advantages over the XML based configurations. There might be few hacks missing when you use annotations but they will come along.
It is even possible to use annotations for new classes in a legacy project that has XML based mapping as told here.
The mapping in xml are not as illustrious as with annotation bases. So using annotations is more elegant at least for mapping.

Is it possible to mix orm.xml definitions with annotations when using JPA/hibernate?

I'm working on an application whihc supports using several DB vendors, with the table definitions being different for each DB type. The trouble is that the column definitions are not what hibernate expects, and so my entities contain a lot of #Column(..columnDefintion="..."...) annotations.
To further complicate the issue, there's no way to specify a columnDefinition per DB. So I tried moving just the columnDefinition bits to an orm.xml file, and I have a Maven profile that bundles the correct file.
JBoss 4.0.5/Hibernate 3.2.0GA fails to validate, and it seems it completely disregards the annotations given an xml file.
Is there a way to make Hibernate "merge" the data from the xml with the annotations ?
JBoss 4.0.5/Hibernate 3.2.0GA fails to validate, and it seems it completely disregards the annotations given an xml file.
Well, maybe you told him to do so (by specifying a metadata-complete attribute). Maybe you should have shown your orm.xml (or one of them).
well apparently not with the version of jboss/hibernate im using

Categories