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?
Related
I have a problem with the integration of hibernate envers on a application that use Teiid. This one doesn't support ddl like 'create table TABLE_NAME_AUD' or sequence generation.
I tried to split entity manager and tranction manager and delegate create table to another entity manager, but the transations on entities created by virtual table doesn't work. I also tried to create tables on DB, but the insert statement generated by hibernate dosn't work because the name of column of reventity is a keyword of teiid syntax and I can't change the name of column.
So, there's a way to implements hibernate envers with Teiid propertly?
Thanks!
Long story short
I want to generate DB schema from Hibernate mappings and then replace a specific table with a view of the same name before the application starts.
How can I do this using Spring / Hibernate / DbUnit / JDBC or something else?
My problem in details
I have a few integration tests that are executed against an in-memory database.
There's an AView view in a real database and it's mapped in Java code as
#Entity #Table #Immutable
public class AView {}
I'm generating H2 DB schema from Hibernate mappings for integration tests. And during test application context initialization this view is created as a table. From logs:
Hibernate: drop table AView if exists
Hibernate: create table AView (...)
Some tests fail because of this.
The idea
In order to fix this, I want to make H2 DB schema as similar as possible to the real DB schema. First, I want to generate DB schema from Hibernate mappings, and then replace AView table with AView view.
What I have tried
I have found a similar question: How to execute sql script after db schema generation but before application startup
I created a file schema.sql with DROP TABLE / CREATE VIEW statements. I tried to put the file in src/test/resources/schema.sql but it's not picked up automatically by Spring. I tried to specify this file explicitly in #Sql annotation, but it still doesn't have a visible effect.
I execute the tests via IntelliJ IDEA (if this is important).
My Code
Test and Test application context:
#RunWith(SpringJUnit4ClassRunner.class)
#ContextConfiguration(classes = { TestH2Config.class })
#TransactionConfiguration(defaultRollback = true)
#Transactional
public class AViewServiceIT {}
#Configuration
#PropertySource({"classpath:datasource-h2.properties"})
#EnableTransactionManagement
//#Sql({"/schema.sql"})
public class TestH2Config {}
datasource-h2.properties
datasource.driverClassName=org.h2.Driver
datasource.url=jdbc:h2:mem:itest;MODE=MSSQLServer;DB_CLOSE_ON_EXIT=FALSE;DB_CLOSE_DELAY=-1;INIT=CREATE SCHEMA IF NOT EXISTS itest\\;SET SCHEMA itest
datasource.username=sa
datasource.password=
hibernate.dialect=org.hibernate.dialect.H2Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create-drop
Spring framework version is 4.1.9.RELEASE.
You should be able to use the import.sql file in the root of your classpath as a means to have Hibernate execute a set of SQL commands after the schema has been built. Given that you wish to do this specifically for tests only, placing it in the test root classpath should be sufficient.
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")
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.
I created an entity class that has the same properties as project.java, and created a class where I can persist the entity object. Also, I created a database using databases in Netbeans using embedded JDBC. I have the persistence.xml, which provides the properties to connect the db, and is used the persitence class on entitymanagerfactory object. The connection seems fine but I am having Internal Exception: java.sql.SQLIntegrityConstraintViolationException: Column 'PROJECT_ID' cannot accept a NULL value. error.
Is it ok to create the db manually (executing the ddl) or should I create the table in the persistence.xml using property name="javax.persistence.jdbc.url"" value="create-tables ?
Regards
You have to set the value of the column PROJECT_ID.
You can either do this in your code, for example by using the annotations #SequenceGenerator or #GenericGenerator,
or use db features (trigger) to set the id during insert.