Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 303
markjason72 Avatar asked Feb 23 '23 21:02

markjason72


1 Answers

ISBN has 13 digits (see wiki). I would use a class that checks the validity of given String. Something like:

class ISBN {
  private String isbn;
  public ISBN(String isbn) throws ISBNFormatException {
    // you might want to filter hyphens first, before the check
    if(ISBN.isValid(isbn)) this.isbn = isbn;
    else throw new ISBNFormatException(isbn);
  }
  public static boolean isValid(String s) {
   // validate number here, see wiki
  }
}

This, of course, may be a bit too much. If your app is really simple, you may go with String just fine.

Edit The hyphenation divides the number into groups (language, publisher, etc.). However, as for the uniqueness of the number, hyphenation (or division with spaces) plays no role.

like image 54
Miki Avatar answered Mar 04 '23 21:03

Miki