-->
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: Best practices for assign id ?
PostPosted: Thu Jun 23, 2005 4:26 am 
Newbie

Joined: Fri Jun 17, 2005 3:41 am
Posts: 4
I'm using <composite-id>.

In hibernate doc:
Your persistent class must override equals() and hashCode() to implement composite identifier equality.

So I implement my own equals() and hashCode() with composit-id.
like this:

Code:
    public int hashCode() {
        return getOID().hashCode(); //getOID() maybe return null;
    }


I want to assign id when hibernate calling onSaveOrUpdate(SaveOrUpdateEvent event) method.

Code:
    @Override
    public Serializable onSaveOrUpdate(SaveOrUpdateEvent event)
            throws HibernateException {
        if (event.getObject() instanceof AbstractEDO) {
            AbstractEDO edo = (AbstractEDO)event.getObject();
            if (edo.getOID() == null) {
                OID oid = new OID();
                oid.setUid(UIDGenerator.nextID());
                oid.setQName(edo.getRecordType().getQualifiedName());
                oid.setAccountId(UserContext.getAccountID());
                edo.setOID(oid);
            }
        }
        return super.onSaveOrUpdate(event);
    }


The problem is :

I add a POJO instance to a HashSet,it will throw NPE.
Because I do not assign id until call onSaveOrUpdate() method.

Resolution:

1. Assign id when calling getOID().But I want to run some test cases in no database entironment.
If I assign id in getOID() method,will cause other problem. Because generate OID need to connect database to get next id.
2. Do not override equals() and hashCode() ,but hibernate doc require to override.
3. Changed hashCode() method like this:
Code:
    public int hashCode() {
        return getOID() == null ? 0 : getOID().hashCode();
    }

but the hash code is not constant.

Is there a better way ?Any help greatly appreciated.
Regards

Wesley


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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.