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