2010-11-29 20 views
20

Je n'arrive pas à créer une table automatique au printemps en utilisant hibernate/jpa.Comment obtenir une création automatique de table en printemps/hibernate/jpa?

Voici mes fichiers de configuration:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 
    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" 
    version="1.0"> 

    <persistence-unit name="naveroTest"> 
    <provider>org.hibernate.ejb.HibernatePersistence</provider> 
    <class>xxx</class> 
     ... 


     <properties> 
      <property name="hibernate.archive.autodetection" value="class, hbm"/> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> 
    <property name="hibernate.connection.url" value="jdbc:hsqldb:file:/tmp/naveroTestDB"/> 
      <property name="hibernate.connection.username" value="sa"/> 
      <property name="hibernate.connection.password" value=""/> 
      <property name="hibernate.hbm2ddl.auto" value="create"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> 
     </properties> 
    </persistence-unit> 
</persistence> 

context.xml

<?xml version="1.0" encoding="UTF-8"?> 
    <beans xmlns:tx="http://www.springframework.org/schema/tx" 
     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.0.xsd 
          http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd 
          http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.4.xsd"> 

    <!-- For auto creation of tables --> 
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
     <property name="url" value="jdbc:hsqldb:file:/tmp/naveroTestDB" /> 
     <property name="username" value="sa" /> 
     <property name="password" value="" /> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="dataSource" /> 
     <property name="loadTimeWeaver"> 
     <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /> 
     </property> 
     <property name="jpaVendorAdapter"> 
     <bean id="jpaAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
     <property name="generateDdl" value="true" /> 
     <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" /> 
     <property name="showSql" value="true" /> 
     </bean> 
     </property> 
    </bean> 

    <bean id="PictureBean" class="de.navero.server.bl.PictureBean"> 
     <property name="entityManagerFactory"><ref local="entityManagerFactory" /></property> 
    </bean> 

    </beans> 

Toutes les idées ce qui peut se tromper? Merci pour toute aide :).

Répondre

6

Pouvez-vous essayer de changer la propriété generateDdl à false sur HibernateJpaVendorAdapter dans votre fichier de configuration de printemps.

Semble en conflit avec la propriété hibernate.hbm2ddl.auto de mise en veille prolongée

Voir https://jira.springframework.org/browse/SPR-6836 pour plus d'informations.

12

les opérations suivantes:


<?xml version="1.0" encoding="UTF-8"?> 
<persistence version="1.0" xmlns="http://java.sun.com/xml/ns/ 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://.sun.com/xml/ns/persistence 
    http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"> 
    <persistence-unit name="naveroTest" transaction-type="RESOURCE_LOCAL" /> 
</persistence> 

<?xml version="1.0" encoding="UTF-8"?> 
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
    <property name="jpaVendorAdapter"> 
     <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
    </property> 
    <property name="jpaProperties"> 
     <props> 
... 
      <prop key="hibernate.hbm2ddl.auto">create-drop</prop> 
     </props> 
    </property> 
    <property name="dataSource" ref="dataSource" /> 
</bean> 

Cela fonctionne pour moi.

+1

est 'persistence.xml' encore nécessaire? –

+0

persistence.xml n'est jamais requis pour les versions récentes d'Hibernate (et Spring d'ailleurs). Tout peut être fait en utilisant des annotations. – kervin

+0

@kervin: jusqu'à aujourd'hui j'ai pensé la même chose, mais au moins jusqu'à hibernate version 4.3.7 vous aurez besoin d'un 'persistence.xml' si vous utilisez des transactions JTA, et vous devez définir' JTA' pour l'attribut 'transaction-type' (@Jin Kwon: vous n'en avez donc pas besoin). Sinon, vous obtiendrez des exceptions étranges et trompeuses concernant les transactions. Il m'a juste fallu un jour pour le découvrir. Voir http://stackoverflow.com/questions/14456622/jtasessioncontext-being-used-with-jdbctransactionfactory-auto-flush-will-not-op –

1

malheureusement les deux solutions ne fonctionnait pas pour moi :(. « Hibernate.hbm2ddl.auto = mise à jour » serait aussi ok, car il doit créer les tables si elles ne sont pas présents.

Il semble que Tous les paramètres de propriété du fichier persistence.xml sont reconnus comme nom d'utilisateur et le fournisseur de base de données sont correctement définis Malheureusement, les tables ne sont pas créées au démarrage ce qui provoque l'échec de mon test car l'instruction SELECT renvoie une erreur ...

peut voir, j'ai défini l'URL de connexion pour utiliser une base de données locale, ce qui me permet de consulter le journal de la base de données après le test, mais le fichier journal est toujours vide après le test, même si aucune erreur n'y est écrite: (.

2

mise en veille prolongée Mon-default.cfg.xml i utilisé

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

et cela a fonctionné parfaitement, de sorte que le fichier de configuration est la suivante

<?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="dialect"> 
     org.hibernate.dialect.MySQLDialect 
    </property> 
    <property name="current_session_context_class">thread</property> 
    <!-- When an HQL statement declares a true/false, replace with the standard Y/N --> 
    <property name="hibernate.query.substitutions">true 'Y', false 'N'</property> 
    <!-- A useful property do debugging queries. Besure sure it is false or commented out when going PROD --> 
    <!--  <property name="hibernate.show_sql">true</property> --> 
    <!-- Format the printed out SQL generated by Hibernate --> 
    <property name="hibernate.format_sql">false</property> 
    <!-- If enabled, Hibernate will collect statistics useful for performance tuning - JMX --> 
    <property name="hibernate.generate_statistics">false</property> 

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

    <mapping class=.... 
7

J'ai eu exactement le même problème ...

Commentant la propriété

<!--property name="generateDdl" value="true"--> in the JpaAdapter, 

mais le réglage

<property name="hibernate.hbm2ddl.auto" value="create"/> in persistence.xml 

a travaillé pour moi. J'utilise hsqldb.

J'ai remarqué dans les journaux que le mode était défini pour être mis à jour peu importe ce que j'ai défini dans persistence.xml.

La valeur contenue dans persistence.xml a été récupérée mais n'a jamais été appliquée. J'utilise Spring 3.0.6 et 4 Mise en veille prolongée

-3

Dans mon cas, les tables ne sont pas été créés parce que les objets annotés avec @Table où non annotés comme @Entity

-2

Peut-être à la fin, mais aujourd'hui j'ai eu le même problème quand j'écrivais quelques tests pour une application héritée. J'utilisais la base de données spring 2.5, hibernate3 et HSQL.

Pour le résoudre, je changé la base de données HSQL à H2 et la source de données de org.springframework.jdbc.datasource.DriverManagerDataSource à org.apache.commons.dbcp.BasicDataSource.

Le printemps context.xml ressemble:

<?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: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/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd"> 

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
     <property name="driverClassName" value="org.h2.Driver"/> 
     <property name="url" value="jdbc:h2:mem:test"/> 
     <property name="username" value="sa"/> 
     <property name="password" value=""/> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"/> 
     </property> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

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

    <tx:annotation-driven/> 
... 
</beans> 

Le persistence.xml ressemble:

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 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" 
      version="1.0"> 

    <persistence-unit name="TestPU" transaction-type="RESOURCE_LOCAL"> 
     <properties> 
      <property name="hibernate.show_sql" value="true"/> 
      <property name="hibernate.format_sql" value="true"/> 
      <property name="hibernate.hbm2ddl.auto" value="create-drop"/> 
      <property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/> 
     </properties> 
    </persistence-unit> 

</persistence> 

J'espère que ça aide.

+0

Dans la question (HSQLDB) est utilisé pour exécuter les tests, dans ce cas, je suppose que changer la base de données en (H2) n'est pas un problème. –

0

Cela a fonctionné pour moi

<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="org.hsqldb.jdbcDriver" /> 
     <property name="url" value="jdbc:hsqldb:mem://productDb" /> 
     <property name="username" value="sa" /> 
     <property name="password" value="" /> 
    </bean> 
<property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="generateDdl" value="true" /> 
       <property name="showSql" value="true" /> 
      </bean> 
     </property> 

Si je change la valeur false comme ce <property name="generateDdl" value="false" /> je reçois SqlExceptionHelper:144 - Table not found in statement