Alex
Alex

Reputation: 3413

Hibernate overwrites data

I'm a begginer in Hibernate, and I'm trying to save test data into a table. Everytime I run the script, my data is overwritten, even the ID field, which is autogenerated. Can you help me?

System: (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();

}

}

public class 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>

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

Upvotes: 1

Views: 509

Answers (1)

Shivan Dragon
Shivan Dragon

Reputation: 15219

Change this:

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

to this:

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

Create drops and recreates your database each time you start the application. Update will only update your schema if needed, but (if possible) leave the data untouched

Upvotes: 5

Related Questions