0

If it is duplicate, my apology. I could not find an answer for my question.

I just start Hibernate. I have an issue is every time the program automatically delete the old shceme and creates new when it is running. For example, If I want to add records into database, I could not do it because the scheme will be recreate, so the histories will be deleted.

Here is my hbm.xml mapping file:

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 22-Oct-2013 1:39:31 PM by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
    <class name="net.ys.hibernate.Equip" table="EQUIP">
        <id name="id" type="int">
            <column name="ID" />
            <generator class="native" />
        </id>
        <property name="dis" type="java.lang.String" column="dis" />

        <property name="ref" type="java.lang.String" column="ref" />

        <property name="type" type="java.lang.String" column="type" />

    </class>
</hibernate-mapping>

hibernate.cfg.xml 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>

        <!-- hibernate dialect -->

        <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="hibernate.connection.password">pw</property>

        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/explorer_DB?useUnicode=true&amp;characterEncoding=GBK</property>
        <property name="hibernate.connection.username">username</property>
        <property name="hibernate.default_schema">explorer_DB</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</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>
        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- mapping files with external dependencies -->
        <mapping resource="net/ys/hibernate/Equip.hbm.xml"/>


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

And addEquip method:

public Integer addEquip(String dis, String ref, String type){
        Session session = sessionFactory.openSession();
        currentSession = sessionFactory.getCurrentSession();
        Transaction tx = null;
        Integer equipID = null;

        try {
            tx = currentSession.beginTransaction(); //start a transaction

            Equip equip = new Equip(dis,ref,type);
            equipID = (Integer)currentSession.save(equip);
            tx.commit();
        } catch (HibernateException e) {
            if(tx!=null) tx.rollback();
            e.printStackTrace();
        } finally {
            session.close();
        }

        return equipID;

    }
}

could someone help me to solve this issue? I just don't understand how to use getCurrentSession(), probably I am wrong in this point. Could you explain how hibernate works when we call getCurrentSession() for me? I really appreciate it.

Thank you so much

Eric
  • 1,271
  • 4
  • 14
  • 21
  • It seems you are creating session factory every time, could you check that? – Admit Oct 23 '13 at 16:14
  • @Admit Hi Admit, in the addEquip(), open the openSession() first, and get CurrentSession. After that, calling currentSession.beginTransaction() starts a transaction. However, changing the create to none solves problem. – Eric Oct 23 '13 at 16:30
  • you also could remove that, if you don't need schema creation at all(which is equals to none value). take a look at place where you create `sessionFactory`, since creating new factory for every request seems not efficient, and it seems it's a root of a problem. – Admit Oct 23 '13 at 16:35
  • @Admit Yes, I do create sessionFactory in the main function. When program is running, it will create a sessionFactory. So the addEquip, listEquip, and updateEquip will define openSession() and getCurrentSession(). Basically, what's the logic when we use Hibernate. I don't have any exprience with it but I have some in JDBC. Thank you – Eric Oct 23 '13 at 16:43

1 Answers1

1

Change this property in your hibernate.cfg.xml file

current configuration

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

to

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

if no change required in database use.

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

More on this please refer this link

Hibernate hbm2ddl.auto possible values and what they do?

Community
  • 1
  • 1
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • Hi ByteCode, Thank you, it works. But It doesn't work when I don't have the scheme at the first time. Is there any method that can check database, if we don't have scheme, it will create scheme automatically. if we have it, then do add, delete, or update without no automatic creation. – Eric Oct 23 '13 at 16:34