Schema generation with Maven Hibernate3 plugin

Prabhjot picture Prabhjot · May 7, 2011 · Viewed 12.7k times · Source

I'm trying to setup a simple hibernate + maven project. I'm following steps as mentioned in Chapter 2 of Java presistence with hibernate (only diff is I'm maven using instead of Ant). However, while trying to generate the schema with maven hibernate 3 plugin, it seems like it is not able to create the required tables.

Code & Resources

POM.xml

<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>com.gs.bce</groupId>
  <artifactId>SpringExample</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  
  <properties>
    <maven.compiler.source>1.6</maven.compiler.source>
    <maven.compiler.target>1.6</maven.compiler.target>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>
  
  
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.0.5.RELEASE</version>
    </dependency>
  
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-c3p0</artifactId>
        <version>3.3.2.GA</version>
    
    </dependency>
    



    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.5.8</version>
    </dependency>
    <dependency>
        <groupId>javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.12.1.GA</version>
    </dependency>
  </dependencies>  
    <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>hibernate3-maven-plugin</artifactId>
        <version>2.2</version>
        
          <executions>
          <execution>
            <phase>process-classes</phase>
            <goals>
              <goal>hbm2ddl</goal>
            </goals>
          </execution>
        </executions>
        
        <configuration>
          <components>
            <component>
              <name>hbm2ddl</name>
              <implementation>jdbcconfiguration</implementation>
            </component>
            <component>
              <name>hbm2hbmxml</name>
              <outputDirectory>src/main/resources</outputDirectory>
            </component>
          </components>
          <componentProperties>
            <drop>true</drop>
            <configurationfile>/src/main/resources/hibernate.cfg.xml</configurationfile>
               <outputfilename>schema.sql</outputfilename>
          </componentProperties>
        </configuration>
         <dependencies>
            <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.0.0</version>
    </dependency>
        </dependencies>
      </plugin>
    </plugins>
  </build>
</project>

`

Output log

Output of my mainclass, where it is trying to access MESSAGES table (as mentioned in book).

18:27:34,357DEBUG SQL:111 - 
    select
        max(MESSAGE_ID) 
    from
        MESSAGES Hibernate: 
    select
        max(MESSAGE_ID) 
    from
        MESSAGES 18:27:34,363DEBUG JDBCExceptionReporter:92 - could not fetch initial value for increment generator [select max(MESSAGE_ID) from MESSAGES] java.sql.SQLException: user lacks privilege or object not found: MESSAGES

Processing output

Output for process-classes goal

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building SpringExample 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ SpringExample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] --- maven-compiler-plugin:2.3.2:compile (default-compile) @ SpringExample ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] >>> hibernate3-maven-plugin:2.2:hbm2ddl (default) @ SpringExample >>>
[INFO] 
[INFO] --- maven-resources-plugin:2.4.3:resources (default-resources) @ SpringExample ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 3 resources
[INFO] 
[INFO] <<< hibernate3-maven-plugin:2.2:hbm2ddl (default) @ SpringExample <<<
[INFO] 
[INFO] --- hibernate3-maven-plugin:2.2:hbm2ddl (default) @ SpringExample ---
18:30:54,300  INFO org.hibernate.cfg.Environment - Hibernate 3.3.1.GA
18:30:54,304  INFO org.hibernate.cfg.Environment - hibernate.properties not found
18:30:54,306  INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist
18:30:54,308  INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling
18:30:54,346  INFO org.hibernate.cfg.Configuration - configuring from url: file:/F:/workspace/SpringExample/src/main/resources/hibernate.cfg.xml
[INFO] Configuration XML file loaded: file:/F:/workspace/SpringExample/src/main/resources/hibernate.cfg.xml
18:30:54,475  INFO org.hibernate.cfg.JDBCMetaDataConfiguration - Ignoring null mapping
18:30:54,475  INFO org.hibernate.cfg.Configuration - Configured SessionFactory: null
[INFO] No hibernate properties file loaded.
18:30:54,489  INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
18:30:54,490  INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
18:30:54,490  INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
18:30:54,495  INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
18:30:54,495  INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sa}
18:30:54,685  INFO org.hibernate.cfg.SettingsFactory - RDBMS: HSQL Database Engine, version: 2.0.0
18:30:54,685  INFO org.hibernate.cfg.SettingsFactory - JDBC driver: HSQL Database Engine Driver, version: 2.0.0
18:30:54,716  INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
18:30:54,745  INFO org.hibernate.transaction.TransactionFactoryFactory - Using default transaction strategy (direct JDBC transactions)
18:30:54,746  INFO org.hibernate.transaction.TransactionManagerLookupFactory - No TransactionManagerLookup configured (in JTA environment, use of read-write or transactional second-level cache is not recommended)
18:30:54,746  INFO org.hibernate.cfg.SettingsFactory - Automatic flush during beforeCompletion(): disabled
18:30:54,746  INFO org.hibernate.cfg.SettingsFactory - Automatic session close at end of transaction: disabled
18:30:54,746  INFO org.hibernate.cfg.SettingsFactory - JDBC batch size: 15
18:30:54,746  INFO org.hibernate.cfg.SettingsFactory - JDBC batch updates for versioned data: disabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Scrollable result sets: enabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - JDBC3 getGeneratedKeys(): enabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Connection release mode: auto
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Default batch fetch size: 1
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Generate SQL with comments: disabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Order SQL updates by primary key: disabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Order SQL inserts for batching: disabled
18:30:54,747  INFO org.hibernate.cfg.SettingsFactory - Query translator: org.hibernate.hql.ast.ASTQueryTranslatorFactory
18:30:54,748  INFO org.hibernate.hql.ast.ASTQueryTranslatorFactory - Using ASTQueryTranslatorFactory
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Query language substitutions: {}
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - JPA-QL strict compliance: disabled
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Second-level cache: enabled
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Query cache: disabled
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Cache region factory : org.hibernate.cache.impl.NoCachingRegionFactory
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Optimize cache for minimal puts: disabled
18:30:54,749  INFO org.hibernate.cfg.SettingsFactory - Structured second-level cache entries: disabled
18:30:54,751  INFO org.hibernate.cfg.SettingsFactory - Echoing all SQL to stdout
18:30:54,752  INFO org.hibernate.cfg.SettingsFactory - Statistics: disabled
18:30:54,752  INFO org.hibernate.cfg.SettingsFactory - Deleted entity synthetic identifier rollback: disabled
18:30:54,752  INFO org.hibernate.cfg.SettingsFactory - Default entity-mode: pojo
18:30:54,753  INFO org.hibernate.cfg.SettingsFactory - Named query checking : enabled
18:30:54,765 DEBUG org.hibernate.cfg.reveng.dialect.JDBCMetaDataDialect - getTables(null.null.null)
18:30:54,767  INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:hsqldb:hsql://localhost
18:30:54,773  INFO org.hibernate.dialect.Dialect - Using dialect: org.hibernate.dialect.HSQLDialect
18:30:54,777  INFO org.hibernate.tool.hbm2ddl.SchemaExport - Running hbm2ddl schema export
18:30:54,778  INFO org.hibernate.tool.hbm2ddl.SchemaExport - writing generated schema to file: F:\workspace\SpringExample\target\hibernate3\sql\schema.sql
18:30:54,778  INFO org.hibernate.tool.hbm2ddl.SchemaExport - exporting generated schema to database
18:30:54,779  INFO org.hibernate.connection.DriverManagerConnectionProvider - Using Hibernate built-in connection pool (not for production use!)
18:30:54,779  INFO org.hibernate.connection.DriverManagerConnectionProvider - Hibernate connection pool size: 20
18:30:54,779  INFO org.hibernate.connection.DriverManagerConnectionProvider - autocommit mode: false
18:30:54,779  INFO org.hibernate.connection.DriverManagerConnectionProvider - using driver: org.hsqldb.jdbcDriver at URL: jdbc:hsqldb:hsql://localhost
18:30:54,779  INFO org.hibernate.connection.DriverManagerConnectionProvider - connection properties: {user=sa}
18:30:54,883  INFO org.hibernate.tool.hbm2ddl.SchemaExport - schema export complete
18:30:54,885  INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:hsqldb:hsql://localhost
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 1.675s
[INFO] Finished at: Sat May 07 18:30:54 IST 2011
[INFO] Final Memory: 5M/47M
[INFO] ------------------------------------------------------------------------
18:30:54,988  INFO org.hibernate.connection.DriverManagerConnectionProvider - cleaning up connection pool: jdbc:hsqldb:hsql://localhost

Hibernate configuration

My hibernate.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration SYSTEM
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">
org.hsqldb.jdbcDriver
</property>
<property name="hibernate.connection.url">
jdbc:hsqldb:hsql://localhost
</property>
<property name="hibernate.connection.username">
sa
</property>

<!-- <property name="hibernate.connection.username">
bceuser123
</property> -->


<property name="hibernate.dialect">
org.hibernate.dialect.HSQLDialect
</property>
<!-- Use the C3P0 connection pool provider -->
<property name="hibernate.c3p0.min_size">5</property>
<property name="hibernate.c3p0.max_size">20</property>
<property name="hibernate.c3p0.timeout">300</property>
<property name="hibernate.c3p0.max_statements">50</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<!-- Show and print nice SQL on stdout -->
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<!-- List of XML mapping files -->
<mapping resource="hello/Message.hbm.xml"/>
</session-factory>
</hibernate-configuration>

Answer

Augusto picture Augusto · May 7, 2011

I think the problem with the the DDL creation is that it cannot find the hibernate configuration file. Try removing the leading slash on the path in the pom file. So the line should read like:

<configurationfile>src/main/resources/hibernate.cfg.xml</configurationfile>

To automatically create the DB add the following property to your hiberante.cfg.xml

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