2

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 ?

Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
Chris Garsonn
  • 717
  • 2
  • 18
  • 32

3 Answers3

8

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;
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183
  • thank you very much. yes it is working like as you said, but, i need a primitive type, what can i do about changing dto class without touching model class ?(like not defining it as notnull etc.) if it is not possible then i can change model class. – Chris Garsonn Dec 13 '18 at 20:01
  • Why do you need a primitive type? The primitive type is by default `false` and might be `true` is set. If you restrict the `Boolean` to be not `null`, it behaves the same. Moreover, you can work with `Boolean` the same as it is a `boolean`. – Nikolas Charalambidis Dec 13 '18 at 20:03
  • by the way i have dto class also. does it matter boolean or Boolean in my dto class ? – Chris Garsonn Dec 13 '18 at 20:17
  • Nope, it really doesn't matter. See [Boolean vs. boolean](https://stackoverflow.com/questions/3728616/boolean-vs-boolean-in-java). – Nikolas Charalambidis Dec 13 '18 at 20:19
  • by the way it did not work. i thought it works when i change model class boolean to Boolean but not. and there is no error message. – Chris Garsonn Dec 13 '18 at 20:28
  • What does mean "it didn't work". What error message is shown? – Nikolas Charalambidis Dec 13 '18 at 20:28
  • same error: 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'] – Chris Garsonn Dec 13 '18 at 20:34
  • remember changing NotBlank to NotNull and boolean to Boolean, because in the message I am still seeing 'org.hibernate.validator.constraints.NotBlank' – Andres Rincon Dec 13 '18 at 20:45
1

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

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

Perkone
  • 57
  • 3
  • 8
Alexis Pavlidis
  • 1,080
  • 1
  • 11
  • 21
0
If(Boolean.TRUE.equals(disabled)){

//If you check this type of any Boolean value then check null value and also false both field}