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=-