在写实体类时,经常会对域增加校验,例如@NotNull表示哪个字段不能为空,昨天晚上调试代码,就遇到了问题,
@Entitypublic class ApplicationCategory implements Serializable { private static final long serialVersionUID = -8018302345969463947L; @Id @GeneratedValue private Integer id; @NotNull(message = "应用分类名称不能为空") private String name; private String remark; @Override public int hashCode() { int result = id.hashCode(); result = 31 * result + name.hashCode(); result = 31 * result + (remark != null ? remark.hashCode() : 0); return result; } ..... ..... //省略其他方法}
程序启动,保存applicationCategory时,抛异常,错误如下:
2017-05-23 18:51:43.195 ERROR 16876 --- [http-nio-65009-exec-4] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.ValidationException: HV000041: Call to TraversableResolver.isReachable() threw an exception.] with root cause java.lang.NullPointerException: null at com.xxx.hhhh.cccc.machine.domain.ApplicationCategory.hashCode(ApplicationCategory.java:69