-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Problem with AnnotationConfiguration and hbm2ddl.auto
PostPosted: Sat Jul 14, 2007 7:20 pm 
Newbie

Joined: Sat Oct 11, 2003 2:01 pm
Posts: 7
Hi,

For some reason it seems that none of "hibernate.hbm2ddl.auto" properties ("create", "update" or "create-drop") are working.

Any insight will be appreciated. Thanks in advance! :-)


Hibernate version: 3.2.4.sp1

Hibernate Annotations version: 3.3.0.ga

Mapping documents: JPA annotated class, no hibernate specific mappings or XML configuration files.

Code between sessionFactory.openSession() and session.close():

Code:
Session session = new AnnotationConfiguration()
   
    .addAnnotatedClass(UserInfoType.class)
   
    .setProperty(Environment.HBM2DDL_AUTO, "create")
    .setProperty(Environment.POOL_SIZE, "1")
    .setProperty(Environment.SHOW_SQL, "true")
    .setProperty(Environment.FORMAT_SQL, "true")
    .setProperty(Environment.AUTOCOMMIT, "false")
   
    .setProperty(Environment.URL, "jdbc:mysql://localhost:3306/test")
    .setProperty(Environment.USER, "root")
    .setProperty(Environment.PASS, "secret")
    .setProperty(Environment.DRIVER, com.mysql.jdbc.Driver.class.getName())
    .setProperty(Environment.DIALECT, org.hibernate.dialect.MySQL5InnoDBDialect
        .class.getName())
       
    .buildSessionFactory().openSession();

session.createCriteria(UserInfoType.class).list();

session.close();


Full stack trace of any exception that occurs:

Code:
org.hibernate.exception.SQLGrammarException: could not execute query
    at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
    at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
    at org.hibernate.loader.Loader.doList(Loader.java:2223)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
    at org.hibernate.loader.Loader.list(Loader.java:2099)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
    at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
    at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
    at Test.testAnnotationConfiguration(MD5Test.java:41)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at junit.framework.TestCase.runTest(TestCase.java:168)
    at junit.framework.TestCase.runBare(TestCase.java:134)
    at junit.framework.TestResult$1.protect(TestResult.java:110)
    at junit.framework.TestResult.runProtected(TestResult.java:128)
    at junit.framework.TestResult.run(TestResult.java:113)
    at junit.framework.TestCase.run(TestCase.java:124)
    at junit.framework.TestSuite.runTest(TestSuite.java:232)
    at junit.framework.TestSuite.run(TestSuite.java:227)
    at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:76)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'test.user' doesn't exist
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2941)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1623)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1715)
    at com.mysql.jdbc.Connection.execSQL(Connection.java:3249)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1268)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1403)
    at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
    at org.hibernate.loader.Loader.doQuery(Loader.java:674)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
    at org.hibernate.loader.Loader.doList(Loader.java:2220)
    ... 25 more


Name and version of the database you are using:

MySQL 5.0.37 (tested also with HSQLDB 1.8.0.7)

The generated SQL (show_sql=true):

Code:
Hibernate:
    insert
    into
        USER
        (EMAIL, FIRST_NAME, LANGUAGE, LAST_NAME, PASSWORD, USERNAME, ACTIVATED, LAST_LOGIN, LAST_UPDATED)
    values
        (?, ?, ?, ?, ?, ?, ?, ?, ?)
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 1.016 sec <<< FAILURE!


Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 7:27 pm 
Newbie

Joined: Sat Oct 11, 2003 2:01 pm
Posts: 7
... and the generated SQL was obviously from another unit test. :-)

Code:
Hibernate:
    select
        this_.USER_ID as USER1_0_0_,
        this_.EMAIL as EMAIL0_0_,
        this_.FIRST_NAME as FIRST3_0_0_,
        this_.LANGUAGE as LANGUAGE0_0_,
        this_.LAST_NAME as LAST5_0_0_,
        this_.PASSWORD as PASSWORD0_0_,
        this_.USERNAME as USERNAME0_0_,
        this_.ACTIVATED as ACTIVATED0_0_,
        this_.LAST_LOGIN as LAST9_0_0_,
        this_.LAST_UPDATED as LAST10_0_0_
    from
        USER this_
Tests run: 2, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.984 sec <<< FAILURE!


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 7:36 pm 
Newbie

Joined: Sat Oct 11, 2003 2:01 pm
Posts: 7
I think I found the problem.

For some reason, hbm2ddl is trying to create a NUMERIC(19,2) column for a BigInteger field.

Code:
@Id
@Column(name = "USER_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
@XmlAttribute(required = true)
@XmlSchemaType(name = "nonNegativeInteger")
protected BigInteger id;


Code:
02:32:29,406  INFO SchemaExport:154 - Running hbm2ddl schema export
02:32:29,406 DEBUG SchemaExport:170 - import file not found: /import.sql
02:32:29,406  INFO SchemaExport:179 - exporting generated schema to database
02:32:29,406 DEBUG DriverManagerConnectionProvider:93 - total checked-out connections: 0
02:32:29,406 DEBUG DriverManagerConnectionProvider:99 - using pooled JDBC connection, pool size: 0
02:32:29,421 DEBUG SchemaExport:303 -
    drop table if exists USER
02:32:29,437 DEBUG SchemaExport:303 -
    create table USER (
        USER_ID numeric(19,2) not null auto_increment,
        EMAIL varchar(50) not null,
        FIRST_NAME varchar(25) not null,
        LANGUAGE varchar(2) not null,
        LAST_NAME varchar(25) not null,
        PASSWORD varchar(32) not null,
        USERNAME varchar(10) not null unique,
        ACTIVATED bit not null,
        LAST_LOGIN datetime,
        LAST_UPDATED datetime not null,
        primary key (USER_ID)
    ) ENGINE=InnoDB
02:32:29,437 ERROR SchemaExport:274 - Unsuccessful: create table USER (USER_ID numeric(19,2) not null auto_increment, EMAIL varchar(50) not null, FIRST_NAME varchar(25) not null, LANGUAGE varchar(2) not null, LAST_NAME varchar(25) not null, PASSWORD varchar(32) not null, USERNAME varchar(10) not null unique, ACTIVATED bit not null, LAST_LOGIN datetime, LAST_UPDATED datetime not null, primary key (USER_ID)) ENGINE=InnoDB
02:32:29,437 ERROR SchemaExport:275 - Incorrect column specifier for column 'USER_ID'
02:32:29,437  INFO SchemaExport:196 - schema export complete


Last edited by Pulverturm on Sat Jul 14, 2007 7:52 pm, edited 1 time in total.

Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 14, 2007 7:50 pm 
Beginner
Beginner

Joined: Thu Feb 22, 2007 6:08 am
Posts: 35
hi. i dont know what is the problem, but im using a xml file insted to configure the entity manager and never gave me a problem. Following is a hbm2ddl.xml file and the persistence.xml

hbm2ddl.xml
Code:
<?xml version="1.0" encoding="UTF-8" ?>
<project name="hbm2ddl" default="all" basedir=".">
   <target name="hbm2ddl" description="Export a generated schema to DB">
      <property name="build.dir" value="./build/web/WEB-INF/classes" />
      <property name="config.dir" value="./src/conf" />
      <path id="toolslib">
         <path location="lib/hibernate-core/c3p0-0.9.1.jar" />
         <path location="lib/hibernate-core/hibernate3.jar" />
         <path location="lib/hibernate-core/commons-collections-2.1.1.jar" />
         <path location="lib/hibernate-core/commons-logging-1.0.4.jar" />
         <path location="lib/hibernate-core/dom4j-1.6.1.jar" />
         <path location="lib/hibernate-entitymanager/hibernate-annotations.jar" />
         <path location="lib/hibernate-entitymanager/ejb3-persistence.jar" />
         <path location="lib/hibernate-entitymanager/hibernate-entitymanager.jar" />
         <path location="lib/hibernate-entitymanager/javassist.jar" />
         <path location="lib/hibernate-entitymanager/jboss-archive-browsing.jar" />
         <path location="lib/hibernate-tools/hibernate-tools.jar" />
         <path location="lib/hibernate-tools/freemarker.jar" />
         <path location="lib/mysql-connector-java-3.1.8-bin.jar" />
      </path>
      <taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask" classpathref="toolslib" />
      <hibernatetool destdir="${build.dir}">
         <classpath path="${build.dir}" />
         <jpaconfiguration persistenceunit="ImmobilePU" />
         <hbm2ddl drop="true" create="true" export="true" outputfilename="sql-ddl.sql" delimiter=";" format="true" />
      </hibernatetool>
   </target>
</project>


persistence.xml

Code:
<?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="ImmobilePU">
      <provider>org.hibernate.ejb.HibernatePersistence</provider>
      <properties>
         <property name="hibernate.archive.autodetection" value="class, hbm" />
         <property name="hibernate.show_sql" value="true" />
         <property name="hibernate.format_sql" value="true" />
         <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
         <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/immo" />
         <property name="hibernate.connection.username" value="root" />
         <property name="hibernate.c3p0.min_size" value="5" />
         <property name="hibernate.c3p0.max_size" value="20" />
         <property name="hibernate.c3p0.timeout" value="300" />
         <property name="hibernate.c3p0.max_statements" value="50" />
         <property name="hibernate.c3p0.idle_test_period" value="3000" />
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
         <property name="hibernate.hbm2ddl.auto" value="update" />
      </properties>
   </persistence-unit>
</persistence>


HibernateUtil.java
Code:
public class HibernateUtil {
    private static EntityManagerFactory emf;
   
    public static EntityManagerFactory getEntityManagerFactory() {
        if (emf == null ) {
            // todo change the entity persistence unit name
            try{
                emf = Persistence.createEntityManagerFactory("PersistenceUnitNAME");
            }catch(Exception e){
                e.printStackTrace();
            }
        }
        return emf;
    }
   
    public static void closeEntityManagerFactory() {
        if (emf != null ) {
            emf.close();
        }
    }
   
       
    /**
     *  Este método retorna a propriedade de uma Entity, passando a path até a propriedade
     *  Ex: Distrito de um imovel - Distrito.Nome
     *
     *  @param entity
     *
     *
     **/
    public static Object getEntityProperty(Object entity, String pathToProperty){
        Object actualEntity = null;
        Object value = null;
        String methodName = null;
        String[] parsedPath = pathToProperty.split("\\.");
        Object[] arguments = new Object[]{};
        Method getValueMethod = null;
       
        if(pathToProperty.length() == 0){
            return "";
        }
       
        if(parsedPath.length == 0){
            parsedPath = new String[1];
            parsedPath[0] = pathToProperty;
        }
       
        try {
            actualEntity = entity;
            for(int i=0; i < parsedPath.length; i++){
                methodName = "get" + parsedPath[i];
                getValueMethod = actualEntity.getClass().getMethod(methodName, new Class[] {});
                value = getValueMethod.invoke(actualEntity, arguments);
                actualEntity = value;
            }
        } catch (IllegalArgumentException ex) {
            ex.printStackTrace();
            return "";
        } catch (IllegalAccessException ex) {
            ex.printStackTrace();
            return "";
        } catch (InvocationTargetException ex) {
            ex.printStackTrace();
            return "";
        } catch (SecurityException ex) {
            ex.printStackTrace();
            return "";
        } catch (NoSuchMethodException ex) {
            ex.printStackTrace();
            return "";
        }
       
        return value;
    }
}


for more informations read the "Hibernate EntityManager User Guide"[/]

hope it helped you to solve the problem

--ms


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.