-->
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: converter vs. validator
PostPosted: Sat Nov 01, 2008 6:59 am 
Newbie

Joined: Sat Nov 01, 2008 6:04 am
Posts: 3
I have an EntityBean with several fields with values between 0 and 99. To keep the database small, I used byte-Values and the Range Annotation.

My Problem:
If a user enters a value, that is not in the range of byte, the converter message is displayed ("Enter a value < 255") and not the validator message ("Enter a value < 99"). My idea was to circumvent this problem by using a String instead of a byte and limit it to 2 digits (@Length(max=2)).

My question is:
Does that increase the memory usage significantly? Is there a way to solve this problem, where I can still use byte for the attributes?

Thanks in advance to everyone.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 11:37 am 
Hibernate Team
Hibernate Team

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

the @Range annotation is used for numbers and string representing numbers. I don't think it behaves correctly when used with bytes.

Using strings and @Length seems to be wrong if you actually have integer values.

Personally I would use integers. If you want to stick with bytes I recommend writing your own custom validator.

BTW, what do you mean with converter message?

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 04, 2008 12:30 pm 
Newbie

Joined: Sat Nov 01, 2008 6:04 am
Posts: 3
With the "converter message" I mean the message displayed under javax.faces.converter.ByteConverter.BYTE_detail in the messages_*.properties.

I tried using a String instead of a Byte and I got the message I was looking for (the one from javax.faces.validator.NOT_IN_RANGE). The reason why I am not using Integer is, because it needs 4times the amount of memory compared to Byte - which is as close as I can get to values between 0 and 99.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 4:17 am 
Hibernate Team
Hibernate Team

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

I still would argue that from a modelling point of view it seems odd to use strings for integers. Is it really essential to save a few bytes in your case? If so I would probably still use integers in the model, but define a custom user type which maps the integer to a byte. Have a look at the online documention for custom/user types. In this case your model still is correct and you properly hide the fact how the integers get persisted.

--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 05, 2008 12:32 pm 
Newbie

Joined: Sat Nov 01, 2008 6:04 am
Posts: 3
I tried using an integer, but the problem is the same. The errormessage I got was the one from javax.faces.converter.IntegerConverter.INTEGER_detail instead of javax.faces.converter.ByteConverter.BYTE_detail - just a larger range of numbers.
I also tried your UserType advice, but I couldn't make it work (probably a combination of bad web-researching- and programming-skills).

However, I found a way to use byte for a datatype, but also to get the error message behavior I was looking for:
Code:
@Range(max=99, min=0)
private Byte brothers;

public String getBrothers() {
   if(brothers==null){
      return "";
   }else {
      return "" + brothers;   
   }
}

public void setBrothers(String brothers) {
   this.brothers = Byte.valueOf(brothers);
}


As you can see, the property is defined as Byte, but I use String in my Get- and Set-methods. It sure is, again, a questionable moddeling style. But at least the property should have byte's memory usage - right?!


Top
 Profile  
 
 Post subject: converter message?
PostPosted: Fri Nov 14, 2008 3:38 am 
Newbie

Joined: Fri Nov 30, 2007 6:29 am
Posts: 14
Location: Oslo
aleda wrote:
I tried using an integer, but the problem is the same. The errormessage I got was the one from javax.faces.converter.IntegerConverter.INTEGER_detail instead of javax.faces.converter.ByteConverter.BYTE_detail - just a larger range of numbers.
I also tried your UserType advice, but I couldn't make it work (probably a combination of bad web-researching- and programming-skills).

However, I found a way to use byte for a datatype, but also to get the error message behavior I was looking for:
Code:
@Range(max=99, min=0)
private Byte brothers;

public String getBrothers() {
   if(brothers==null){
      return "";
   }else {
      return "" + brothers;   
   }
}

public void setBrothers(String brothers) {
   this.brothers = Byte.valueOf(brothers);
}


As you can see, the property is defined as Byte, but I use String in my Get- and Set-methods. It sure is, again, a questionable moddeling style. But at least the property should have byte's memory usage - right?!


Maybe you can try and set the jsf converter message id for that specific jsf component to the same message id you use in your validator (you're using message bundles aren't you?). Also, as mentioned, if @Range does not support bytes then write your own @ByteRange or something (it's easy).


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.