Upgrading hibernate from 3.2.4.sp1 to a suitable version - java

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>

Related

Exception in thread "main" java.lang.NoSuchMethodError: 'void org.hibernate.cfg.AnnotationBinder.bindDefaults(org.hibernate.boot.spi.MetadataBuilding

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!

Adding JAR file as dependency to project without its related JARs

We have file payment.jar that developed with Spring Framework 3.1.2 . We need to use it as a dependency on a new project that its version is Spring Framework 5.x .
Problem: When payment.jar used as a dependency, the payment.jar transitive dependencies add to project class-path so in cause conflicting Spring Framework 3 and Spring Framework 5.
I tried solutions:
big fat jar:
create-an-executable-jar-with-dependencies-using-maven
there is no success, the fat JAR, adds Spring Framework 3 to project. (what I am doing wrong!)
excluding payment Spring Framework 3.1.2 dependency in new project:
because of using spring-ibatis implementation in payment.jar it is not worked. Spring Framework new versions do not support ibatis.
Error:
Caused by: java.lang.ClassNotFoundException:
org.springframework.orm.ibatis.support.SqlMapClientDaoSupport
my requirement:
I need to use payment.jar (with Spring Framework 3.1.2) as a dependency on new project with Spring Framework 5.x without problem mentioned above.
If the Spring versions are as incompatible as it sounds as though they are, you're probably going to have to recompile either the jar or the project so that they're using the same version of Spring.
If that's impossible (e.g. if you don't have source for the jar), you might be able to wrap the payment jar up in a microservice so that it's running in a different process, and talk to it via XML-RPC or SOAP.
Your wish
I need to use payment.jar (with Spring Framework 3.1.2) as a
dependency on new project with Spring Framework 5.x without problem
mentioned above.
You cannot do it. Because the mismatching of version of dependencies. It is not backward compatible between many things, many version of iBatis, Spring Framework, payment.jar and other dependencies.
You must upgrade it manually.
The best option for this is to upgrade this manually.
Even if you will do this with Spring 3 jar, you will land with a lot of issues while compiling and running.
The ideal solution would be to update everything to Spring 5 or deploy payment.jar separately as a microservice as others mentioned. But this is not the core part of your question, maybe you have some unavoidable requirements. So as #tomer-shahar said the solution is to use maven shade plugin.
This kind of conflicts happen a lot when you use open source libraries sharing the same libraries but with different versions.
The maven shade plugin allows renaming/recolocating the packages selected avoiding conflicts within versions. The drawback is that some classes will be duplicated (so it is far from ideal). See the example below:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.0.0</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<finalName>spring321-payment-shaded</finalName>
<relocations>
<relocation>
<pattern>spring.package</pattern>
<shadedPattern>shaded.spring.package</shadedPattern>
</relocation>
</relocations>
<artifactSet>
<includes>
<include>*:*</include>
</includes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
And then add as dependency to the Spring 5 project this shaded library with all the renaming/rellocation done. I used this interesting article where all the details are fully described.
I suggest you to convert your jar in Spring 5 and MyBatis. I think it's the better solution in terms of mantenibility and compatibility. Migrate to iBatis to MyBatis does not a big problem, I think.
I had a working project that uses Spring 5 and My-Batis 3. I encountered some problem to configure it, but in the end, I solved everything. Just check the POM configuration and the Spring configuration.
POM configuration
In the following lines of code, you can read the project pom (I removed useless part of the file for your problem).
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>it.dummy</groupId>
<artifactId>dummy</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Dummy App</name>
<url>http://maven.apache.org</url>
<properties>
<java-source.version>1.8</java-source.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.skip.tests>true</maven.skip.tests>
<servlet-api.version>3.1.0</servlet-api.version>
<spring.version>5.0.7.RELEASE</spring.version>
<swagger.version>2.9.2</swagger.version>
<jackson.version>2.9.6</jackson.version>
<mybatis-spring>1.3.2</mybatis-spring>
<junit.version>4.12</junit.version>
<frontend-maven-plugin.version>1.4</frontend-maven-plugin.version>
<mapstruct.version>1.2.0.Final</mapstruct.version>
</properties>
<dependencies>
<!-- -->
<!-- https://mvnrepository.com/artifact/org.mapstruct/mapstruct -->
<!-- https://github.com/mapstruct/mapstruct -->
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-jdk8</artifactId>
<version>${mapstruct.version}</version>
</dependency>
<!-- cucumber -->
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.14.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-java -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-java</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->
<dependency>
<groupId>info.cukes</groupId>
<artifactId>cucumber-junit</artifactId>
<version>1.2.5</version>
<scope>test</scope>
</dependency>
<!-- test -->
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet-api.version}</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-core -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-core</artifactId>
<version>3.0.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-tx -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security Artifacts - START -->
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-web%20 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- http://mvnrepository.com/artifact/org.springframework.security/spring-security-config%20 -->
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-config</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring Security Artifacts - END -->
<!-- Swagger - BEGIN -->
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- Swagger - END -->
<!-- json -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
<type>jar</type>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9</version>
</dependency>
</dependencies>
</project>
Just remember to check always the dependencies tree to avoid to have indesiderate Spring jars.
Spring configuration
I have different file for Spring configuration. I attach only the interesting one.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:mybatis-spring="http://mybatis.org/schema/mybatis-spring"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="jdbc:oracle:thin:#xxx:yyy:zzz" />
<property name="user" value="aaa"/>
<property name="password" value="bbb"/>
<property name="connectionCachingEnabled" value="true" />
</bean>
<bean id="transactionManagerNG" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="sqlSessionFactoryNG" class="org.mybatis.spring.SqlSessionFactoryBean" name="SqlSessionFactoryOrarioStandardDao">
<property name="dataSource" ref="dataSource" />
<!-- 1. Where is the xml for query's definitions -->
<property name="mapperLocations" value="classpath:com/dummy/persistence/dao/*.xml" />
<!-- 2. My-batis configuration -->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!-- 3. My-batis type alias package -->
<property name="typeAliasesPackage" value="com.dummy.persistence.model" />
</bean>
<!-- 4. needed to parse Java DAO interfaces -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="it.insiel.sanita.farmacieng.persistence.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryNG"/>
</bean>
<tx:annotation-driven/>
</beans>
In the above Spring file you will find:
How I defined the location where put XML for SQL queries (comment 1)
How I use My-batis configuration (comment 2)
How I defined the type alias package (comment 3)
How I defined the DAO interfaces
Last but not least the My-batis configuration file mybatis-config.xml, that I put in the same folder of Spring configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<!-- http://mybatis.org/dtd/ -->
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
<setting name="cacheEnabled" value="true"/>
<setting name="defaultStatementTimeout" value="3000"/>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL" />
</settings>
</configuration>
Though I have not done this with spring specifically, the general concept of what you are trying to accomplish is called shading.
https://softwareengineering.stackexchange.com/questions/297276/what-is-a-shaded-java-dependency
The reason the fat jar didn't work is because all jars were loaded to the classpath, and the wrong one will be chosen for at least on of the dependencies.
What you need to do is change the package name in both a dependency and the depending jar (in your case spring 3 and payment jar), so you basically end up with two pairs of dependencies that don't overlap.
This is relatively simple to do in maven and similar tools. See the link for more details.

Why is Hibernate not creating database for MySQL?

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.

Spring MVC + Hibernate

i've a problem with hibernate configuration in Spring.
My project is a repository project. This is pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Framework-core-data-oracle</groupId>
<artifactId>Framework-core-data-oracle</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>4.2.8.Final</version>
</dependency>
</dependencies>
This are my beans definition:
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="configLocation">
<value>classpath:hibernate.cfg.xml</value>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
This is my hibernate config 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="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="hibernate.connection.url">jdbc:oracle:thin:#10.10.10.4:1521:BAGSTORAGE</property>
<property name="hibernate.connection.username">system</property>
<property name="hibernate.connection.password">bagstorage!</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="hibernate.default_schema">BAGSTORAGE</property>
<property name="show_sql">true</property>
</session-factory>
</hibernate-configuration>
When i try to autowire SessionFactory i've got this error:
org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate4.LocalSessionFactoryBean] for bean with name 'sessionFactory' defined in ServletContext resource -> nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate4.LocalSessionFactoryBean
I have spring orm added in pom, spring mvc version is > 3, so there is no problem with hibernate 4. Any ideas?
I've this situation:
Project A: core project, with spring dependency in maven
Project B: Repository project, where there are core project (project A), hibernate-core, oracle and spring-orm dependencies
Project C: Work project, with beans configurations and project A and project B in dependencies
Thanks
Your configuration looks correct, but I see you depend on spring-core 3.1.1 and spring-orm 4.0.0.
You really should align the versions of all your Spring modules. Please try to upgrade spring-core to version 4.0.0 and see if the error goes away ?
Usually, when a class is not loaded/found, there are 2 reasons:
1. There are multiple versions of it in the classpath(2+ artifacts containing it)
2. There is no class defined in the classpath
You have different versions for Spring modules, it is usually a best practice to keep them in sync, like this:
<properties>
<spring.version>4.0.0.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>

starting and stopping hsqldb from unit tests

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>

Categories

Resources