0

I'm trying to set up a project with Spring and JPA + Hibernate with a MySQL database but whenever I start my Server (VMware vFabric tc Server, came with STS) I run into the following error :

ERROR: HHH000299: Could not complete schema update
java.lang.NullPointerException
    at org.hibernate.tool.hbm2ddl.DatabaseMetadata.initSequences(DatabaseMetadata.java:182)

I use the following configuration :

applicationContext.xml

<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"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.1.xsd">

    <!-- Database Configuration -->
    <import resource="classes/config/spring/beans/DataSource.xml"/>
    <import resource="classes/config/spring/beans/HibernateSessionFactory.xml"/>

    <!-- Security Configuration -->
    <import resource="springSecurityContext.xml" />

    <context:component-scan base-package="eu.shishigami" />

    <context:annotation-config />

</beans>

DataSource.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    http://www.springframework.org/schema/tx 
    http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location">
            <value>WEB-INF/classes/config/database/db.properties</value>
        </property>
    </bean>

    <bean id="dataSourceTemplate"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
        destroy-method="close">
        <property name="dataSource" ref="dataSourceTemplate" />
    </bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle10gDialect" />
        <property name="showSql" value="true" />
        <property name="generateDdl" value="true" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="hibernate-unit" />
        <property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mydb
jdbc.username=root
jdbc.password=password

HibernateSessionFactory.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">

        <property name="dataSource">
            <ref bean="dataSource" />
        </property>

        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
                <prop key="hibernate.ejb.naming_strategy">org.hibernate.cfg.ImprovedNamingStrategy</prop>
            </props>
        </property>

    </bean>

</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.0"
    xmlns="http://java.sun.com/xml/ns/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_1_0.xsd">

    <persistence-unit name="hibernate-unit" transaction-type="RESOURCE_LOCAL">
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        </properties>
    </persistence-unit>

</persistence>

"mydb" exists and the MySQL server is started.


I found a very similiar question here : ERROR: HHH000299: Could not complete schema update java.lang.NullPointerException but I couldn't get that answer to work :

I replaced the dataSource in my DataSource.xml :

<bean id="dataSourceTemplate"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="${jdbc.driverClassName}" />
    <property name="url" value="${jdbc.url}" />
    <property name="username" value="${jdbc.username}" />
    <property name="password" value="${jdbc.password}" />
</bean>

<bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource"
    destroy-method="close">
    <property name="dataSource" ref="dataSourceTemplate" />
</bean>

with

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="java:comp/env/mydb" />
    <property name="resourceRef" value="true" />
</bean>

and added this to my web.xml :

<resource-ref>
    <description>MySQL Development DataSource</description>
    <res-ref-name>mydb</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>

Then I changed the Server location in STS to "Use workspace metadata (does not modify tc Server installation" and modified the context.xml and server.xml in vfabric-tc-server-developer-2.9.3.RELEASE\base-instance\conf :

context.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <WatchedResource>WEB-INF/web.xml</WatchedResource>

    <ResourceLink name="mydb" 
        type="javax.sql.DataSource" 
        global="mydb" />
</Context>

server.xml

<?xml version="1.0" encoding="UTF-8"?>
<Server port="${base.shutdown.port}" shutdown="SHUTDOWN">

    ...

    <GlobalNamingResources>
        <Resource auth="Container" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" name="UserDatabase" pathname="conf/tomcat-users.xml" type="org.apache.catalina.UserDatabase"/>

        <Resource type="javax.sql.DataSource" name="mydb"
            driverClassName="com.mysql.jdbc.Driver" jdbcUrl="jdbc:mysql://localhost:3306/mydb"
            factory="org.apache.tomcat.jdbc.pool.DataSourceFactory" username="root"
            password="password" idleMaxAge="240" idleConnectionTestPeriod="60"
            partitionCount="3" acquireIncrement="5" maxConnectionsPerPartition="2"
            minConnectionsPerPartition="1" statementsCacheSize="50"
            releaseHelperThreads="5" />
    </GlobalNamingResources>

    ...

</Server>

I also copied mysql-connector-java-5.1.27.jar to the following locations (I wasn't sure which one I should copy it to) :

vfabric-tc-server-developer-2.9.3.RELEASE\base-instance\lib
vfabric-tc-server-developer-2.9.3.RELEASE\tomcat-6.0.37.A.RELEASE\lib
vfabric-tc-server-developer-2.9.3.RELEASE\tomcat-7.0.42.A.RELEASE\lib

I have the connector in my pom.xml as well (here's a full list of my dependencies in case I'm missing something?)

<dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-orm</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-web</artifactId>
      <version>3.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework.security</groupId>
      <artifactId>spring-security-config</artifactId>
      <version>3.1.4.RELEASE</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>3.2.4.RELEASE</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.16</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.inject</groupId>
      <artifactId>javax.inject</artifactId>
      <version>1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-api</artifactId>
      <version>2.1.13</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.faces</groupId>
      <artifactId>jsf-impl</artifactId>
      <version>2.1.13</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>servlet-api</artifactId>
      <version>2.5</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.glassfish.web</groupId>
      <artifactId>el-impl</artifactId>
      <version>2.2</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.sun.el</groupId>
      <artifactId>el-ri</artifactId>
      <version>1.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.27</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.primefaces</groupId>
      <artifactId>primefaces</artifactId>
      <version>4.0</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>joda-time</groupId>
      <artifactId>joda-time</artifactId>
      <version>2.3</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.12.2</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

For hibernate, I downloaded it and copied the 'required' libraries and the entitymanager library to the lib folders above :

antlr-2.7.7.jar
dom4j-1.6.1.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.7.Final.jar
hibernate-entitymanager-4.2.7.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.18.1-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar

After modifying my config as suggested in the other thread, I'm left with the following exception when starting the server :

WARNING: Unexpected exception resolving reference
java.sql.SQLException
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:254)

...

Caused by: java.lang.NullPointerException
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:266)
    at org.apache.tomcat.jdbc.pool.PooledConnection.connectUsingDriver(PooledConnection.java:246)

Thanks for any help, please tell me if I forgot something.

Community
  • 1
  • 1
Shishigami
  • 441
  • 3
  • 7
  • 20
  • Your configuration is quite unclear. You have 3 different locations where hibernate is configured: once in the Spring JPA configuration (where you say that the dialect is Oracle, BTW), once in the Spring session factory configuration (what's the use of this config?), and once in persistence.xml (what's the use of this config if you want to configure it using Spring)? – JB Nizet Nov 30 '13 at 09:08
  • Thanks for the comment. I don't fully understand how to configure this properly so I used a lot of different sources and got it a bit mixed up I suppose. I removed the persistence.xml and removed the hibernateProperties from my session factory. I also changed the oracle dialect to MySQL but it still shows the same exception when starting. Should I update the original question to reflect these changes? Again, thanks for your help. – Shishigami Nov 30 '13 at 09:21

1 Answers1

1

It is hard to tell what the problem is, but what I would try is to let hibernate create a database creation SCRIPT instead of executing it. And then I would have a look at this script (or excecute it by hand) to get some hint which mapping/table/sequence is the problem.

For creating this SCRIPT, you will need some lines of code, you can find one in Bozho's tech blog: How To Generate A Schema Creation Script With Hibernate 4, JPA And Maven

(I have my own script creation program (that is in its core like Bozho's) but it has to much special additional features. But if you can't get Bozho's script run, then leave a message and I will simplify my program and post it)

Ralph
  • 118,862
  • 56
  • 287
  • 383
  • Thanks a lot for your help. I copied the class and added the exec-maven-plugin to my build in my pom.xml but I'm not completely sure what to do after that. First off, is ${sql.generation.phase} generally available or do I need to define it myself? Also, how do I execute this plugin? I tried Runs As -> Maven build and specified "process-classes" as goal, but it seems like that didn't run the plugin (at least I can't see it in the console). Sorry for asking so many questions. – Shishigami Nov 30 '13 at 11:04
  • You should implement this short JpaSchemaExport class, and run its mail class. - Then Maven thing is some additional step you do not need to go. – Ralph Nov 30 '13 at 11:25
  • It runs without any exceptions but it doesn't do anything either. I used "C:\\schema.sql" as destination, is that a problem? Thanks for your help. – Shishigami Nov 30 '13 at 11:35
  • that is strange, have you seen second link in the blog: http://blog.iprofs.nl/2013/01/29/hibernate4-schema-generation-ddl-from-annotated-entities/ – Ralph Nov 30 '13 at 11:40
  • Sorry, I should have mentioned this earlier - I do not have *any* entities or even any classes just yet. Should I do this regardless? Also, should I go back to using dataSourceTemplate or is it better to use JNDI? I only get the error with the schema when I use the dataSourceTemplate, when using JNDI I get the exception at the end of my question. Again, thanks for your help. – Shishigami Nov 30 '13 at 12:21
  • "I do not have any entities or even any classes just yet" -- Thats the reason! The generator uses the entities to generate the scripts. maybe that is also the reason for the initial problem. – Ralph Nov 30 '13 at 13:38
  • I went ahead and cleaned all config files and removed some additional stuff (namely, Spring Security) and have been able to started the server successfully. I also created a TestEntity and the table was successfully created at server start. I wasn't able to get JpaSchemoExport to work despite this but it seems like that won't be necessary anymore. I tried to use JdbcDaoImpl for a User/Group/Permission based security, is that the right way to go about it or is there a better alternative? – Shishigami Nov 30 '13 at 14:43