-->
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: different artificial key types for the same object
PostPosted: Tue Jan 30, 2007 7:01 am 
Beginner
Beginner

Joined: Wed Feb 23, 2005 11:32 am
Posts: 25
Location: Austria/Vienna
since our artifical keys are allways generated by the database by using
<generator class="native"/>
we would like to have persistant objects which are not dependent on the type of key in the database, e.g. integer, big-integer or string.

public class Teehaus {
private Object key = null;

public String getKey() {..}
public void setKey(String key) {..}
}

until now we had solved this by using field access and mapping like this:
<id name="key" type="integer" column="account_id" access="field">
<generator class="native"/>
</id>

the application uses the key as a string. but this solution requires us to convert to Integer several times like this:
th = s.load(Teehaus.class, new Integer(key-string));

And if we change the underlining table to big-integer or to a uuid string
this would change the code which should not happen.

We therefore use this mapping:

<id name="key" type="string" column="account_id" access="field">
<generator class="native"/>
</id>

It works with int, biging and uuid in the database (mssql-server for tests).

so the code does not change when the artifical key type is changed.

also the conversion during load seems to work fine like this:


Teehaus result = s.load(Teehaus.class, testAccount.getKey());

Summary:
The Pojos are accessed by field and uses object inside.
By using the Type="string" mapping the int or big integer gets converted
to a string in the pojo, which now is compatible for the load methods.

this would allow us to have different databases with different arifical key types, but only one code and mapping.

Is this a working solution for hibernate or are there any porblems which we have not seen yet.


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.