Search code examples
javajakarta-eeisbn

correct way to model isbn number in j2ee app


I am trying out a web app using servlets and jsps and need to model isbn number of an item in my class as well as in hibernate mappings. Which should be the type of an isbn number?Long or String? I had come across many tutorials that use either of them.. isbn is supposedly a 10 digit identifier..sometimes you come across numbers like 0-85131-041-9 which cannot be a Long ..Some examples use numbers without hyphens..

So,which should be the type? Any suggestions?

thanks

mark


Solution

  • I'd store it in a Long property and use a formatter/parser (essentially, a converter). When you're going to display it, the converter should just convert the Long property to a human representation with hyphens in the right places. When you're going to validate/persist it, the converter should remove all hyphens from the submitted value and put it in a Long property.

    Basically, it's the same idea as you would use for a Date field which you format/parse in the human representation with help of SimpleDateFormat. The only difference is that the ISBN formatter/parser isn't available by the standard Java SE API. You'd need to write one yourself or to adopt a 3rd party one (of which none existing comes to mind though). Finally this converter can then be used as a JSP tag (like JSTL's <fmt:formatDate>) or a standalone Java class which is invoked by an EL function or when you're using JSF, a @FacesConverter class.

    Due to the complexiteit of the ISBN number, this is indeed more than often stored as a String so that the developer doesn't need to worry about valid patterns. Whether that's good or bad is a question that you've to ask yourself and your team.