aspose file tools*
The moose likes Object Relational Mapping and the fly likes Hibernate: new insert always overrides previous data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Hibernate: new insert always overrides previous data" Watch "Hibernate: new insert always overrides previous data" New topic
Author

Hibernate: new insert always overrides previous data

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
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

Joined: Dec 29, 2005
Posts: 308
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 ;)


aldana software engineering blog & .more
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61007
    
  65

Moved to the ORM forum where Hibernate and other ORM tools are discussed.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Naseem Khan
Ranch Hand

Joined: Apr 25, 2005
Posts: 809
Change your code like this....




Naseem


Asking Smart Questions FAQ - How To Put Your Code In Code Tags
PK Ghosh
Greenhorn

Joined: Aug 08, 2006
Posts: 24
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

Joined: Jul 18, 2006
Posts: 148
you're the best!!!

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

resolved my problem. Thanks for your insight.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Hibernate: new insert always overrides previous data