-->
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.  [ 3 posts ] 
Author Message
 Post subject: [HV] Applying a constraint on an enumeration type
PostPosted: Thu Jan 09, 2014 1:09 pm 
Newbie

Joined: Fri Dec 20, 2013 4:41 pm
Posts: 4
Hi,

I have a simple example where I need to define a constraint on a field which is an enumeration type. Basically, I want to make sure that this enumeration type field has a specific value.

How can I achieve that? I'm using the programmatic API but I didn't find anything specific to enumeration types.

Is it possible and if yes how ?

Regards,
BQ.

PS: Here is what I have:

public enum ProductStatus {
active,
inactive
}

public class Product {
ProductStatus status;
}

And the constraint I'm trying to define:
constraintMapping.type(Product.class).property("status", ElementType.FIELD).constraint( ??? );

Where ??? could be for instance:

new ValidLiteralDef(ProductStatus.class, ProductStatus.inactive);


Top
 Profile  
 
 Post subject: Re: [HV] Applying a constraint on an enumeration type
PostPosted: Fri Jan 10, 2014 8:20 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
Hey,

What you want to achieve is not possible due to limitations of the Java annotation system. In particular one can't have an annotation member which takes any enumeration value (Enum<?>), one can only define annotation members which accept one specific enumeration type.

As the programmatic API is resembling the possiblities of annotation-based constraint declaration, the same limitation applies there. So you'd have to define a custom constraint annotation type for this. The reference guide disscusses in [1] in detail this particular case of creating a constraint based on an enumeration.

You could then declare constraints of that type via the API (see [2] for more details):

Code:
constraintMapping
    .type(Product.class)
        .property("status", ElementType.FIELD)
            .constraint( new GenericConstraintDef<>( ProductStatusOf.class )
                .param( "value", ProductStatus.ACTIVE )
            );


Hth,

--Gunnar

[1] http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#validator-customconstraints-simple
[2] http://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#section-programmatic-api

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: [HV] Applying a constraint on an enumeration type
PostPosted: Sat Jan 11, 2014 6:41 pm 
Newbie

Joined: Fri Dec 20, 2013 4:41 pm
Posts: 4
Thanks


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