I'm creating a school diary app using spring boot and faced with a small problem. When I try to add some data to database, JPA shows me such error:
ERROR: relation "timetable_entity_tuesday" does not exist
I don't know why it's happening, because I gave all the settings to my entity, where it should look for the table.
So here is my code.
TimetableEntity
@Entity
@Table(name = "timetables", schema = "working_schema")
public class TimetableEntity {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Getter @Setter
private long id;
@Getter @Setter @ElementCollection
private List<String> monday;
@Getter @Setter @ElementCollection
private List<String> tuesday;
@Getter @Setter @ElementCollection
private List<String> wednesday;
@Getter @Setter @ElementCollection
private List<String> thursday;
@Getter @Setter @ElementCollection
private List<String> friday;
@Getter @Setter @ElementCollection
private List<String> saturday;
@Getter @Setter @ElementCollection
private List<String> sunday;
@OneToOne(mappedBy = "timetable")
@Getter @Setter
private ClassEntity schoolClass;
public TimetableEntity() {
}
}
TimetableRepository
public interface TimetableRepository extends JpaRepository<TimetableEntity, Long> {
}
Method, which adds data to database, stored in service:
public TimetableEntity addTimetable(TimetableAddModel timetableData) throws SubjectNotFoundException {
TimetableEntity timetable = new TimetableEntity();
timetable.setTuesday(Arrays.asList("math"));
timetableRepo.save(timetable);
return timetable;
}
And here' how hibernate makes queries:
Hibernate:
insert
into
timetable_entity_tuesday
(timetable_entity_id, tuesday)
values
(?, ?)
It tries to add data to timetable_entity
, although timetables
table was given to entity.
Here's my database:
If you know what can be a problem, please tell me, I'd really appreciate it!
CodePudding user response:
You can not save a list in a column that's why you are getting this error you need to map each day with the time table for reference you can watch this video : mapping in jpa
CodePudding user response:
JPA 2.0 @ElementCollection and @CollectionTable annotations allow to store a collection of Java types. Try to modify the mapping, adding the @CollectionTable annotation to your entity.
@Entity
@Table(schema = "working_schema")
public class Timetables {
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE)
@Getter @Setter
private long id;
@Getter @Setter @ElementCollection @CollectionTable(name = "Monday")
private List<String> monday;
@Getter @Setter @ElementCollection @CollectionTable(name = "Tuesday")
private List<String> tuesday;
...
}