-->
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.  [ 2 posts ] 
Author Message
 Post subject: Generate value for non-Id attribute
PostPosted: Wed Aug 21, 2013 10:18 am 
Newbie

Joined: Wed Aug 21, 2013 9:56 am
Posts: 1
First of all, sorry about my poor English. But I have a problem and I couldn't find a solution until now, then I need your help.

I have a class that has an attribute and it isn't the class @Id. However, its value has to be generated automatically.
I know that there are two ways of doing this:
- Annotate the attribute with @GeneratedValue annotation (it won't work because the attribute isn't the class @Id);
- Annotate the attribute with @Generated annotation. This solution works fine, because the field in the database has a default value. But my problem is that I want to generate the default value just when the atributte is NULL.

For example: if I set the value "1000" to the attribute before saving, the @Generated shouldn't be executed. But if I didn't set any value to the attribute e it is NULL, then the @Generated would have to be executed.

Are there any way of doing this with Hibernate?


Top
 Profile  
 
 Post subject: Re: Generate value for non-Id attribute
PostPosted: Mon Aug 26, 2013 10:23 pm 
Newbie

Joined: Sun Jun 20, 2010 9:57 pm
Posts: 7
Location: Madison WI
You could use an Interceptor or an EventListener to generate the value when the entity is saved to the database. It wouldn't be ideal because the interceptor/eventlistener is triggered for every entity that is saved and not just entities of a specific type, but you could implement the logic to only apply to instances of a specific class and only generate the value if the value is null.

Something like the following (uses Interceptors):

Code:
public class GeneratePropertyValueInterceptor extends EmptyInterceptor {

    ...

    public boolean onSave(Object entity, Serializable id, Object[] state, String[] propertyNames, Type[] types) {

        if ( entity instanceof ClassWithPropertyValueToGenerate) {
            for ( int i=0; i<propertyNames.length; i++ ) {
                if ( "propertyToGenerateValue".equals( propertyNames[i] ) ) {
                    if (state[i] == null) {
                        state[i] = functionThatGeneratesValue();
                        return true;
                    }
                }
            }
        }
        return false;
    }

    ...
}


Using an EventListener would be similar. I'm not sure if one is preferable to the other. You can get more information about Interceptors and EventListeners from the Hibernate manual (Chapter 14).

This will be more difficult if your function for generating the value requires arugments that are not available at this point.


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