OK, I'm trying to connect my SpringBoot application via JPA (Hibernate) to a legacy AS/400 DB database. The table names however have a "." (Period) in them.
ex: LE.ONFP is the table name.
Hibernate however is converting the period to an underscore which causes an error because there is not table called "le_onfp".
#Entity
#Table(name = "\"LE.OFNP\"", schema = "QS36F")
Here is my annotations at the beginning of my Entity class.
adding the following line to my application.properties files fixed my issue.
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
and keeping my annotation the same.
#Table(name = "\"LE.OFNP\"", schema = "QS36F")
Related
I have an entity with table name "Transaction". While creating connection using spring orm and hibernate, on startup it successfully creates tables in MySQL but being "Transaction" a keyword in SQL server, it fails on startup.
I cannot change the table name in the code as there is a lot of ripple effects. Hence is there a way where I can define the table name as Transaction in java and intercept or configure in such a way that hibernate while connecting to SQL server escapes the keyword and queries it as "[Transaction]" (escaping it using [])
You can use a naming strategy as explained in this article: https://www.baeldung.com/hibernate-naming-strategy
public class CustomPhysicalNamingStrategy implements PhysicalNamingStrategy {
#Override
public Identifier toPhysicalTableName(final Identifier identifier, final JdbcEnvironment jdbcEnv) {
return Identifier.toIdentifier(**your table name**);
}
// Other methods
You can try one of the following way:
#Table(name = "`Transaction`");
#Table(name = "\"Transaction\"")
or by setting a property:
hibernate.auto_quote_keyword=true
I have the following jpa/hibernate/hsqldb configuration:
JPA ddl-auto: create-drop
Hibernate entities have no #Table annotation and created with SpringPhysicalNamingStrategy. So, PersonalData entity table name is personal_data. Hibernate creates them due to running the application
hsql DB URL is jdbc:hsqldb:mem:testdb;sql.syntax_pgs=true
My problem is when I try to select due spring repositories with the hsql there is the error about non-existing PERSONAL_DATA table.
I found that this is SQL notation to use CASE_SENSETIVE tables and hqsl follows that. To resolve that developers offer quote table names in sql.
So, I have 2 unlikely ideas
Add #Table annotation to entities.
Override SpringPhysicalNamingStrategy
Is there a way to use a simple property?
I am generating MySQL table by Hibernate as follows:-
#Entity
#Table(name = "buyerPartyDetails")
public class BuyerPartyDetails {
.......
}
But in MySQL the actual table name is as buyerpartydetails I was expecting it should be buyerPartyDetails. How do I force hibernte to genarate table name as my espection?
You can force Hibernate to quote the identifiers by setting:
hibernate.globally_quoted_identifiers=true
or
hibernate.globally_quoted_identifiers_skip_column_definitions=true
which will generate quoted table names in DDL. It might however require SET GLOBAL SQL_MODE=ANSI_QUOTES; as by default MySQL uses backticks ` to quote the names.
I have several database tables that my Spring MVC/JPA application refers to using the #Entity and #Table Annotations. I've run into the issue where if my application switches between database connections, some tables that exist on database 1 may not exist in database 2 (as we are following the SDLC cycle and promoting table additions/changes after they get the "OK"), thus resulting in an SQL Exception when the application server starts.
Does spring offer a way to mark specific #Entity Classes as "Optional" or "Transactional" so there are no database Exceptions returned because of nonexistant tables?
In my opinion, there is no option to do that.
You can add automatic update of schema in Hibernate, but you mentioned that you are doing this manually.
Hibernate is validating the schema, when he establishes connection. You use #Entity, so he looks for that table and throws an error if there is no with the name specified.
There is a typical situation being faced where different tables are scattered through different schemas in Oracle database and they are related to each other (encompassing all different types of relations).
How can they be represented in Hibernate using annotations as when a sessionfactory handle is created for one schema, tables in that schema can't access other related tables (foreign key relation to tables in other schema)?
For a query like following, exception is thrown -
"from table1 as model where model.table2Name.table2column = "+foo
Exception comes as -
org.hibernate.QueryException:
could not resolve property:
table2column of:
com.test.table1
[from com.test.table1 as model where model.table2Name.table2column = 1]
Here table1 and table2 are present in different schemas.
Finally I got the solution. It is done using the schema annotation for that table as follows -
#Entity
#Table(name = "table1", schema="schema1")
Also the mapping of table2 class should be included in configuration file of table1 schema.