-->
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.  [ 4 posts ] 
Author Message
 Post subject: Vaildation not Firing with Oracle 11g and SequenceGenerator
PostPosted: Fri Jun 03, 2011 12:57 pm 
Newbie

Joined: Fri Apr 09, 2010 3:55 pm
Posts: 8
Hi,

Using Oracle 11.2.0.1.0.
Java 1.6 u25
Hibernate Validator 4.2.0.Beta2 (but problem also occurs on 4.1.0.Final)
Hibernate 3.6.4.Final.

Right, here we go:

Here is a snippet of code:

Code:
@SequenceGenerator(name = "articleseq", sequenceName = "articleseq", allocationSize = 1)
public class Article extends AbstractModel {

    private static final long serialVersionUID = 3154322570848739695L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO, generator = "articleseq")
    private Long id;

    @NotEmpty
    @Length(max = 256)
    private String headline;

    @Valid
    @NotNull
    @Cascade(org.hibernate.annotations.CascadeType.SAVE_UPDATE)
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH})
    private Channel channel;
}


Here is a test case:

Code:
    @Test
    public void articleWithNullChannelIdShouldBeInvalid() {
        thrown.expect(ConstraintViolationException.class);
        thrown.expectMessage("Channel cannot be empty!");
        article.setChannel(null);
        articleDao.save(article);
    }

    @Test
    public void articleWithEmptyHeadlineShouldBeInvalid() {
        thrown.expect(ConstraintViolationException.class);
        thrown.expectMessage("Headline cannot be empty!");
        article.setHeadline(null);
        articleDao.save(article);
    }


What is happening is that the org.hibernate.validator.contraints.impl.NotNullValidator is NOT being fired, thus the ExpectedException in each unit test is failing.

If I look at my trace, I see that Hibernate is requesting sequence values from Oracle and the Entity (Article) is being assigned an id, thus it is being persisted to the database.

If I change the @GeneratedValue(strategy = GenerationType.AUTO, generator = "articleseq") to @GeneratedValue(strategy = GenerationType.IDENTITY) then the NotNullValidator fires and the Unit Tests pass successfully.

We can't use IDENTITY here since we have other processes that could update the database, so we have to rely on Oracle offering us primary keys.

I really don't understand why the Validation is not firing - anyone have any information why?

Thank you so much :-)

-=david=-


Top
 Profile  
 
 Post subject: Re: Vaildation not Firing with Oracle 11g and SequenceGenerator
PostPosted: Fri Jun 03, 2011 2:21 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Quote:
If I look at my trace, I see that Hibernate is requesting sequence values from Oracle and the Entity (Article) is being assigned an id, thus it is being persisted to the database.

I wouldn't be so sure of that: as soon as an entity is attached to the session (like when you persist a new object), then a primary key must be assigned to it, but Hibernate will not actually flush the entity to the database until as late as possible: you might always want to change some more values before the transaction commits, or maybe you won't commit it at all.

So when IDENTITY is being used, to assign an identifier it's forced to write (and so to validate) to the entity table, but if a sequence is being used then it's good enough to get a sequence value from the database, it doesn't need yet to actually store the value (flush the persistence context).

I'm confident it will validate the object at transaction commit, which I don't see here.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject: Re: Vaildation not Firing with Oracle 11g and SequenceGenerator
PostPosted: Sat Jun 04, 2011 5:16 am 
Newbie

Joined: Fri Apr 09, 2010 3:55 pm
Posts: 8
Hi!

Thank you *so* much for your reply. It makes sense. I really appreciate it :-) I'll have to rethink my transactional boundaries so that before my unit tests finish, that I get the transaction to commit so that Hibernate Validator fires for me.

Grazie tanto :-) sei molto gentile :-)

-=david=-


Top
 Profile  
 
 Post subject: Re: Vaildation not Firing with Oracle 11g and SequenceGenerator
PostPosted: Sat Jun 04, 2011 10:16 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Prego!

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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:
cron
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.