2012-08-23 113 views
1

我是Hibernate中的一个begginer,我试图将测试数据保存到表中。每次运行脚本时,我的数据都会被覆盖,即使是自动生成的ID字段。你可以帮我吗?休眠覆盖数据

系统:(WIN7 32时,Eclipse靛蓝SR2时,JBoss Hybernate 3.5.1)

public class testeHibernate { 

public testeHibernate() { 
} 

/** 
* @param args 
*/ 
public static void main(String[] args) { 

    Session session = SessionFactoryUtil.getSessionFactory().getCurrentSession(); 
    session.beginTransaction(); 
    Status st = new Status(); 
    st.setAbrev("CON"); 
    st.setDescricao("Consultado"); 
    st.setFim(false); 
    st.setVigente(true); 
    session.persist(st); 
    List result = session.createQuery("FROM Status").list(); 

    for(Status sta : (List<Status>) result){ 
     System.out.println(sta.getDescricao()); 
    } 

    session.getTransaction().commit(); 

} 

}

公共类SessionFactoryUtil {

private static final SessionFactory sessionFactory; 

static { 
    try { 
     // Create the SessionFactory from hibernate.cfg.xml 
     sessionFactory = new Configuration().configure().buildSessionFactory(); 

    } catch (Throwable ex) { 
     // Make sure you log the exception, as it might be swallowed 
     System.err.println("Falha na criação da SessionFactory Inicial: " + ex); 
     throw new ExceptionInInitializerError(ex); 
    } 
} 

public static SessionFactory getSessionFactory() { 
    return sessionFactory; 
} 

}

XML映射

<class name="br.com.bb.analop.classes.Status" table="analop_status"> 

    <id name="id" column="ID"> 
     <generator class="native" /> 
    </id> 

    <property name="descricao"> 
     <column name="DESCRICAO" length="30" not-null="true" /> 
    </property> 

    <property name="abrev"> 
     <column name="ABREV" length="10" not-null="true" /> 
    </property> 

    <property name="fim"> 
     <column name="FIM" length="1" not-null="true" /> 
    </property> 

    <property name="vigente"> 
     <column name="VIGENTE" length="1" not-null="true" /> 
    </property> 

</class> 

配置XML

<?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 name="sessionFactoryDB2"> 
    <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> 
    <property name="hibernate.connection.password">***classified****</property> 
    <property name="hibernate.connection.url">jdbc:mysql://**CLASSIFIED**:3306/DB2</property> 
    <property name="hibernate.connection.username">developer</property> 
    <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> 
    <property name="hibernate.current_session_context_class">thread</property> 
    <property name="hibernate.show_sql">true</property> 
    <property name="hibernate.format_sql">true</property> 
    <!-- Automatic schema creation (begin) === --> 
    <property name="hibernate.hbm2ddl.auto">create</property> 
    <!-- Simple memory-only cache --> 
    <property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property> 
    <mapping class="br.com.bb.analop.classes.Status" 
    package="br.com.bb.analop.classes" resource="br/com/bb/analop/classes/Status.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

回答

5

更改此:

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

这样:

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

每次启动应用程序时创建drop并重新创建数据库。更新只会在需要时更新您的模式,但是(如果可能的话)保留数据不变

+0

好极了!而已! – Alex