Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get automatic table creation working in spring / hibernate / jpa?

Tags:

I can't get automatic table creation working in spring when using hibernate / jpa.

Here are my config files:

<?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>

Any ideas what may be wrong? Thanks for any help :).

like image 237
Ben Avatar asked Nov 29 '10 20:11

Ben


People also ask

Does Hibernate create tables automatically?

Hibernate framework can be used to create tables in the database automatically. Below property is added in the configuration file to create tables automatically.

Which property is used for auto creation of tables in Hibernate?

A developer can have Hibernate create tables by adding the hbm2ddl. auto property and setting it to one of the following four values: validate. update.

Can we create table using JPA?

EntityManager entityManager = entityManagerFactory. createEntityManager(); entityManager. createNativeQuery("CREATE TABLE FOO (FOO_ID NUMBER)").


1 Answers

try the following:


<?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>

This works for me.

like image 81
ytoh Avatar answered Oct 02 '22 12:10

ytoh