Java hibernate No validator could be found for boolean

两盒软妹~` 提交于 2020-01-25 06:09:14

问题


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;

}

My model class is:

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

And DAO implementation class is:

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:

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'

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 ?


回答1:


You should use the @NotNull annotation.

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):

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

Use the object type:

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



回答2:


to solve this error you MUST use correct annotations. In above problem, @NotBlank annotation must be applied on any String field only.

To validate boolean type field, use annotation @NotNull or use Boolean boxed type



来源:https://stackoverflow.com/questions/53769165/java-hibernate-no-validator-could-be-found-for-boolean

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!