-->
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.  [ 4 posts ] 
Author Message
 Post subject: Hibernate Mapping issue
PostPosted: Thu Oct 16, 2008 2:41 am 
Newbie

Joined: Thu Oct 16, 2008 2:08 am
Posts: 3
Hi, I have an issue in Hibernate mapping...

i am using Informix database. And i am try to map two tables. In the first table i am having the foreign key of the second table. When i am inserting a row in the second table and insert the foreign key in the first table, it inserts a zero(0).

[code]
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class table="tab1" name="tab1">

<id name="catExtSrlNo" type="long" column="CAT_EXT_SRL_NO">
<generator class="identity"/>
</id>


<property name="lastUpdatedUser" type="string" not-null="false" column="LAST_UPD_USER"/>
<property name="lastUpdatedTimestamp" type="timestamp" not-null="false" column="LAST_UPD_DT"/>

<many-to-one name="tab2" class="tab2" column="EXT_SRL_NO" not-null="true" lazy="true" cascade="all"/>


</class>
</hibernate-mapping>
[/code]



the ext_srl_no is an auto incremented column. it is the foriegn key of the tab1 . I call the hibernate.save(tab1) and insert a value in tab2 and then insert the foriegn key in tab1. but it is inserting zero(0). can i know why zero is getting inserted.

thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 4:14 am 
Beginner
Beginner

Joined: Tue Sep 09, 2008 5:42 am
Posts: 22
Location: Romania
Ciao,

I guess your mapping is not right. You say that "ext_srl_no" is an auto incremented column but in the same time is a foreign key. If you map tab1 then you can only reference columns from that table. So if on tab1 you have a column that references a foreign key then it cannot be auto incremented.

I think your classes and mappings should look something like this:

Tab2 class:

Code:
public class Tab2
{
private int id;
private string description;

public virtual int IdTab2
{
set{id=value;}
get{return id;}
}

public virtual string Description
{
set{description=value;}
get{return description;}
}
}


Tab2 mapping:

Code:
<hibernate-mapping mapping-2.2">
  <class name="Tab2" table="Tab2" lazy="true">
    <id name="IdTab2" type="System.Int16" column="IdTab2" unsaved-value="0">
      <generator class="native" />
    </id>
    <property name="Description" type="System.String" column="Description" not-null="true" length="50" />
</class>
</hibernate-mapping>


Tab1 class:

Code:
public class Tab1
{
private int id;
private Tab2 referencedObject;

public virtual int IdTab1
{
set{id=value;}
get{return id;}
}

public virtual Tab2 Tab2ReferencedObject
{
set{referencedObject=value;}
get{return referencedObject;}
}
}


Tab1 mapping:

Code:
<hibernate-mapping mapping-2.2">
  <class name="Tab1" table="Tab1" lazy="true">
    <id name="IdTab1" type="System.Int16" column="IdTab1" unsaved-value="0">
      <generator class="native" />
    </id>
    <many-to-one name="Tab2ReferencedObject" class="Tab2">
      <column name="ColumnNameFromTab1ReferencingTab2"/>
    </many-to-one>
</class>
</hibernate-mapping>


The "ColumnNameFromTab1ReferencingTab2" column should not be auto-incremented.

I hope this helps. Good luck!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 9:17 pm 
Newbie

Joined: Thu Oct 16, 2008 2:08 am
Posts: 3
Hi,

thank you for the answer. I think you did not understand my question. the ext_srl_no is an auto incremented field in tab2 and not in tab1 and it is inserted in tab1.

Anyway i solved the problem. i had to give the generator class as "increment" for both the tables' mapping file.

thanks and regards
prasram83


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 16, 2008 9:18 pm 
Newbie

Joined: Thu Oct 16, 2008 2:08 am
Posts: 3
Hi,

thank you tomaandtoma for the answer. I think you did not understand my question. the ext_srl_no is an auto incremented field in tab2 and not in tab1 and it is inserted in tab1.

Anyway i solved the problem. i had to give the generator class as "increment" for both the tables' mapping file.

thanks and regards
prasram83


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