Java hibernate找不到布尔值的验证程序

Java hibernate找不到布尔值的验证程序,java,spring,hibernate,validation,annotations,Java,Spring,Hibernate,Validation,Annotations,我有一个服务方法,它尝试使用hibernate的store()方法添加一个对象。get方法对此DAO和服务类有效,而添加则无效。在控制台中没有错误 UrlWhiteListDaoImpl urlDao; MapperFacade mapper; @Autowired public UrlWhiteListingServiceImpl(UrlWhiteListDao urlWhiteListDao, MapperFacade mapper, UrlWhiteListDaoImpl urlDao

我有一个服务方法,它尝试使用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;
}

我的模范班是:

@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
}
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);
    }
}
DAO实现类为:

@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
}
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);
    }
}
公共类UrlWhiteListDaoImpl扩展EntityDaoImpl实现UrlWhiteListDao{
受保护的UrlWhiteListDaoImpl(){
super(UrlWhitelist.class);
}
@凌驾
公共列表getByDomainName(字符串名称){
DetachedCriteria=DetachedCriteria.forClass(UrlWhitelist.class);
标准。添加(限制。eq(“域名”);
返回getAllByCriteria(criteria);
}
}
控制台中没有错误,但服务器日志中显示:

严重:路径为[]的上下文中Servlet[services]的Servlet.service()引发异常[请求处理失败;嵌套异常为javax.validation.UnexpectedTypeException:HV000030:找不到约束“org.hibernate.validator.constraints.NotBlank”validating type“java.lang.Boolean”的验证器。请检查配置中是否存在“disabled”]及其根本原因 javax.validation.UnexpectedTypeException:HV000030:找不到约束“org.hibernate.validator.constraints.NotBlank”validating type“java.lang.Boolean”的验证器。请检查配置中的“disabled”

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

您应该使用注释

您的
boolean
是基本类型,而不是对象类型(
boolean
),因此无法应用约束,因为基本类型不能为
null
。注释执行以下验证(由我添加的格式):

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

使用对象类型:

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

要解决此错误,必须使用正确的批注。在上述问题中,
@NotBlank
批注必须仅应用于任何字符串字段


若要验证布尔类型字段,请使用注释
@NotNull
或使用
布尔
盒式类型

非常感谢。是的,它的工作方式与您所说的一样,但是,我需要一个基元类型,在不接触模型类的情况下如何更改dto类?(如不将其定义为NotNull等)如果不可能,那么我可以更改模型类。为什么需要基元类型?基元类型默认为
false
,并且可能是
true
。如果将
Boolean
限制为非
null
,则其行为相同。此外,可以使用
Boolean
boole>一样一个
。顺便说一句,我也有dto类。它在我的dto类中是布尔型的还是布尔型的?不,它真的不重要。看。顺便说一句,它不起作用。我认为当我将模型类布尔型更改为布尔型时它起作用,但不是。并且没有错误消息。