1

我是 Hibernate 的初学者,我正在尝试将测试数据保存到表中。每次我运行脚本时,我的数据都会被覆盖,甚至是自动生成的 ID 字段。你能帮助我吗?

系统:(win7 32,Eclipse Indigo 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>
4

1 回答 1

5

改变这个:

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

对此:

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

每次启动应用程序时,Create 都会删除并重新创建数据库。更新只会在需要时更新您的架构,但(如果可能)保持数据不变

于 2012-08-23T13:23:34.417 回答