我正在试用一个使用 servlet 和 jsps 的 Web 应用程序,并且需要在我的类以及 hibernate 映射中对项目的 isbn 编号进行建模。 isbn 编号的类型应该是 Long 型还是 String 型?我遇到过很多使用它们中任何一个的教程.. isbn 应该是一个 10 位数字标识符。有时您会遇到像 0-85131-041-9 这样的数字,它不能是长整数。一些示例使用没有连字符的数字。
那么,应该是哪种类型呢?有什么建议吗?
谢谢
标记
最佳答案
ISBN 有 13 位数字(参见 wiki)。我会使用一个类来检查给定 String
的有效性。像这样的东西:
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
}
}
当然,这可能有点过分了。如果您的应用非常简单,您可以使用 String
就好了。
编辑 连字符将数字分成几组(语言、出版商等)。然而,至于数字的唯一性,连字符(或用空格分隔)不起作用。
关于java - 在 j2ee 应用程序中为 isbn 编号建模的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6334579/