-->
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.  [ 1 post ] 
Author Message
 Post subject: Hibernate with Sybase SQL Anywhere dialect
PostPosted: Thu Dec 11, 2008 11:56 am 
Newbie

Joined: Wed Dec 10, 2008 2:57 pm
Posts: 1
We are using hibernate with a Sybase SQLAnywhere dialect.

We are using the hibernate mappings below for two objects:
CaseInfoReq (parent) and CaseInfoReqPh (child)

The child CaseInfoReqPh table has a foreign key column that contains the key id of its parent CaseInfoReq.
Because we are running the application on many remote machines that sync up their database tables to
one central database, we need to manage the key ids in the parent CaseInfoReq table so they are unique.
Therefore, when an insert is done on the parent table on the remote database, we have a trigger that will
then get called which gets a unique key and sets it in the id column. We cannot make the id column an
autoincrement or identity column because we want the trigger to assign the key id only to inserts that
have an id column of NULL or 0. (This is because we can also have some inserts that will have ids
and we do not want to change the id for them.)

We are using Sybase SQL Anywhere for our remote databases.
On our remote SQL Anywhere database, when we do an insert with hibernate session.saveOrUpdate on our
CaseInfoReq instance (parent) that has a CaseInfoReqPh (child), the unique key id is assigned for the
CaseInfoReq (parent) insert, and then hibernate uses @@ identity to get the
last inserted key id so it can use it as the foreign key in the CaseInfoReqPh (child) insert.

Since @@identity will only work with autoincrement or identity columns and our id column cannot be
autoincrement or identity, the @@identity returns 0 for the foreign key and an exception is thrown.
(See debug below)

Is it possible to alter the behavior of hibernate for a table (in a configuration file) so that we can tell
it not to use @@identity to get the key id to use with the child insert and instead get or use a
value that we specify? (The @@identity is used by hibernate with Sybase to get the last inserted key).

In other words, how can hibernate insert a parent and child when the parent table does not have autoincrement
on the primary key?

<hibernate-mapping>
<class name="CaseInfoReq" table="case_info_req">
<id name="id" type="int">
<column name="id" />
<generator class="native" />
</id>
<property name="idCase" type="java.lang.Integer">
<column name="id_case" />
</property>
...

<list name="phones" inverse="true"
cascade="all,delete,delete-orphan" lazy="false">
<key>
<column name="id_case_info_req" not-null="true" />
</key>
<list-index base="1" column="phone_sequence" />
<one-to-many class="CaseInfoReqPh" />
</list>

</class>
</hibernate-mapping>

<hibernate-mapping>
<class name="CaseInfoReqPh" table="case_info_req_ph" >
<id name="id" type="int">
<column name="id" />
<generator class="native" />
</id>
<many-to-one name="caseInfoReq" column="id_case_info_req" not-null="true"/>

<property name="phoneTypeCd" type="java.lang.Character">
<column name="phone_type_cd" length="1" />
</property>
...
</class>
</hibernate-mapping>


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.