-->
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.  [ 12 posts ] 
Author Message
 Post subject: Updating instead of inserting
PostPosted: Tue Sep 05, 2006 2:48 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

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

<hibernate-mapping>

<class name="com.ranlogic.prueba.Vehicle" table="vehiculos">
<id name="id" column="veh_id">
<generator class="identity"/>
</id>
<property name="name" column="veh_marca"/>
<property name="price" column="veh_valor"/>
</class>

</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():
Configuration cfg = new Configuration();
cfg.addClass(obj.getClass());
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(obj);
session.getTransaction().commit();
Full stack trace of any exception that occurs:

Name and version of the database you are using:

The generated SQL (show_sql=true):
Hibernate: insert into vehiculos (veh_marca, veh_valor) values (?, ?)
Debug level Hibernate log excerpt:

Problems with Session and transaction handling?

Read this: http://hibernate.org/42.html

Well. My problem is that every time I save an object (Vehicle in this case) Hibernate UPDATES the tabla instead of inserting a new record.
I have copied a singleton class called HibernateUtil, with a method getSessionFactory.

In the main method I wrote this:
Code:
Vehicle veh = new Vehicle();
veh.setId((long)5);
veh.setName("Vehicle 3");
veh.setPrice(25000.0);
      
HibernateTest.save(veh);


The save method:
Code:
public static void save(Object obj){
   configurate(obj);
   Session session = HibernateUtil.getSessionFactory().getCurrentSession();
   session.beginTransaction();
   session.save(obj);
   session.getTransaction().commit();
}


And, finally, the configurate method:
Code:
private static void configurate(Object obj){
   Configuration cfg = new Configuration();
   cfg.addClass(obj.getClass());
}


The veh_id field is identity in MySQL (PK, not null and autoincrement).
I don't know why Hibernate performs an Update instead an Insert.

By the way, also deletes all previous records


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 05, 2006 5:49 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
If you are wanting to insert and you have an identity column defined in the hibernate mapping, you don't need this

veh.setId((long)5);

Let the identity set the id for you. Since you are providing an ID hibernate thinks you are updating an existing record.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 9:34 am 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Sorry, I have added that to try if the identity has something to do with my problem.
Originally that line wasn't there.
I have removed again and I'm having the same problem.

The table "vehiculos" contains 5 records. After I run my program, (without the line setId((long)5) the table remains with only 1 record (the one I inserted in my program), and no mather how many times I try to insert, always remains with 1 (with the values updated, of course).

It is something else I have to consider?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 10:30 am 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
I don't see any reason why records would get deleted. If you don't see any delete sql in the logs, then the records should not go anywhere. Are you sure you don't have initialization code that is running somewhere to empties the table? Any other plain JDBC that is running that would not make it into the server log?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 10:30 am 
Newbie

Joined: Wed Sep 06, 2006 7:03 am
Posts: 3
The one thing I can sugest is setting:
<generator class="native" />
in you maping file.
Hope this helps.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 10:49 am 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
I don't think setting the generator class=native will do anything. All that does is let Hibernate decide what generator class to use. It will still pick identity because it will know that the column is set to identity in the database (I am assuming you are using DB2, MySQL, Informix, Sybase, HSQLDB, HypersonicSQL or SQL Server)


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 11:35 am 
Expert
Expert

Joined: Tue Jul 11, 2006 10:21 am
Posts: 457
Location: Columbus, Ohio
You wouldn't happen to have hbm2ddl.auto turned on in your cfg.xml would you? Post cfg.xml please.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 11:51 am 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
This is my hibernate.cfg.xml:
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>

        <!-- Database connection settings -->
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="connection.url">jdbc:mysql://localhost:3306/leandro</property>
      <!--
        <property name="connection.driver_class">com.microsoft.jdbc.sqlserver.SQLServerDriver</property>
        <property name="connection.url">jdbc:microsoft:sqlserver//localhost:1433;databaseName=Hibernate;SelectMethod=Cursor;</property>
        -->
        <property name="connection.username">root</property>
        <property name="connection.password"></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect -->
        <property name="dialect">org.hibernate.dialect.MySQLDialect</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="com/ranlogic/prueba/Vehicle.hbm.xml" />
       

   </session-factory>

</hibernate-configuration>


I have been debugin trhough the Hibernate code, and I have identified the exact moment when the data is erased, it's in the class SessionFactoryImpl.class at the line 295:

Code:
      if ( settings.isAutoCreateSchema() ) new SchemaExport(cfg, settings).create(false, true);
      if ( settings.isAutoUpdateSchema() ) new SchemaUpdate(cfg, settings).execute(false, true);
      if ( settings.isAutoValidateSchema() ) new SchemaValidator(cfg, settings).validate();
      if ( settings.isAutoDropSchema() ) schemaExport = new SchemaExport(cfg, settings);


After those lines, my table becomes empty.
Any ideas?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 12:11 pm 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
Ananasi wrote:
You wouldn't happen to have hbm2ddl.auto turned on in your cfg.xml would you? Post cfg.xml please.

You're right, i change from "create" to "update" and now is working ok!

Thanks!


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 06, 2006 4:09 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
Also you should change your dialect in your config file from MySQL to SQLServer since you are using SQLServer according to your driver.

from this

Code:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>


to this

Code:
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 10:20 am 
Beginner
Beginner

Joined: Tue Sep 05, 2006 2:36 pm
Posts: 24
smithbstl wrote:
Also you should change your dialect in your config file from MySQL to SQLServer since you are using SQLServer according to your driver.

from this

Code:
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>


to this

Code:
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>

No, I'm using MySQL, you can see in the config file that the SQLServer is between comment tags.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 07, 2006 12:33 pm 
Beginner
Beginner

Joined: Fri Jul 28, 2006 12:01 pm
Posts: 21
You're right, sorry for the confusion, I was not looking at it carefully enough ite seems.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 12 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.