File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate: new insert always overrides previous data

 
Nina Anderson
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys

I'm new to using Hibernate.

My application is trying to INSERT data into my "Country" database table. Each time I execute the application, the insert is successful. However, the new data overrides the previous data and I'm wondering why a new record is not created each time I do an insert.

I'll appreciate your insight.

Here's my code:

##############################

LoginAction.java

package com.testWebApp.actions;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.admin.domain.Country;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


/**
* Form bean for a Struts application.
* Users may access 2 fields on this form:
* <ul>
* <li>passWord - [your comment here]
* <li>userName - [your comment here]
* </ul>
* @version 1.0
* @author
*/
public class LoginAction extends Action {

public ActionForward execute(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws Exception {

////BEGIN: This is a test to add data in the database

try{

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
Session session = sessionFactory.openSession();

//Create new instance of Contact and set values in it by reading them from form object
Country country = new Country();
country.setCountryID(258); //I change this value each time
country.setCountryCode("183"); //I change this value each time
country.setCountryDesc("Test Nina country3"); //I change this value each time
//country.setFirstName((String)contactForm.get("firstName"));
session.save(country);

// Actual country insertion will happen at this step
session.flush();
session.connection().commit();
session.close();

} catch (Exception ex)
{
System.out.println("!! EXECEPTION !!: " + ex);
}


////END
System.out.println("Forwarding - SUCCESS page");

return mapping.findForward("success");
}


}

########################################################

hibernate.cfg.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>
<property name="hibernate.connection.driver_class">
com.ibm.db2.jcc.DB2Driver</property>
<property name="hibernate.connection.url">jdbc:db2://localhost:50000/testDB</property>
<property name="hibernate.connection.username">db2admin</property>
<property name="hibernate.connection.password">db2admin</property>
<property name="hibernate.connection.pool_size">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.DB2Dialect</property>
<property name="hibernate.hbm2ddl.auto">create-drop</property>
<mapping resource="Country.hbm.xml"/>
</session-factory>
</hibernate-configuration>


######################################################

Country.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping>
<class name="com.admin.domain.Country" table="country">
<id name="countryID" type="java.lang.Integer" column="COUNTRY_ID">
<generator class="assigned"/>
</id>
<property
name="countryCode"
type="java.lang.String"
column="COUNTRY_CODE"
not-null="false"
/>
<property
name="countryDesc"
type="java.lang.String"
column="COUNTRY_DESC"
not-null="false"
/>
<property
name="createdBy"
type="java.lang.String"
column="CREATED_BY"
not-null="false"
insert="false"
update="false"
/>
<property
name="creationTimeStamp"
type="java.sql.Timestamp"
column="CREATION_TIMESTAMP"
not-null="false"
insert="false"
update="false"
/>
<property
name="lastUpdatedBy"
type="java.lang.String"
column="LAST_UPDATED_BY"
not-null="false"
insert="false"
update="false"
/>
<property
name="lastUpdatedTimeStamp"
type="java.sql.Timestamp"
column="LAST_UPDATED_TIMESTAMP"
not-null="false"
insert="false"
update="false"
/>
</class>

</hibernate-mapping>
<!-- parsed in 0ms -->

#############################################################

Thanks in Advance!!!
 
manuel aldana
Ranch Hand
Posts: 308
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
two guesses (from your provided information):

1) what about the values inside your database. do they change and does the id change appropriately to your new source-code ID after each new insert?
if not: maybe you forgot to deploy the application on your tomcat/app-server
if yes: have a look at 2)

2) maybe you somehow are dropping and re-creating tables after every new compile and restart of your webapp, so the tables are initially empty.


in general this is not a very good way to test your app, by changing values inside source code. believe it is a pain. invest some effort into testing and build harness and you will see how easy live is. remember that doing things manually which can be automated end in many mistakes and takes much longer. computers love to help you with that ;)
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64196
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moved to the ORM forum where Hibernate and other ORM tools are discussed.
 
Naseem Khan
Ranch Hand
Posts: 809
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Change your code like this....




Naseem
 
PK Ghosh
Greenhorn
Posts: 24
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think problem is here:

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

take it off this "create-drop" when you are getting a session object, virtually resetting you mapping table and creating a new one.

try this:

<property name="hibernate.hbm2ddl.auto">update</property>
 
Nina Anderson
Ranch Hand
Posts: 148
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you're the best!!!

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

resolved my problem. Thanks for your insight.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic