-->
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.  [ 6 posts ] 
Author Message
 Post subject: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Tue Jul 19, 2011 12:29 pm 
Newbie

Joined: Tue Jul 19, 2011 12:25 pm
Posts: 4
Hello all - first post here.
I am using NetBeans 7 to generate entity classes which I use in a Seam 3 application under JBOSS AS 7.

The entity classes are generated with the following annotations...

Code:
public class Orders implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @NotNull
    @Column(name = "orderid")
    private Integer orderid;
.....


When persisting, bean validation fails because the autogenerated primary key is null at the time the object is persisted.

Netbeans have stated that they wont fix this as they see it as bean validation issue rather than their generated classes.
See here: http://netbeans.org/bugzilla/show_bug.cgi?id=197845

Anyone else come across this - any workarounds? (other than disabling bean validation)
BTW I have asked this same question on the seam forums.
Thanks in advance. Rich


Top
 Profile  
 
 Post subject: Re: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Wed Jul 20, 2011 5:12 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi Rich,

I don't think this is a Hibernate Validator (Bean Validation) issue. Here is what the spec says:

Quote:
For each event type, a list of groups is targeted for validation. By default, the default Bean Validation group (the group Default) will be validated upon the pre-persist and pre-update lifecycle validation events, and no group will be validated upon the pre-remove event.


On pre-persist your orderid is still null and hence the validation error. Bean Validation does not have any knowledge about JPA and that this is a generated value which will be set (and neither should it).

One solution is obviously to disable lifecycle based validation via the javax.persistence.validation.mode property. And alternative would be to work w/ validation groups and use the properties javax.persistence.validation.group.pre-persist/pre-update/pre-remove. Personally I think the @NotNull is not useful in this case.

--Hardy


Top
 Profile  
 
 Post subject: Re: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Wed Jul 20, 2011 6:49 am 
Newbie

Joined: Tue Jul 19, 2011 12:25 pm
Posts: 4
Hardy,
I have solved this for now by editing all of my Netbeans generated classes (I have 79 tables!) to remove the @NotNull annotation. This is not ideal as I have to remember to repeat this if I ever rebuild these classes from my database.

It would be nice is Netbeans generated classes which did not generate the @NotNull annotation along with @GeneratedValue(....) annotation, but there you are!

The guys at Netbeans are saying..
Quote:
I think it is a defect in Beans Validation integration into
Persistence. The prePersist should be smart enough to ignore NotNull when
GeneratedValue is present. Compare it with preUpdate and preRemove: NotNull is
perfectly valid there and make sense.


I don't want to disable bean validation as I am using it throughout my application (and it's great!).

I haven't looked at validation groups yet, thanks for the pointer.

Thanks for your help.
Rich


Top
 Profile  
 
 Post subject: Re: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Wed Jul 20, 2011 8:19 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi again,

as said, BV is not aware of JPA at all. If you think it is worth changing you could create an issue in the actual Bean Validation Specification issue tracker - https://hibernate.onjira.com/browse/BVAL
The work for BV 1.1 (the next version) has just started.

--Hardy


Top
 Profile  
 
 Post subject: Re: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Wed Jul 20, 2011 12:52 pm 
Newbie

Joined: Tue Jul 19, 2011 12:25 pm
Posts: 4
Issue created....

https://hibernate.onjira.com/browse/BVAL-234

Regards
Rich


Top
 Profile  
 
 Post subject: Re: NetBeans 7 generated entity classes conflict with Bean Valid
PostPosted: Wed Jul 20, 2011 2:55 pm 
Hibernate Team
Hibernate Team

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

as Hardy said, the simplest way is to put the @NotNull constraint into a separate group, which is only validated when updating the entity. You can find an example for this approach here.

Alternatively you could chose another way for id generation. When using a sequence (available in Oracle, Derby etc.), the id field is set already when the entity is persisted and validated:

Code:
@Id
@NotNull
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator="OrderSequence")
@SequenceGenerator(name="OrderSequence",sequenceName="ORDER_SEQ", allocationSize=50)
private Integer orderid;

Hth, Gunnar

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


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