1

I am not sure if this is problem with how Hibernate or Spring Data understands my input. In my project on backend I am using Java + Spring Data + Hibernate + PostgreSQL.
My problem is that when I try to persist to database a Java object which contains array with elements, I get error from my PostgreSQL database saying:
org.postgresql.util.PSQLException: ERROR: null value in column "free_timeslots" violates not-null constraint
I really don't understand what is happening underneath here, as from what I see in the debugger, I am providing a correct object to the repository (JpaRepository that looks like this:

import com.ap.personalassistant.model.DayTimeSlots;
import org.springframework.data.jpa.repository.JpaRepository;

public interface DayTimeSlotsRepository extends JpaRepository<DayTimeSlots, Long> {
}

)
Object that I provide:
enter image description here
DayTimeSlots class that looks like this:

import com.ap.personalassistant.dto.DayTimeSlotsDto;
import lombok.AccessLevel;
import lombok.Data;
import lombok.Setter;

import javax.persistence.*;
import java.time.LocalDate;
import java.util.List;

@Data
@Entity
@Table(name = "day_timeslots")
public class DayTimeSlots {

    @Id
    @Setter(AccessLevel.NONE)
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long id;
    private LocalDate date;
    @Column(name = "free_timeslots")
    @ElementCollection
    private List<Long> freeTimeSlotsIds; // possible from 62 to 109

    public DayTimeSlots() {
    }
    public DayTimeSlots(DayTimeSlotsDto dayTimeSlotsDto) {
        this.id = dayTimeSlotsDto.getId();
        this.date = dayTimeSlotsDto.getDate();
        this.freeTimeSlotsIds = dayTimeSlotsDto.getFreeTimeSlotsIds();
    }
}

And in the database I have a table named day_timeslots with these columns:

  • id (bigint)
  • date (date)
  • free_timeslots (bigint[])


Is someone able to help me with this? I've looked in the internet for answers but nothing's worked so far.

augenss
  • 71
  • 10
  • tried without `@Setter(AccessLevel.NONE)` ? – Paizo Feb 14 '20 at 23:19
  • This is refering to id field only and I don't have problem with id field. I have problem with freeTimeslots field – augenss Feb 14 '20 at 23:24
  • Your mapping for the `free_timeslots` is not correct. Look at [this](https://stackoverflow.com/questions/1647583/mapping-a-postgresql-array-with-hibernate) – SternK Feb 15 '20 at 18:19

0 Answers0