我是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 mapping
<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>
答案 0 :(得分:5)
改变这个:
<property name="hibernate.hbm2ddl.auto">create</property>
到此:
<property name="hibernate.hbm2ddl.auto">update</property>
每次启动应用程序时创建drop并重新创建数据库。更新只会在需要时更新您的架构,但(如果可能)保持数据不变