H2 in-mem-DB with hibernation set to create a table giving me no found errors

I wanted to create a project with hibernate, spring mvc and H2 as (at the moment) in the memory database. When everything loads (in the berth), I get errors saying that the tables are not yet present.

This is the error I get:

Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport execute INFO: HHH000227: Running hbm2ddl schema export Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: HHH000389: Unsuccessful: alter table PUBLIC.Object drop constraint FK_9sd2x4ehbugdjyrp1xqmsjw00 Okt 09, 2013 3:42:47 PM org.hibernate.tool.hbm2ddl.SchemaExport perform ERROR: Tabelle "OBJECT" nicht gefunden Table "OBJECT" not found; SQL statement: ... 

This would be normal if I did not install Hibernate, for example:

 hibernate.hbm2ddl.auto=create 

and connection string for H2:

 jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=TRUE 

I can connect to the database using IntelliJ, so there is one.

I expected Hibernate to generate tables for me, and then the constraints and everything else, but for some reason it does not. Could this be user rights? these are the user settings that I set:

 jdbc.user=sa jdbc.pass= 

Any help is appreciated! Thanks:)

UPDATE

If I changed the connection string to this:

 jdbc.url=jdbc:h2:~/db/database.db;DB_CLOSE_ON_EXIT=TRUE;INIT=create schema IF NOT EXISTS generic; 

it works. But why it doesn’t work in memory and why it didn’t work before, when I set the default scheme to “public” (which was already there, so I decided that I could just dump my things there ...) IDK!

+6
source share
2 answers

This sounds like https://hibernate.atlassian.net/browse/HHH-7002 .

In my case, I was able to throw exceptions by setting Hibernate to use the following class using the hibernate.dialect property in persistence.xml :

 import org.hibernate.dialect.H2Dialect; // This class is a workaround for https://hibernate.atlassian.net/browse/HHH-7002 public class CustomH2Dialect extends H2Dialect { @Override public String getDropSequenceString(String sequenceName) { // Adding the "if exists" clause to avoid warnings return "drop sequence if exists " + sequenceName; } @Override public boolean dropConstraints() { // We don't need to drop constraints before dropping tables; that just // leads to error messages about missing tables when we don't have a // schema in the database return false; } @Override public boolean supportsIfExistsBeforeTableName() { return true; } @Override public boolean supportsIfExistsAfterTableName() { return false; } @Override public String getCascadeConstraintsString() { return " CASCADE "; } } 

(Thanks to fooobar.com/questions/147194 / ... )

0
source

In addition to using:

  • DB_CLOSE_DELAY = -1;
  • DB_CLOSE_ON_EXIT = FALSE;
  • DATABASE_TO_UPPER = false

USE JPA PROPERTY :

 <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> 

INSTEAD OF THIS IBERNIC PROPERTY:

 <property name="hibernate.hbm2ddl.auto" value="create-drop" /> 

WORKING H2 in memory: persistence.xml example:

 <?xml version="1.0" encoding="UTF-8" ?> <persistence xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd" version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"> <persistence-unit name="testPU" transaction-type="RESOURCE_LOCAL"> <description>Hibernate test case template Persistence Unit</description> <class>com.domain.A</class> <class>com.domain.B</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="hibernate.connection.url" value="jdbc:h2:mem:test;MODE=PostgreSQL;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false" /> <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" /> <property name="hibernate.show_sql" value="true" /> <property name="hibernate.format_sql" value="false" /> <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/> <property name="javax.persistence.schema-generation.scripts.action" value="drop-and-create" /> <property name="javax.persistence.schema-generation.scripts.create-target" value="db-schema.jpa.ddl" /> <property name="javax.persistence.schema-generation.scripts.drop-target" value="db-schema.jpa.ddl" /> </properties> </persistence-unit> </persistence> 
0
source

Source: https://habr.com/ru/post/955582/


All Articles