Announcement Announcement Module
Collapse
No announcement yet.
Hibernate : Empty Table recreated when spring starts Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Hibernate : Empty Table recreated when spring starts

    Each time I'm starting Tomcat/Spring, hibernate recreates the mapped table (only one table is mapped).
    Below the applicationcontext and the Entity Class.

    Many thanks in advance.


    PHP Code:
    <?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"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:task="http://www.springframework.org/schema/task"
           xsi:schemaLocation="
               http://www.springframework.org/schema/beans 
               http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
               http://www.springframework.org/schema/tx 
               http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
               http://www.springframework.org/schema/context 
               http://www.springframework.org/schema/context/spring-context-3.0.xsd
               http://www.springframework.org/schema/task 
               http://www.springframework.org/schema/task/spring-task-3.0.xsd">           
               
        <context:component-scan base-package="com.stockdomain">
            <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        </context:component-scan>           

        <!-- dataSource -->
        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
          <property name="driverClassName"><value>org.postgresql.Driver</value></property>
          <property name="url"><value>jdbc:postgresql://localhost/db_finance</value></property>
          <property name="username"><value>xxxx</value></property>
          <property name="password"><value>xxxx</value></property>
        </bean>  
      
        <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
    <!--        <property name="packagesToScan" value="com.stockdomain.domain" />-->
            <property name="annotatedClasses">
                 <list>
                       <value>com.stockdomain.domain.Stock</value>
                 </list>
             </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">create</prop>
                </props>
            </property>
        </bean>
        
        <bean id="txManager"
            class="org.springframework.orm.hibernate3.HibernateTransactionManager">
            <property name="sessionFactory">
                    <ref bean="sessionFactory" />
            </property>
        </bean>
        
        <!-- Enable the configuration of transactional behavior based on annotations -->
        <tx:annotation-driven transaction-manager="txManager"/>             
        
    </beans>

    PHP Code:
    package com.stockdomain.domain;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;

    @
    Entity
    @Table(name "stock")
    public class 
    Stock {

        @
    Id
        
    @GeneratedValue
        
    private int id;
        @
    Column
        
    private String code;
        @
    Column
        
    private String name;
        @
    Column
        
    private String isin;
        @
    Column
        
    private String exchangeCode;
        @
    Column
        
    private String countryCode;
        @
    Column
        
    private String subSectorCode;
        @
    Column
        
    private String active;
        @
    Column
        
    private String liquid;

        public 
    void setId(int id) {
            
    this.id id;
        }

        public 
    int getId() {
            return 
    id;
        }

        public 
    String getCode() {
            return 
    code;
        }

        public 
    void setCode(String code) {
            
    this.code code;
        }

        public 
    String getName() {
            return 
    name;
        }

        public 
    void setName(String name) {
            
    this.name name;
        }

        public 
    String getIsin() {
            return 
    isin;
        }

        public 
    void setIsin(String isin) {
            
    this.isin isin;
        }

        public 
    String getExchangeCode() {
            return 
    exchangeCode;
        }

        public 
    void setExchangeCode(String exchangeCode) {
            
    this.exchangeCode exchangeCode;
        }

        public 
    String getCountryCode() {
            return 
    countryCode;
        }

        public 
    void setCountryCode(String countryCode) {
            
    this.countryCode countryCode;
        }

        public 
    String getSubSectorCode() {
            return 
    subSectorCode;
        }

        public 
    void setSubSectorCode(String subSectorCode) {
            
    this.subSectorCode subSectorCode;
        }

        public 
    String getActive() {
            return 
    active;
        }

        public 
    void setActive(String active) {
            
    this.active active;
        }

        public 
    String getLiquid() {
            return 
    liquid;
        }

        public 
    void setLiquid(String liquid) {
            
    this.liquid liquid;
        }



  • #2
    Found the reason : <prop key="hibernate.hbm2ddl.auto">create</prop>

    When I remove this property, the table is not recreated each time Spring starts.
    Last edited by fv967; Sep 17th, 2011, 03:04 AM.

    Comment


    • #3
      Problem is solved
      Last edited by fv967; Sep 17th, 2011, 03:05 AM.

      Comment

      Working...
      X