27.08.2017

Hibernate: Boolean vs boolean in entity

...
    private boolean friend;
...

    @Basic
    @Column(name = "friend", nullable = true)
    public boolean isFriend() {
        return friend;
    }

    public void setFriend(boolean friend) {
        this.friend = friend;
    }
If field "freind" in database can be null, We get error:
org.springframework.orm.jpa.JpaSystemException: Null value was assigned to a property of primitive type setter of ru.javastudy.entities.ContactEntity.friend; nested exception is org.hibernate.PropertyAccessException: Null value was assigned to a property of primitive type setter of ru.javastudy.entities.ContactEntity.friend
So, use Boolean if field can be NULL

Following the JavaBean naming convention, the property name is "friend", not "isFriend". "isFriend" is the name of the accessor method for the property.

Комментариев нет:

Отправить комментарий