Hibernate插入空值?

时间:2018-04-23 13:32:33

标签: hibernate spring-boot spring-data spring-data-jpa hibernate-mapping

我使用带有Hibernate和Postgres数据库的Spring Data ReST Repository将记录插入到数据库中,并且由于某种原因,即使我传入了来自ReST客户端。这会引发约束违规异常,因为该字段在数据库中设置为非空。

这是实体:

package com.stsi.pss.domain.Model.LookupEntities;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.stsi.pss.domain.Base.AuditedEntity;
import com.stsi.pss.domain.Model.PersonnelEntity;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;

@Entity
@Table(name = "Personnel_Types", schema = "dbo", catalog = "SEVIS-CAD")
@EntityListeners(AuditingEntityListener.class)
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="@id")

public class PersonnelTypesEntity extends AuditedEntity<String> {
    private long id;
    private String description;
    private Boolean isActive;

    @Id
    @Column(name = "PersonnelTypeId", nullable = false)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    @Basic
    @Column(name = "Description", nullable = true, length = 8000)
    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    @Basic
    @Column(name = "IsActive", nullable = false)
    public Boolean getIsActive() {
        return isActive;
    }

    public void setIsActive(Boolean isActive) {
        isActive = isActive;
    }


    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PersonnelTypesEntity)) return false;
        PersonnelTypesEntity that = (PersonnelTypesEntity) o;
        return getId() == that.getId() &&
                Objects.equals(getDescription(), that.getDescription()) &&
                Objects.equals(getIsActive(), that.getIsActive());
    }

    @Override
    public int hashCode() {

        return Objects.hash(getId(), getDescription(), getIsActive());
    }

    @OneToMany(mappedBy = "personnelType")
    public Set<PersonnelEntity> getPersonnel() {
        return personnel;
    }
    public void setPersonnel(Set<PersonnelEntity> personnel) {
        this.personnel = personnel;
    }
    private Set<PersonnelEntity> personnel = new HashSet<PersonnelEntity>();
}

这是我从ReST客户端拨打的电话:

POST

http://localhost:4444/pssv2/api/personneltypes/

{

    "description": "terminated",
    "isactive": true
}

这是我得到的错误:

2018-04-23 09:22:53.693 DEBUG 16804 --- [nio-4444-exec-3] org.hibernate.SQL                        : insert into dbo.Personnel_Types (creationTime, creatorUserId, lastModificationTime, lastModifierUserId, Description, IsActive) values (?, ?, ?, ?, ?, ?)
Hibernate: insert into dbo.Personnel_Types (creationTime, creatorUserId, lastModificationTime, lastModifierUserId, Description, IsActive) values (?, ?, ?, ?, ?, ?)
2018-04-23 09:22:53.695 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [TIMESTAMP] - [Mon Apr 23 09:22:53 EDT 2018]
2018-04-23 09:22:53.696 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [sunil]
2018-04-23 09:22:53.696 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [TIMESTAMP] - [Mon Apr 23 09:22:53 EDT 2018]
2018-04-23 09:22:53.696 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [VARCHAR] - [sunil]
2018-04-23 09:22:53.698 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [5] as [VARCHAR] - [terminated]
2018-04-23 09:22:53.698 TRACE 16804 --- [nio-4444-exec-3] o.h.type.descriptor.sql.BasicBinder      : binding parameter [6] as [BOOLEAN] - [null]
2018-04-23 09:22:53.711  WARN 16804 --- [nio-4444-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 0, SQLState: 23502
2018-04-23 09:22:53.711 ERROR 16804 --- [nio-4444-exec-3] o.h.engine.jdbc.spi.SqlExceptionHelper   : ERROR: null value in column "isactive" violates not-null constraint
  Detail: Failing row contains (5, 2018-04-23 09:22:53.691, sunil, 2018-04-23 09:22:53.691, sunil, terminated, null).
2018-04-23 09:22:53.730 ERROR 16804 --- [nio-4444-exec-3] o.s.d.r.w.RepositoryRestExceptionHandler : could not execute statement; SQL [n/a]; constraint [isactive]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [isactive]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

这是我的存储库:

package com.stsi.pss.domain.IRepository;

import com.stsi.pss.domain.Model.LookupEntities.PersonnelTypesEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.stereotype.Repository;

@Repository
@RepositoryRestResource(collectionResourceRel = "personneltypes", path = "personneltypes")

public interface IPersonnelTypesRepository extends JpaRepository<PersonnelTypesEntity, Long> {
}

有关可能发生的事情的任何线索?

0 个答案:

没有答案