I have following hibernate.cfg.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/userdb</property>
<property name="connection.username">root</property>
<property name="connection.password">1234</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<property name="connection.pool_size">1</property>
<property name="current_session_context_class">thread</property>
<mapping class="com.beingjavaguys.hbn.User" />
</session-factory>
</hibernate-configuration>
I tryed another dialect(org.hibernate.dialect.MySQLDialect) but I see old result
pom.xml:
...
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.0.1.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
</dependencies>
</project>
When invokes following code line:
return new Configuration().configure().buildSessionFactory();
I see following stacktrace:
ERROR: HHH000231: Schema export unsuccessful
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown database 'userdb'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
...
at org.hibernate.tool.hbm2ddl.SuppliedConnectionProviderConnectionHelper.prepare(SuppliedConnectionProviderConnectionHelper.java:51)
at org.hibernate.tool.hbm2ddl.DatabaseExporter.<init>(DatabaseExporter.java:52)
at org.hibernate.tool.hbm2ddl.SchemaExport.execute(SchemaExport.java:368)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:305)
at org.hibernate.tool.hbm2ddl.SchemaExport.create(SchemaExport.java:294)
at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:452)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
at com.beingjavaguys.hbn.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
at com.beingjavaguys.hbn.HibernateUtil.<clinit>(HibernateUtil.java:12)
at com.beingjavaguys.hbn.App.saveUser(App.java:45)
at com.beingjavaguys.hbn.App.main(App.java:30)
What the reason of this problem?
How to fix it?
P.S.
database schema doesn't exist in MySql!
if I add Database shema explicitly - all works good.
Is where way to create schema from java application?
I usually use the properties file to automatically create a database when i'm using Spring, and below is how its done, hope this works so u'll modify this to suite your needs.....
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://localhost:3306/userdb?createDatabaseIfNotExist=true
database.user=root
database.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=create
MySQL will create your schema within a database, but will not create your actual database for you. You must have an existing database called 'userdb' in your local MySQL installation (log in and run something like 'create database userdb') before you run the schema export in hibernate.
My thoughts are that your MySQL doesn't have a schema for your database. So, at least you should go to check if it exists. I use MySQL Workbench (quite nice tool), so just check/create it there if it doesn't exist and try again.
Related
Im trying to solve it an Hibernate problem from yesterday, I already check my libraries and classes and i think im not missing anything, so Im not sure about the real problem on this, the error tha Im getting is:
Exception in thread "main" java.lang.NoSuchMethodError: 'void org.hibernate.cfg.AnnotationBinder.bindDefaults(org.hibernate.boot.spi.MetadataBuildingContext)'
Error message
I already uploaded all the correct dependecies/libraries, I hope can be other kind of issue, libraries already are there
POM imge file
thanks a lot for help me on this, already found the solution, let me share to you the solution in below code format:
<dependencies>
<!-- Hibernate framework 5.6.10.Final-->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.6.10.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.persistence/javax.persistence-api -->
<dependency>
<groupId>javax.persistence</groupId>
<artifactId>javax.persistence-api</artifactId>
<version>2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.6</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.mysql/mysql-connector-j -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.31</version>
</dependency>
</dependencies>
this is the correct dependecies structure, now the hibernate configuration is the following:
<property name = "hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<!-- Assume test is the database name -->
<property name = "hibernate.connection.url">jdbc:mysql://localhost:3306/hibernatedemo</property>
<property name = "hibernate.connection.username">root</property>
<property name = "hibernate.connection.password">mysqlroot</property>
<!-- JDBC Connection pool -->
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- Every session will run in separate thread and thread safe -->
<property name="current_session_context_class">thread</property>
<!-- Disable second level cache -->
<property name = "hibernate.cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>
<!-- Echo all executed sql to stdout -->
<property name="show_sql">true</property>
<!-- drop and re-create the database schema on startup -->
<property name="hbm2ddl.auto">create</property>
<!-- names the annotated entity class -->
<mapping class="com.to.Player"/>
hope can i help you with this, good day!
I'm trying to write JUnit Tests for an application that uses JPA with a Hibernate provider.
The way the JPA Context Beans are instantiated in the application is through JNDI names, that are defined in the Server Resources (Tomcat). I can't have that when testing with JUnit, so I have to provide a different context for JPA that is suited to run the tests.
The main problem when writing JUnit tests for an application that was written to be packaged in a WAR and run in a "Server Container", is that you have to replace the javaee-api jars with actual implementations of the APIs, I understand that part.
I use :
#ContextConfiguration(locations = { "classpath:context-config-test.xml" })
to tell Spring to setup a different context configuration XML for the Test.
Now, to the real problem : I'm trying to setup an EMF (EntitiManagerFactory) that uses Hibernate as the "Persistence Provider".
The way the "normal context" does it is via this :
<bean id="MyEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="persistenceUnitName" value="my_pu_name" />
<property name="persistenceXmlLocation" value="classpath:jpa/persistence.xml" />
</bean>
and then in jpa/persistence.xml :
<!-- spring with jta and entity manager factory -->
<persistence-unit name="my_pu_name" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/my_jndi_db_name</jta-data-source>
<properties>
....
</properties>
</persistence-unit>
And it works fine.
Now the way I'm trying to do it for the "Test Context" is faily close, like this :
<bean id="myEntityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDatasource"/>
<property name="persistenceUnitName" value="my_pu_name_test" />
<property name="persistenceXmlLocation" value="classpath:jpa/persistence-test.xml" />
</bean>
Then in jpa/persistence-test.xml :
<persistence-unit name="my_pu_name_test" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<exclude-unlisted-classes/>
<properties>
.....
</properties>
</persistence-unit>
... it seems OK, but I get the exception :
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException:
Error creating bean with name 'myEntityManagerFactory'
defined in class path resource [spring/beans-test.xml]: Invocation of
init method failed; nested exception is java.lang.NoSuchMethodError:
javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
I looked around a while, changed the Maven Dependencies to point to Hibernate-Core, which is an Implemenatation of JPA, checked the scopes were OK, and such things... but nothing worked.
After a while looking around I found that
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
was maybe better to use than
<provider>org.hibernate.ejb.HibernatePersistence</provider>
and I thought I found the problem but that didn't fix it...
So I really don't know what else to do now...
Here is the Hibernate-Dependencies i use in my POM :
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
<scope>runtime</scope>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<!-- slf4j already contained in Tomcat -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<exclusions>
<!-- slf4j already contained in Tomcat -->
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>
I also tried commenting the first one but didn't work.
I would really appreciate any help or pointers on this issue...
Thanks and have a nice day :)
So the way I fixed it, since nothing seemed to work on the Maven side of things (no scope I put changed anything, javaee-api was always resolved in the classpath before hibernate-jpa) I downloaded and added the jars in the classpath of my JUnit test manually, putting them before the Maven Dependencies... it's not pretty but it does the trick.
I'm using Hibernate in a Maven project. The DBMS is MySQL. The libraries are imported into the pom as follows:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>4.2.7.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>3.2.0.Final</version>
</dependency>
<dependency>
<groupId>javax.transaction</groupId>
<artifactId>jta</artifactId>
<version>1.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.27</version>
</dependency>
Now, when I try in Netbeans to run a simple HQL query (i.e. from User), I get the following error:
org.hibernate.HibernateException: could not instantiate QueryTranslatorFactory: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:436)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:258)
at org.hibernate.cfg.Configuration.buildSettingsInternal(Configuration.java:2863)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2859)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1870)
Caused by: java.lang.ClassCastException: org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory cannot be cast to org.hibernate.hql.QueryTranslatorFactory
at org.hibernate.cfg.SettingsFactory.createQueryTranslatorFactory(SettingsFactory.java:433)
... 7 more
The strangest thing is that I see a ClassCastException for org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory. I see from here that the correct configuration into the hibernate.cfg.xml for the hibernate.query.factory_class is:
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
Moreover, my hibernate.cfg.xml configuration file is:
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/datadb?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory</property>
<mapping resource="edu/project/entities/User.hbm.xml"/>
<mapping resource="edu/project/entities/Code.hbm.xml"/>
<mapping resource="edu/project/entities/Incident.hbm.xml"/>
</session-factory>
</hibernate-configuration>
What's wrong?
PS: the MySQL server is online. Moreover I tried other queries, like select u from User.
Kindly provide some details which help to upgrade the hibernate version 3.2.4.sp1 to a suitable version.
We have already updated the Java runtime, Server OS and Database Server as well as some minor changes in the source code.
Following is the details of my Java Swing Application.
Application uses Java web-start technology which communicates with the Servlets running on Tomcat 7.
The Application is created some 8-9 years back. We keep on enhancing and bug fixing the application till date.
DB is SQL Server 2012.
Server Machine: Windows 64 bit 2008 R2 Enterprise Server
Java 7 update 25 64 bit
DB Driver: jtds-1.2
Ehcache version 1.2.3
The observations are as follows:
There is no ehcache.xml file int the project.
We are not using any annotation in Entity class or any other parts in the application.
Found that Ehcache 1.7.2 and above supports Java 1.5 and 1.6 at runtime.
Found that the ehcache some times creates deadlock not sure about aver version but visit the following link: http://jira.terracotta.org/jira/browse/EHC-918.
Following is my hibernate.cfg.xml for reference:
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="connection.url">jdbc:jtds:sqlserver://<ip>:<port>/TestDB</property>
<property name="connection.username">username</property>
<property name="connection.password">password</property>
<property name="hibernate.jdbc.batch_size">10</property>
<property name="c3p0.acquire_increment">1</property>
<property name="c3p0.max_size">5</property>
<property name="c3p0.max_statements">0</property>
<property name="c3p0.min_size">3</property>
<property name="c3p0.timeout">20</property> <!-- seconds -->
<property name="c3p0.idle_test_period">60</property> <!-- seconds -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="show_sql">false</property>
<property name="format_sql">true</property>
<property name="use_sql_comments">true</property>
<property name="generate_statistics">true</property>
<property name="hibernate.connection.provider_class">somepackage.C3P0ConnectionProvider</property>
<!-- Followed by our entity class mapping -->
<mapping resource="Batch.hbm.xml"/>
</session-factory>
</hibernate-configuration>
In this we are not specifying the second level hibernate cache, but I believe we are using some class in ehcache-1.2.3.jar as the cache provider since its there in the class path. When I go through the hibernate changelog I could see the default cache providers are different in some of the versions.
We are facing some performance and deadlock issue in current version of the application.
Kindly advice me a suitable version of hibernate so that I can upgrade to it without any issues.
<modelVersion>4.0.0</modelVersion>
<groupId>com.ranga</groupId>
<artifactId>HibernateApp2</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>HibernateApp2</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- JBoss repository for Hibernate -->
<repositories>
<repository>
<id>JBoss repository</id>
<url>http://repository.jboss.org/nexus/content/groups/public/</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<!-- Hibernate framework -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.1.12.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-ehcache</artifactId>
<version>4.1.12.Final</version>
</dependency>
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.4.0</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
</dependencies>
I'm trying to create integration tests using hsqldb in an in memory mode. At the moment, I have to start the hsqldb server from the command line before running the unit tests. I would like to be able to control the hsqldb server from my integration tests. I can't seem to get this to all work out though from code.
Update:
This appears to work along with having a hibernate.cfg.xml file in the classpath:
org.hsqldb.Server.main(new String[]{});
and in my hibernate.cfg.xml file:
<property name="connection.driver_class">org.hsqldb.jdbcDriver</property>
<property name="connection.url">jdbc:hsqldb:mem:ww</property>
<property name="connection.username">sa</property>
<property name="connection.password"></property>
<property name="connection.pool_size">1</property>
<property name="dialect">org.hibernate.dialect.HSQLDialect</property>
<property name="current_session_context_class">thread</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="hbm2ddl.auto">update</property>
Update
It appears that this is only a problem when running the unit tests from within Eclipse using jUnit and the built in test runner. If I run
mvn test
they are executed correctly and there is no exception. Am I missing something as far as a dependency goes? I have generated the eclipse project using
mvn eclipse:eclipse
and my pom is:
<?xml version="1.0" encoding="UTF-8"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myproject</groupId>
<artifactId>myproject</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>myproject</name>
<dependencies>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.3.2.GA</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.1.0.4.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.0</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.14</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.8.0.GA</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-annotations</artifactId>
<version>3.4.0.GA</version>
</dependency>
<!-- Test Dependencies -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
<version>3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>hsqldb</groupId>
<artifactId>hsqldb</artifactId>
<version>1.8.0.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty-servlet-tester</artifactId>
<version>6.1.24</version>
<scope>test</scope>
</dependency>
<!-- Provided -->
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>servlet-api</artifactId>
<version>6.0.26</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.26</version>
</dependency>
</dependencies>
<build>
<finalName>ww_main</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
<plugin>
<groupId>org.mortbay.jetty</groupId>
<artifactId>maven-jetty-plugin</artifactId>
</plugin>
</plugins>
</build>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
Update
Ok, well not sure what exactly was going wrong here, but I seemed to have fixed it. I deleted all the files that HSQLDB created, as well as all of the created files in my Maven target folder, did a clean, recreated my eclipse .project using maven and refreshed the project in eclipse. I think I may have had something left over from a previous configuration that was throwing it off.
Thanks for everyone's help!
I use the following configuration (directly inspired by the Hibernate tutorial) without any problem:
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/>
<property name="hibernate.connection.url" value="jdbc:hsqldb:mem:foobar"/>
<property name="hibernate.connection.username" value="sa"/>
<property name="hibernate.connection.password" value=""/>
<!-- JDBC connection pool (use the built-in) -->
<property name="connection.pool_size">1</property>
<!-- SQL dialect -->
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Disable the second-level cache -->
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<property name="hibernate.hbm2ddl.auto" value="update"/>
<mapping resource="..."/>
</session-factory>
</hibernate-configuration>
When using an in-memory HSQLDB, there is no need to start anything explicitly. Just use the mem: protocol and the in-memory database will get started from JDBC.
See also
Unit-Testing Hibernate With HSQLDB
Try appending this to the jdbc url:
;ifexists=true;shutdown=true;
In your shutdown method just do
Statement st = conn.createStatement();
st.execute("SHUTDOWN");
conn.close();
What about starting server through Runtime.getRuntime().exec("shell command here")? You have to do it only once for all tests, so it won't add too big lag.
Update
Ok, looks like you've solved it yourself :)
Update 2
To execute some code once before (or after) unit tests, you can
static class TestWrapper extends TestSetup {
TestWrapper(TestSuite suite) {
super(suite);
}
protected void setUp() throws Exception {
// start db
}
protected void tearDown() throws Exception {
// kill db
}
}
Then, just wrap your test set in it: new TestWrapper(suite)
Maybe this might help to start HSQL in server mode in a Unit test, but in the same JVM.
Sample code runs org.hsqldb.server.WebServer (i.e. port 80) but you may use org.hsqldb.server.Server. You may call setPort on either to override default port.
https://stackoverflow.com/a/37784679/15789
check my hsqldb maven plugin :
https://github.com/avianey/hsqldb-maven-plugin
You can just start/stop it like jetty-maven-plugin or tomee-maven-plugin for your tests :
<plugin>
<!-- current version -->
<groupId>fr.avianey.mojo</groupId>
<artifactId>hsqldb-maven-plugin</artifactId>
<version>1.0.0</version>
<!--
default value for in memory jdbc:hsqldb:hsql://localhost/xdb
override only values you want to change
-->
<configuration>
<driver>org.hsqldb.jdbcDriver</driver>
<path>mem:test</path>
<address>localhost</address>
<name>xdb</name>
<username>sa</username>
<password></password>
<validationQuery>SELECT 1 FROM INFORMATION_SCHEMA.SYSTEM_USERS</validationQuery>
</configuration>
<!-- call start and stop -->
<executions>
<execution>
<id>start-hsqldb</id>
<phase>pre-integration-test</phase>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>stop-hsqldb</id>
<phase>post-integration-test</phase>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>