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.
|