Chr*_*onn 2 java validation spring annotations hibernate
我有一个服务方法,它尝试使用store()
休眠方法添加一个对象。get 方法适用于此 DAO 和服务类,而添加不起作用。在控制台中没有错误。
UrlWhiteListDaoImpl urlDao;
MapperFacade mapper;
@Autowired
public UrlWhiteListingServiceImpl(UrlWhiteListDao urlWhiteListDao, MapperFacade mapper, UrlWhiteListDaoImpl urlDao) {
this.urlDao = urlDao;
this.urlWhiteListDao = urlWhiteListDao;
this.mapper = mapper;
}
@Override
public UrlWhiteListDto addUrlWhiteListItem(UrlWhiteListDto urlWhiteListDto) throws Exception {
String domainUrlToBeAdded = parseUrl(urlWhiteListDto.getDomain());
if (isDomainExistbyName(domainUrlToBeAdded)) {
throw new Exception("Already existed domain is tried to be added");
}
UrlWhitelist urlModel = mapper.map(urlWhiteListDto,UrlWhitelist.class);
urlDao.store(urlModel);
return urlWhiteListDto;
Run Code Online (Sandbox Code Playgroud)
}
我的模型类是:
@Entity
@Table(name = UrlWhitelist.TABLE_NAME)
public class UrlWhitelist implements EntityBean {
public static final String TABLE_NAME = "URL_WHITE_LIST";
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID", nullable = false)
private Long id;
@NotBlank
@Column(name = "DOMAIN", nullable = false)
private String domain;
@NotBlank
@Column(name = "DISABLE", nullable = false)
private boolean disabled;
// getters & setters omitted
}
Run Code Online (Sandbox Code Playgroud)
而DAO实现类是:
public class UrlWhiteListDaoImpl extends EntityDaoImpl<UrlWhitelist, Long> implements UrlWhiteListDao {
protected UrlWhiteListDaoImpl() {
super(UrlWhitelist.class);
}
@Override
public List<UrlWhitelist> getByDomainName(String name) {
DetachedCriteria criteria = DetachedCriteria.forClass(UrlWhitelist.class);
criteria.add(Restrictions.eq("domain", name));
return getAllByCriteria(criteria);
}
}
Run Code Online (Sandbox Code Playgroud)
在控制台中没有错误,但在服务器日志中它说:
严重:servlet [服务] 的 Servlet.service() 在路径 [] 的上下文中引发异常 [请求处理失败;嵌套异常是 javax.validation.UnexpectedTypeException: HV000030: 找不到约束“org.hibernate.validator.constraints.NotBlank”验证类型“java.lang.Boolean”的验证器。检查“禁用”的配置,根本原因是 javax.validation.UnexpectedTypeException: HV000030: 找不到约束“org.hibernate.validator.constraints.NotBlank”验证类型“java.lang.Boolean”的验证器。检查“禁用”的配置
我认为 to 和模型类之间的映射有问题,但是,为什么 get 方法有效而只是store()
无效?解决办法是什么 ?
您应该使用@NotNull
注释。
您boolean
是原始类型,而不是对象类型 ( Boolean
) 因此@NotNull
无法应用约束,因为原始类型不能是null
。注释执行以下验证(格式由我添加):
带注释的元素不能是
null
。接受任何类型。
使用对象类型:
@NotNull
@Column(name = "DISABLE", nullable = false)
private Boolean disabled;
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6182 次 |
最近记录: |