-->
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: Hibernate Validator and Oracle Sequence
PostPosted: Mon Dec 22, 2008 7:06 pm 
Newbie

Joined: Mon Dec 22, 2008 6:58 pm
Posts: 2
Hello all:
I'm trying to setup and use Hibernate Validator. I am using an Oracle Sequence to generate the value for the primary key on one my EventCategories table. As far as I can tell, I think I have it all setup to use Validator correctly. The problem I am having is that an Oracle Sequence number is being generated even when invalid data is being entered.

For example, I am trying to enter a 60-character value into a 50-character field. Now the validator stuff all seems to work correctly as it is not writing my data and its coming back with a nice error message. What I don't want to happen though is for a Sequence number to be generated when the basic validation failed but yet it is still using a Sequence number.

I know this is being picky but I don't see any sense in using a Sequence number when the data didn't pass the basic validation rules. Is there any way around this so that it doesn't get a number from the Oracle Sequence unless the validation did pass? I hope I'm explaining this clearly enough. I am attaching some code snippets of what I have in case you need to refer to them. Please let me know if I haven't provided enough information. And if you cannot help me, that's more than OK as I will understand.

Here is the "pre" code from hibernate.cfg.xml:
[code]
<event type="pre-update">
<listener class="org.hibernate.validator.event.ValidateEventListener"/>
</event>

<event type="pre-insert">
<listener class="org.hibernate.validator.event.ValidateEventListener"/>
</event>

</session-factory>

[/code]
Here is the Validator annotations in my POJO bean for the description column:
[code]
public class EventCategories {

private Integer id;
private String description;

@NotNull
@Length(max=50)
public String getDescription() {
return description;
}
[/code]

Here is mapping code from EventCategories.hbm.xml:

[code]
<hibernate-mapping package="gov.faa.amc.nas.newsAndEvents">

<class name="EventCategories" table="AOS.EVENT_CATEGORIES">
<id column="id" name="id" length="5">
<generator class="sequence">
<param name="sequence">AOS.SQ_EVENT_CATEGORIES</param>
</generator>
</id>
<property column="description" name="description" length="50"
not-null="true" />

</class>

</hibernate-mapping>
[/code]

Here is my JUnit Test case code and yes I realize this is far from a finished product (smiling):
[code]
@Test
public void testInsertEventCategory() {
List<String> allErrors = new ArrayList<String>();
EventCategories eventCategory = new EventCategories();
eventCategory.setDescription("Now we need to test an entry that is more than 50 characters");
Transaction tx = session.beginTransaction();
try {
logger.info("Monte test before the session.save");
Integer newId = (Integer)session.save(eventCategory);
assertNotNull(newId);
tx.commit();
} catch (InvalidStateException ex) {
for (InvalidValue error : ex.getInvalidValues()) {
allErrors.add(error.getPropertyName() + " " + error.getMessage());
logger.info("Invalid Values: " + error.toString());
}
}
}
[/code]

Best Regards,
Hobi
hobione.wordpress.com


Top
 Profile  
 
 Post subject:
PostPosted: Mon Dec 22, 2008 9:05 pm 
Newbie

Joined: Mon Jun 16, 2008 1:15 pm
Posts: 15
Quote:
The problem I am having is that an Oracle Sequence number is being generated even when invalid data is being entered.


Put aside other issues, I would like to know what made you think or say that a sequence number is being generated. Many people don't realize that Oracle does not grantee that sequence numbers will be continuous - it is "directional" and unique but not necessarily continuous - there can be gaps easily.

Also sequence numbers are cached for connections. This shows easily, especially in testing environment where pooled connections can be easily retired (easier than production environment as there are simply not enough transactions going through).

Above when I said directional, I put quotes around it. The reason being that it is not truly one direction at all levels. Say you have two pooled connections - one got 10 cached values of a sequence: for example 10 to 19, and second connection came in and got next 10: 20 - 29. Imagine this sequence of events: connection 1 uses its first value - 10, connection 2 independently uses its first value - 20, than connection 1 uses its second value - 11. When an observer looks at this at a grand level, he sees 10, 20, 11, it's obviously not even directional.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 23, 2008 12:31 pm 
Newbie

Joined: Mon Dec 22, 2008 6:58 pm
Posts: 2
Peiguo:

Thank you for your nice explanation about how Oracle manages sequences. Now, let me get back to my original question. Since sequence numbers are cached for connections, is it still possible to tell Hibernate not to increment the sequence number if validation fails?

Code:
<property column="description" name="description" length="50"
not-null="true" />


Please advise.
Thank you.
Hobi


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.