-->
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.  [ 2 posts ] 
Author Message
 Post subject: Hibernate do an UPDATE instead of INSERT
PostPosted: Tue Dec 16, 2008 6:49 am 
Newbie

Joined: Tue Dec 16, 2008 6:06 am
Posts: 1
Hi everyone,

I'm having a problem when I want to insert something in my database.

Here are the two java classes I'd like to save in database :

Source code :
Code:
package com.myPackage;

import java.io.Serializable;

public class MyClassA implements Serializable{
   private static final long serialVersionUID = -8832112548535849481L;
   private long id;
   private MyclassB myclassB;
   
   public MyClassA(){
      super();
   }
   
   public long getid() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }

   public long getMyclassB() {
      return myclassB;
   }
   public void setMyclassB(long myclassB){
      this.myclassB  = myclassB;
   }      
}


Code:
package com.myPackage;

import java.io.Serializable;

public class MyClassB implements Serializable{
   private static final long serialVersionUID = -8812584561535849481L;
   private long id;
   private String myField;

   public MyClassB(){
      super();
   }

   public long getid() {
      return id;
   }
   public void setId(long id) {
      this.id = id;
   }

   public long getMyField() {
      return myField;
   }
   public void setMyField(long myField){
      this.myField  = myField;
   }      
}


Hibernate mapping :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate mapping DTD 3.0//EN"
"packages/resources/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.myPackage">
   <class name="MyClassA" table="MYCLASSA" lazy="false">
      <id  name="id" type="long">
         <generator class="sequence">
            <param name="sequence">ssr_id_seq</param>
         </generator>
      </id>
        <one-to-one name="myClassB" column="MYCLASSB_FK" class="com.myPackage.MyClassB" cascade="all"/>
   </class>
</hibernate-mapping>


Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate mapping DTD 3.0//EN"
"packages/resources/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.myPackage">
   <class name="MyClassB" table="MYCLASSB" lazy="false">
      <id  name="id" type="long">
         <generator class="sequence">
            <param name="sequence">pm_id_seq</param>
         </generator>
      </id>
        <property name="myField" type="java.lang.String">
            <column name="MyField" not-null="true" unique="false" sql-type="VARCHAR2(255)"/>
        </property>   
   </class>
</hibernate-mapping>


When I want to save "MyClassA", log files tells me that it has been trying to update "MYCLASSA", but there is nothing to update in DB, while I want the application to insert MYCLASSA (and indeed MYCLASSB) in DB.



Here is my configuration:
Hibernate : 3.0
Database : Oracle 10.2.0.3
Web server : Jonas 4.8.4
IDE : Eclipse 3.4.1
Struts application


Can anybody help me ?
Thanks


Top
 Profile  
 
 Post subject: Attribute name missing?
PostPosted: Fri Jan 02, 2009 10:04 am 
Newbie

Joined: Thu Oct 16, 2008 3:10 pm
Posts: 1
Location: Charleston, SC
Looking at your first class.hbm.xml it looks like your missing the column definition that tells Hibernate where to put the Object ID your getting from the sequence. I'm not using a sequence, but his works for me:

Code:
<class name="class1" table="CLASS1">
    <id name="id" type="java.lang.String" column="CLASS1_ID" node="@id"/>
    <property name="attr2" type="java.lang.Long" column="ATTR2"/>
    ...


I am very new to this, but I hope this helps.

Jim


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