I a Java app that I work on, there are several metamodel classes e.g. "Company_". However, these are in target folder and I am wondering why do we use them because the other files e.g. "Company" will also be built and generated in the target folder. Then, what is the purposes using JPA Hibernate Metamodel classes in a Java app.
Any explanations would be appreciated.
CodePudding user response:
Then, what is the purposes using JPA Hibernate Metamodel classes in a Java app.
Why do we need metamodel classes you ask ?
They are an internal detail which allows to achieve type safety for criteria queries. They are a means to an end. And this end is type safety for criteria queries. If this could be achieved in an easier way, without these extra classes it would be ideal to do it like this. Less classes -> less RAM used -> less classloading -> less startup time.
Specifically, here's the motivation for this ...
When you write a criteria query or create a dynamic entity graph, you need to reference the entity classes and their attributes. The quickest and easiest way is to provide the required names as Strings. But this has several drawbacks, e.g. you have to remember or look-up all the names of the entity attributes when you write the query. But it will also cause even greater issues at later phases of the project if you have to refactor your entities and change the names of some attributes. In that case, you have to use the search function of your IDE and try to find all Strings that reference the changed attributes. This is a tedious and error-prone activity which will easily take up the most time of the refactoring.
You can lookup more details here.
If you want to go deep and understand why such an implementation was chosen to achieve this goal you can look at the original Java Specification Request and get all the details.
Does this resolve your question ? Let me know in the comments.