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.  [ 6 posts ] 
Author Message
 Post subject: Oracle sequence and mapping to multiple columns in table
PostPosted: Fri Dec 12, 2008 9:44 am 
Newbie

Joined: Fri Dec 12, 2008 9:24 am
Posts: 2
This might be a total newbee question, but I've been searching for hours and can't seem to find anything on this.

Were doing a test project on moving from all Oracle SP to NHibernate.
In our insert_customer(v_firstname, v_lastname, etc.) we use a sequence(CUSTOMER_ID_SEQ) to generate the pk(customer_id).

Problem is:

The sequence is not used directly, its appended a matchcode.
The sequence is also used to set the external_id column if external_id is not supplied.

Per now i have:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
    namespace="xxx.Core.Domain" assembly="Core">

    <class name="Customer" table="CUSTOMER" lazy="false">
        <cache usage="read-write"/>
        <id name="Id" type="long" column="CUSTOMER_ID" unsaved-value="0">
            <generator class="sequence">
                <param name="sequence">CUSTOMER_ID_SEQ</param>
            </generator>
        </id>
        <property name="FirstName">
            <column name="FIRSTNAME"/>
        </property>
        <property name="LastName">
            <column name="SURNAME"/>
        </property>
        <property name="Adresse">
            <column name="DM_ADDRESS_1"/>
        </property> etc etc.


But i cant seem to figure out how to use the sequence to update external_id without generating a new number(obviously wrong) or how to append to the sequence number before inserting.

I'm thinking this can be implemented either by calling a SP or possibly by querying the db for sequence.nextval() and updating the properties before persisting, as .net does not have the Sequence value at the time the sql is generated.

But i was hoping hibernate supplied a more clean solution as mine sort of makes the move to hibernate kinda a mute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 12, 2008 10:46 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
You can specify custom sql statements for insert/update:

http://www.hibernate.org/hib_docs/nhibernate/1.2/reference/en/html/querysql.html#querysql-cud

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 14, 2008 6:35 am 
Newbie

Joined: Fri Dec 12, 2008 9:24 am
Posts: 2
Thanks for the replay, but i already know that.
It might be possible to solve the matchcode problem with this, but how does it solve the sequence problem?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 2:57 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I would use a trigger for that. If you don't want that, you probably can write a update statement that calls some thing like external_id = NVL(?, sequence.nextval()).

_________________
--Wolfgang


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 9:41 am 
Newbie

Joined: Fri Jan 19, 2007 4:13 am
Posts: 6
Location: Germany / Osnabrueck
another way could be in the EO classes, by changing the propperty for your external_id column, to return the customer_id, when the external_id is still 0.

when i understan hibernate correctly, it will first check if the customer_id is 0, when yes, then a new id will be taken from the sequence and pushed to the variable. After this, hibernate will get all the values from the variables and place them into an insert statement. At this time, the customer_id is already filled, and when hibernate asks your property for the external_id, it will return the new customer_id (when the external_id was 0) and the insert should contain the value that you want.

As I haven't tested this right now, I can not guarantee this works, but should.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 15, 2008 9:56 am 
Expert
Expert

Joined: Thu Dec 14, 2006 5:57 am
Posts: 1185
Location: Zurich, Switzerland
I'm afraid that won't work that way. When you use a database based identity, hibernate will do the insert as soon as you call session.Save() in order to obtain the id. But it also inserts the other values with that call, because otherwise you wouldn't be able to apply any not null constraints.

But the approach might still work. Map external_id with access="field" and a default of 0 or -1 (whatever suits your logic). In the getter you can check it external_id is 0/-1 and return the id in that case.

This would work as long as you only access your database through the application and you don't have any joins involing external_id.

_________________
--Wolfgang


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