These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Why only one row added running the these codes under Eclipse
PostPosted: Sun Jul 04, 2010 9:09 pm 
Newbie

Joined: Sun Jul 04, 2010 8:51 pm
Posts: 3
The key codes are bellow. I run these codes under Eclipse many times, but only one record is added to DB.
It seems that the new record is added into DB when first time running. Other times, it only update the same record in DB.
But the sql showed by Hibernate is always "insert into department values(?,?,?)". What happened? How to add one new row every time?

POJO:
Code:
public class Department
{
    private int deptID;
    private String deptName;
    private int manager;
    private String address;
   //getters and setters........
}

HBM XML file:
Code:
<hibernate-mapping package="com.tt.orm">
  <class name="com.tt.orm.Department" table="department">
    <id name="deptID" column="DeptID" >
        <generator  class="increment"/>
    </id>
    <property name="deptName"/>
    <property name="manager"/>
    <property name="address"/>
  </class>
</hibernate-mapping>


Test Code:
Code:
public static void main(String[] args)
    {
        SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

        Session session = sessionFactory.openSession();

        session.beginTransaction();
        Department dept = new Department();
        //dept.setDeptID(3);
        dept.setAddress("test1 address");
        dept.setDeptName("HR1-1");

        session.save(dept);

        session.getTransaction().commit();
        session.close();
    }


I'm a beginner for Hibernate. Thank you. Any help is welcome.


Last edited by Tacker_Huang on Wed Jul 07, 2010 6:39 am, edited 1 time in total.

Top
 Profile  
 
 Post subject: Re: Why only one row added running the these codes under Eclipse
PostPosted: Wed Jul 07, 2010 6:24 am 
Newbie

Joined: Sun Jul 04, 2010 8:51 pm
Posts: 3
Anyone can help? Thank you the most!


Top
 Profile  
 
 Post subject: Re: Why only one row added running the these codes under Eclipse
PostPosted: Wed Jul 07, 2010 8:16 am 
Newbie

Joined: Wed Sep 24, 2008 12:48 pm
Posts: 6
tacker,

Could it be that you're recreating the db schema every time you run that code? it might help if you show how you configured hibernate.


Top
 Profile  
 
 Post subject: Re: Why only one row added running the these codes under Eclipse
PostPosted: Wed Jul 07, 2010 8:56 pm 
Newbie

Joined: Sun Jul 04, 2010 8:51 pm
Posts: 3
Hi, Augusto, thank you. The hibernate configuration is as bellow:
Code:
<?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>
        <property name="hibernate.connection.driver_class">org.gjt.mm.mysql.Driver</property>
        <property name="hibernate.connection.password">root</property>
        <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testmysql</property>
        <property name="hibernate.connection.username">root</property>
        <property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
       
        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
      <property name="hibernate.default_schema"></property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">create</property>

        <mapping resource="DepartmentMapping.hbm.xml"/>
        <!-- mapping resource="EmployeeMapping.hbm.xml"/-->
       
    </session-factory>
</hibernate-configuration>



According to your reply, i found the key reason is " <property name="hbm2ddl.auto">create</property>". I changed it to "validate" and the problem is resolved.

Thank you very much.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.