Results 1 to 7 of 7

Thread: JPA Generate DDL

  1. #1
    Join Date
    Jul 2006
    Location
    London
    Posts
    500

    Default JPA Generate DDL

    Hello,

    Has anyone got the DDL to be generated using spring jpa config?

    Code:
    2007-07-23 13:50:42,132 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - Running hbm2ddl schema update
    2007-07-23 13:50:42,132 INFO [org.hibernate.tool.hbm2ddl.SchemaUpdate] - fetching database metadata
    2007-07-23 13:50:42,132 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] - trace com.mchange.v2.resourcepool.BasicResourcePool@1516490 [managed: 3, unused: 2, excluded: 0] (e.g. com.mchange.v2.c3p0.impl.NewPooledConnection@166b0df)
    2007-07-23 13:50:42,210 DEBUG [com.mchange.v2.c3p0.impl.NewPooledConnection] - com.mchange.v2.c3p0.impl.NewPooledConnection@2423ad handling a throwable.
    java.sql.SQLException: ORA-00942: table or view does not exist
    No tables seem to get created when I try.

    Code:
    <property name="jpaVendorAdapter">
    		<bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    			<property name="database" value="HSQL" />
    			<property name="showSql" value="true" />
    			<property name="generateDdl" value="true" />
    		</bean>
    </property>
    Regards,
    James

  2. #2
    Join Date
    Jul 2006
    Location
    London
    Posts
    500

    Default

    Resolved with further problems.
    Regards,
    James

  3. #3
    Join Date
    Sep 2006
    Location
    UK
    Posts
    8,424

    Default

    Quote Originally Posted by jamesclinton View Post
    Resolved with further problems.
    , care to elaborate?
    Last edited by karldmoore; Aug 27th, 2007 at 03:56 PM.
    Barracuda Networks SSL VPN Lead Developer
    http://pramatr.wordpress.com
    http://twitter.com/karldmoore
    http://www.linkedin.com/in/karldmoore
    Any postings are my own opinion, and should not be attributed to my employer or clients.

  4. #4
    Join Date
    Jul 2006
    Location
    London
    Posts
    500

    Default

    Having only looked at this for a short time. The reason *I think* for the error was down to using pure spring configuration if you want schema generation.

    I needed to use a mix of between spring and persistence.xml. (this could be related to other issues or more likely an error in my setup).

    Either way this is how I got it working.

    jpa-context.xml

    Code:
    <property name="jpaVendorAdapter">
      <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="databasePlatform" value="org.hibernate.dialect.Oracle9Dialect" />
        <property name="showSql" value="true" />
      </bean>
    </property>
    persistence.xml

    Code:
    <property name="hibernate.archive.autodetection" value="class, hbm" />
    <property name="hibernate.default_schema" value="XXXXX"/>
    <property name="hibernate.hbm2ddl.auto" value="create" />
    Setting default schema is very important depending the your development setup and the user you are connecting as. In my case the development user has DBA rights and could see all schemas which had identical tables inside them but different data sets. (This was unrelated to the problem, but something you should be aware of)

    HTH
    Last edited by jamesclinton; Jul 24th, 2007 at 03:28 AM.
    Regards,
    James

  5. #5

    Default

    I've been having the exact problem (except with MySQL). I've attempted your solution, but it does not work.

    persistence.xml:
    Code:
    	<persistence-unit name="dev" transaction-type="RESOURCE_LOCAL">
    		<class>com.foo.bar.Test</class>
    		<properties>
    			<property name="hibernate.archive.autodetection"
    				value="class, hbm" />
    			<property name="hibernate.default_schema" value="db_dev" />
    			<property name="hibernate.hbm2ddl.auto" value="create" />
    		</properties>
    	</persistence-unit>
    app.xml:
    Code:
    	<bean id="entityManagerFactory"
    		class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="persistenceUnitName" value="dev" />
    		<property name="dataSource" ref="dataSource" />
    		<property name="jpaVendorAdapter">
    			<bean id="jpaAdapter"
    				class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="databasePlatform"
    					value="org.hibernate.dialect.MySQLDialect" />
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    			</bean>
    		</property>
    	</bean>

    I always get the same exception since the schema doesn't exist (blank db):
    Code:
    javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not execute query
    	at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:629)
    	at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:73)
    	at com.foo.bar.Test.getChildren(Test.java:61)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:281)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:187)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:154)
    	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
    	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:176)
    	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:210)
    	at $Proxy18.getChildren(Unknown Source)
    	at com.foo.bar.OneTest.testGetChildren(OneTest.java:40)
    	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    	at java.lang.reflect.Method.invoke(Unknown Source)
    	at junit.framework.TestCase.runTest(TestCase.java:154)
    	at junit.framework.TestCase.runBare(TestCase.java:127)
    	at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.access$001(AbstractAnnotationAwareTransactionalTests.java:44)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests$1.run(AbstractAnnotationAwareTransactionalTests.java:112)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTest(AbstractAnnotationAwareTransactionalTests.java:177)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runTestTimed(AbstractAnnotationAwareTransactionalTests.java:150)
    	at org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests.runBare(AbstractAnnotationAwareTransactionalTests.java:108)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:159)
    	at org.springframework.test.jpa.AbstractJpaTests.runBare(AbstractJpaTests.java:239)
    	at junit.framework.TestResult$1.protect(TestResult.java:106)
    	at junit.framework.TestResult.runProtected(TestResult.java:124)
    	at junit.framework.TestResult.run(TestResult.java:109)
    	at junit.framework.TestCase.run(TestCase.java:118)
    	at junit.framework.TestSuite.runTest(TestSuite.java:208)
    	at junit.framework.TestSuite.run(TestSuite.java:203)
    	at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130)
    	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: 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:2214)
    	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2095)
    	at org.hibernate.loader.Loader.list(Loader.java:2090)
    	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:388)
    	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
    	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
    	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
    	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
    	at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:64)
    	... 39 more
    Caused by: com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Table 'db_dev.test' doesn't exist
    	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
    	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
    	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
    	at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
    	at com.mysql.jdbc.Connection.execSQL(Connection.java:3176)
    	at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1153)
    	at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1266)
    	at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
    	at org.hibernate.loader.Loader.getResultSet(Loader.java:1778)
    	at org.hibernate.loader.Loader.doQuery(Loader.java:662)
    	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)
    	at org.hibernate.loader.Loader.doList(Loader.java:2211)
    	... 47 more

  6. #6
    Join Date
    Jul 2006
    Location
    London
    Posts
    500

    Default

    Not sure what your problem is.

    I would remove the following lines in your config and retry:

    From Spring: <property name="generateDdl" value="true" />
    From JPA: <class>com.foo.bar.Test</class>

    They're not needed. Might have an impact (bit of a long shot thou).
    Regards,
    James

  7. #7

    Default

    I'm thinking it might be a bug with hibernate hbm2ddl. I found that most entities do get their schema created. However, for one @OneToOne related entity, the generation fails. It could be because I'm using interfaces and I specify the targetEntity parameter in the annotation. That's my guess at this point. More research is definitely required before I file anything in jira.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •