nuke90
nuke90

Reputation: 3

Opening a new session in hibernate cancels table rows

when i execute this code:

SessionFactory fact=new Configuration().configure().buildSessionFactory();

Session session=fact.openSession();
Transaction tx = session.beginTransaction(); //start transaction

No matter what i do after that, it deletes EVERY ROWS from EVERY TABLE in the database, what can it be?

Here's the hibernate configuration file

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

    <!-- Database connection settings -->
    <property name="connection.driver_class">com.ibm.db2.jcc.DB2Driver</property>
    <property name="connection.url">jdbc:db2://localhost:50000/db2</property>

    <property name="connection.username">db2admin</property>
    <property name="connection.password">db2admin</property>
    <property name="connection.pool_size">1</property>

    <property name="dialect">org.hibernate.dialect.DB2Dialect</property>
    <!-- <property name="dialect">org.hibernate.dialect.HSQLDialect</property> -->
    <!-- <property name="dialect">org.hibernate.dialect.MySQLDialect</property> -->

    <property name="current_session_context_class">thread</property>
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="show_sql">true</property>
    <property name="hbm2ddl.auto">create</property>

    <!-- proprietà per il mapping delle classi-->
    <mapping resource="hibernate/Studenti.hbm.xml"/>
    <mapping resource="hibernate/Cani.hbm.xml"/>

  </session-factory>
</hibernate-configuration>

The mapping works, adding rows works, the problem is that everytime i execute the code, it deletes every previous data on the tables.

Upvotes: 0

Views: 64

Answers (2)

Mitul Maheshwari
Mitul Maheshwari

Reputation: 2647

you just have to change the following property of your hibernate configuration file.

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

instead of this put Update :-

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

That will definitely solve your problem.

Upvotes: 1

JB Nizet
JB Nizet

Reputation: 691865

You set the property hbm2ddl.auto to create. This means: create the database schema everytime the session factory is built. Just remove that property, and Hibernate won't recreate the schema every time.

Upvotes: 0

Related Questions