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

查看:21
本文介绍了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 ?

推荐答案

你应该使用 @NotNull 注释.

You should use the @NotNull annotation.

您的 boolean 是原始类型,而不是对象类型 (Boolean) 因此约束 @NotNull 不能应用,因为原始类型不能是 .注释执行以下验证(格式由我添加):

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天全站免登陆