-->
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.  [ 5 posts ] 
Author Message
 Post subject: table insert become update.
PostPosted: Mon Jul 18, 2005 8:50 pm 
Newbie

Joined: Thu Jul 14, 2005 7:54 pm
Posts: 11
I am trying to insert a very simple table, but everytime I run the test program(see code below), I end up updating existing record. the generated sql is a insert statement but looks like it doesn't cause mysql to generate a new PK for it. I do have auto_increment specified:


CREATE TABLE `DIGITAL_GIFTS` (
`DIGITAL_GIFT_ID` bigint(20) NOT NULL auto_increment,
`PURCHASER_CUSTOMER_ID` bigint(20) NOT NULL default '0',
`RECIPIENT_CUSTOMER_ID` bigint(20) default NULL,
`ORDER_ID` varchar(255) NOT NULL default '',
`STATUS` varchar(255) NOT NULL default '',
`PURCHASE_DATE` datetime NOT NULL default '0000-00-00 00:00:00',
`CREATED_BY` varchar(255) NOT NULL default '',
`CREATION_DATE` datetime NOT NULL default '0000-00-00 00:00:00',
`LAST_UPDATE_BY` varchar(255) default NULL,
`LAST_UPDATE` datetime default NULL,
PRIMARY KEY (`DIGITAL_GIFT_ID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1



Hibernate version:3.05

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>
<property name="connection.driver_class">org.gjt.mm.mysql.Driver</property>
<property name="connection.url">jdbc:mysql://xguo/digital-gifting</property>
<property name="connection.username">xguo</property>
<property name="connection.password"></property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="show_sql">true</property>
<property name="transaction.factory_class">
org.hibernate.transaction.JDBCTransactionFactory
</property>
<property name="cache.provider_class">
org.hibernate.cache.HashtableCacheProvider
</property>
<property name="hbm2ddl.auto">create</property>

<mapping resource="db/Gift.hbm.xml"/>

</session-factory>

</hibernate-configuration>


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">

<hibernate-mapping>
<class name="db.Gift" table="DIGITAL_GIFTS">
<id name="id" column="DIGITAL_GIFT_ID" type="long" >
<generator class="native"/>
</id>
<property name="purchaserID" column = "PURCHASER_CUSTOMER_ID" type="long" not-null="true"/>
<property name="recipientID" column = "RECIPIENT_CUSTOMER_ID" type="long"/>
<property name="orderID" column = "ORDER_ID" type="java.lang.String" not-null="true"/>
<property name="status" column = "STATUS" type="java.lang.String" not-null="true"/>
<property name="purchaseDate" column = "PURCHASE_DATE" type="timestamp" not-null="true"/>
<property name="createdBy" column = "CREATED_BY" type="java.lang.String" not-null="true"/>
<property name="creationDate" column = "CREATION_DATE" type="timestamp" not-null="true"/>
<property name="lastUpdatedBy" column = "LAST_UPDATE_BY" type="java.lang.String"/>
<property name="lastUpdateDate" column = "LAST_UPDATE" type="timestamp"/>
</class>
</hibernate-mapping>


Code between sessionFactory.openSession() and session.close():

Transaction tx = session.beginTransaction();
Date now = new Date();
Gift g = new Gift();
g.setPurchaserID(1);
g.setOrderID("ORDER123");
g.setStatus("N");
g.setPurchaseDate(now);
g.setCreatedBy("DGS");
g.setCreationDate(now);
session.save(g);
tx.commit();


Name and version of the database you are using:

mysql 4.1

The generated SQL (show_sql=true):

Hibernate: insert into DIGITAL_GIFTS (PURCHASER_CUSTOMER_ID, RECIPIENT_CUSTOMER_ID, ORDER_ID, STATUS, PURCHASE_DATE, CREATED_BY, CREATION_DATE, LAST_UPDATE_BY, LAST_UPDATE) values (?, ?, ?, ?, ?, ?, ?, ?, ?)

Debug level Hibernate log excerpt:


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 18, 2005 8:57 pm 
Newbie

Joined: Thu Jul 14, 2005 7:54 pm
Posts: 11
One thing I want to mention is that I can run following statement to insert a new record without any problem.

insert into `digital-gifting`.`DIGITAL_GIFTS`
( PURCHASER_CUSTOMER_ID, RECIPIENT_CUSTOMER_ID,
STATUS, PURCHASE_DATE, CREATED_BY, CREATION_DATE, LAST_UPDATE_BY,
LAST_UPDATE)
values
(4, 4, 'assdd', 'a', '0000-00-00 00:00:00', 'DGS', '0000-00-00 00:00:00', 'DGS', '0000-00-00 00:00:00')


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 2:27 am 
Newbie

Joined: Thu Jul 14, 2005 7:50 am
Posts: 8
</property>
<property name="hbm2ddl.auto">create</property>


try running your application again after removing above line from properties file. Because this creates a new schema everytime we run application.

This could be possible solution to your problem.

Priti


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 6:02 am 
Expert
Expert

Joined: Fri Feb 06, 2004 7:49 am
Posts: 255
Location: Moscow, Russia
Code:
<id name="id" column="DIGITAL_GIFT_ID" type="long">
<generator class="native"/>
</id>

What is the type of your id? Try to specify correct unsaved-value for your id.

Quote:
unsaved-value (optional - defaults to a "sensible" value): An identifier property value that indicates that an instance is newly instantiated (unsaved), distinguishing it from detached instances that were saved or loaded in a previous session.


unsaved-value is the parameter to distinguish if hibernate should insert of update the record.

_________________
Leonid Shlyapnikov


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 19, 2005 1:24 pm 
Newbie

Joined: Thu Jul 14, 2005 7:54 pm
Posts: 11
priti wrote:
</property>
<property name="hbm2ddl.auto">create</property>


try running your application again after removing above line from properties file. Because this creates a new schema everytime we run application.

This could be possible solution to your problem.

Priti


This is it, Thanks!


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