Java Hibernate找不到布尔值的验证器 [英] Java hibernate No validator could be found for boolean

查看:76
本文介绍了Java Hibernate找不到布尔值的验证器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务方法,该方法尝试使用休眠的store()方法添加对象. get方法适用于此DAO和服务类,但添加不起作用.在控制台中没有错误.

I have a service method that tries to add an object with store() method of hibernate. get method is working for this DAO and service class whereas adding not working. In console there is no error.

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;

}

我的模型课是:

@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
}

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);
    }
}

在控制台中没有错误,但是在服务器日志中显示:

In console there is no error but in server log it says:

严重:路径为[]的上下文中的servlet [services]的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"的验证器.检查已禁用"的配置

SEVERE: Servlet.service() for servlet [services] in context with path [] threw exception [Request processing failed; nested exception is javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Boolean'. Check configuration for 'disabled'] with root cause javax.validation.UnexpectedTypeException: HV000030: No validator could be found for constraint 'org.hibernate.validator.constraints.NotBlank' validating type 'java.lang.Boolean'. Check configuration for 'disabled'

我认为to与模型类之间的映射有问题,但是,为什么为什么get方法起作用而只有store()不起作用?解决方案是什么?

I thought there is something wrong with mapping between to and model class, but, then why get method is working and only store() is not working? What is the solution ?

推荐答案

您应使用

You should use the @NotNull annotation.

您的boolean是原始类型,而不是对象类型(Boolean),因此是约束

Your boolean is a primitive type, not an object type (Boolean) hence the constraint @NotNull cannot be applied since the primitive type cannot be null. The annotation performs the following validation (formatting added by me):

带注释的元素不能为null.接受任何类型.

The annotated element must not be null. Accepts any type.

使用对象类型:

@NotNull
@Column(name = "DISABLE", nullable = false)
private Boolean disabled;

这篇关于Java Hibernate找不到布尔值的验证器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆