I have a strange issue my ear project:
I have put the correct annotations on my entity class:
#Entity
#Table(name = "PRODUCTS")
But when I deploy my application on glassfish 3.1.2.2, I find that JPA has created the tables with lowercase chars
I am using EclipseLink 2.4.1
Please Help me.
I got the solution from Brian Vosburgh's comment:
In the windows version of MySQL the names of table are set to lower case. On linux, by default, this configuration is disabled, and the tables'names set by JPA are applied to MySQL without modification.
To make it working on windows as like linux, add the line:
lower_case_table_names=0
Thank you for your help, specially Brian Vosburgh
I found a property the makes all the difference.
In the EclipseLink wizard (generate entities from tables), third page (Customize Defaults), I checked the "Always generate optional JPA annotations and DDL parameters" option.
This option writes the below annotation in the Entity class:
#Table(name="TableName")
The correct table name letter case will be used.
Related
I have a JPA 2.1 application that connects to two different databases with two different sets of tables. Within IntelliJ, there is a Persistence View you can use to assign each JPA Entity to the appropriate Data Source and this works fine. IntelliJ is able to validate the Entity's Table and Column against the corresponding table in the Data Source.
Every now and then, IntelliJ (version 2018.3) loses my choice and attaches the Entity to the other Data Source. I find this out when I open the class and find the Entity's table and columns don't match. I stumble across the change some indefinite time after the swap has occurred.
My work-around is to manually remove the incorrect assignment and make the correct assignment. IntelliJ's inability to remember this assignment is getting old.
I suspect IntelliJ might be auto-generating files to represent the classes which are annotated #Entity and maybe this is where the problem lies.
I understand I could add the Entity to the persistence.xml using the <class> attribute such that the data source assignment is made in this config file, but it only appears to be a problem with IntelliJ. Deployments (maven-based) to the server compile and run as expected.
Is there something I can do with IntelliJ to avoid losing the data source assignments?
Other notes:
These mappings are not recorded in the <module>.iml file. I remain unsuccessful at finding where this is recorded.
Using the <class> attribute in the persistence.xml is not considered by IntelliJ for validation.
Using the #PersistenceUnit(unitName = "unitName") annotation on the Entity is not considered by IntelliJ for validation.
To get IntelliJ to remember the association between and Entity and its DataSource is the wrong approach. Instead, IntelliJ needs to be told the association between the Persistence Unit and its DataSource.
Both options are available from the Persistence View that is enabled when the JPA facet is enabled, but IntelliJ will list all annotated Entities under both persistence units which can lead you to thinking you need to open each Entity to perform the mapping.
The only action that is required within the Persistence View is to map each Persistence Unit to its DataSource.
I have been working on EER diagram model for my Mysql base for the last 7 days. It is a rather complicated model with lots of connections and attributes. Now i know spring boot automatically creates tables based on your entity classes(including foreign keys and other settings) if you use spring.jpa.hibernate.ddl-auto = update in your application.properties, but is it possible to create entity classes after providing good credentials to DataSource object based on tables withing schema?
Point is i would probably need another 3-4 days of back-end coding to create all the classes with all the attribute, relationships etc.
Given the fact it can only be done in one correct way, based on schema tables and it is not really rocket science. Why not do the thing just once?
There is this question Automatically create Entities from database
but 1. i am using spring boot not JPA project and second blog is no longer active.
Any hints?
Just figured it out. I used the hibernate perspective in eclipse to connect to a database, create a cfg.xml and run the project as hibernate configurations, create a new reveng.xml config and that will work. More detailed answer can be found in the article below.
http://o7planning.org/en/10125/using-hibernate-tools-generate-entity-classes-from-tables
Hope it will help someone!!
There is this question Automatically create Entities from database but
1. i am using spring boot not JPA project and second blog is no longer active.
Under the woods, Spring boot uses JPA and more specifically Hibernate since it has bad compatibility with other JPA implmentations such as EclipseLink.
Why don't you use Dali Eclipse plugin ?
https://www.eclipse.org/webtools/dali/docs/3.2/user_guide/tasks006.htm
It has a wizard with many options and it addresses well this kind of need. The real drawback when I use it and we cannot store our orm configuration. So, since you have many tables, I advise you to generate it in an incremental way your entities.
May I know can the Eclipse plugin Hibernate tool use to generate JPA entity #entity? The Java files that generated look like below and not JPA:
package com.test.only.model;
// Generated Jul 19, 2011 12:13:40 PM by Hibernate Tools 3.2.0.CR1
import java.math.BigDecimal;
import java.util.Date;
/**
* Account generated by hbm2java
*/
public class Account implements java.io.Serializable {
You just need to choose EJB3 + Java5 as configuration options when generating.
Learn more: http://docs.jboss.org/tools/4.0.0.Final/en/hibernatetools/html_single/index.html#jpa_annotations
I believe you can generate Hibernate annotation code by using the Hibernate Perspective -> Add Configuration… and follow the steps of this tutorial .
I suspect the JPA annotated entity can be generated if you select JPA(jdk 1.5+) or Annotation (jdk 1.5+) in the following configuration .Please try it.
Yes, it can.
Hibernate Tools, which is now available as a part of JBoss Tools, can be used for generating JPA entities as well. In fact you need to select JPA option in Hibernate Configuration which is being used for your project.
Even better would be to start off your reverse code generation as a JPA Project. Then once the project is created switch over to the Hibernate Perspective to confirm the JPA annotations option is selected in the Hibernate Configuration settings.
Following tutorial has pictorial representation of various steps involved in creating a JPA Project in Eclipse. It then shows in the Appendix section how to edit the Hibernate Configuration as well.
http://www.javabrahman.com/j2ee/how-to-setup-a-jpa-project-with-a-data-connection-for-mysql-in-eclipse/
For me changing from hibernate version 5.4 to 5.2 worked while configuring.
and do check use Java5 syntax and Generate EJB3 annotations while code generation.
I'm currently working on a desktop application using JPA/Hibernate to persist data in a H2 database. I'm curious what my options are if I need to make changes to the database schema in the future for some reason. Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
Is there support in JPA/Hibernate to do this?
Would I have to manually script a solution?
I usually let Hibernate generate the DDL during development and then create a manual SQL migration script when deploying to the test server (which I later use for UAT and live servers as well).
The DDL generation in Hibernate does not offer support for data migration at all, if you only do as much as adding a non-null field, DDL generation cannot help you.
I have yet to find any truely useful migration abstraction to help with this.
There are a number of libraries (have a look at this SO question for examples), but when you're doing something like splitting an existing entity into a hierarchy using joined inheritance, you're always back to plain SQL.
Maybe I'll have to introduce new entities, remove them or just change the types of properties in an entity.
I don't have any experience with it but Liquibase provides some Hibernate Integration and can compare your mappings against a database and generate the appropriate change log:
The LiquiBase-Hibernate integration records the database changes required by your current Hibernate mapping to a change log file which you can then inspect and modify as needed before executing.
Still looking for an opportunity to play with it and find some answers to my pending questions:
does it work when using annotations?
does it require an hibernate.cfg.xml file (although this wouldn't be a big impediment)?
Update: Ok, both questions are covered by Nathan Voxland in this response and the answers are:
yes it works when using annotations
yes it requires an hibernate.cfg.xml (for now)
There are two options:
db-to-hibernate - mirror DB changes to your entities manually. This means your DB is "leading"
hibernate-to-db - either use hibernate.hbm2ddl.auto=update, or manually change the DB after changing your entity - here your object model is "leading"
I've installed Hibernate Tools 3.2.4.GA in Eclipse Ganymede. The main driver for installing it was to be able to issue HQL queries interactively.
I have configured all of our mapping files, hibernate.properties, etc. and it shows all of our entities and mappings and is able to read a list of tables from the database. It doesn't appear to use the custom naming strategy I specified in the configuration and therefore I can't query anything. The query in the Dynamic SQL Preview is correct except for table names.
The way I configured the naming strategy was to right click the hibernate configuration and select 'edit configuration' In the 'Options' tab I 'browse' for a Naming strategy. Eclipse is able to find the naming strategy and auto-complete it for me, which would indicate it is in the classpath. I then hit OK and still have problems.
Am I missing something in the naming strategy configuration for hibernate tools (now jboss tools it would appear) or is it broken?
Optionally, is there another tool that I could use (in or out of eclipse) to issue HQL statements against my database and be able to see the results?
UPDATE
The problem was in the NamingStrategy implementation. It prepends a string to the front of every table, and changes camel case to all lower case with underscores. Unfortunately, in the implementation, the string is injected via spring, and there is no default set. That spring injection wasn't happening in hibernate tools, and because there was no default, the string was empty. It has been changed to have a default ( which matches our current spring configuration ) and it is working now.
I didn't use Hibernate Tools in Eclipse. But here is an alternative solution to issue HQL statements against your database.
there is tool called HQE Hibernate Query Editor.
Hibernate Query Editor (HQE) is a simple application designed to make easy the work of writting HQL/SQL queries for Hibernate.
It has simple features like syntax highlight and other editing stuff.
more information about HQE
can be found here https://hqe.dev.java.net/
You might look into my plugin which can create simple hqls easily.
http://fast-code.sourceforge.net/
Configure hibernate with myeclipse and you get can various options . This might help
http://www.skill-guru.com/blog/tag/hibernate-and-eclipse-tutorial/