Spring-Data-Jpa save: Related entities saved as null which are not

Nəriman Nəhmədov :

I have an entity timetable:

@Entity @Table(name = TableUtils.TIMETABLE)public class Timetable {

private static final long serialVersionUID = -1307879048598194633L;

@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = ColumnUtils.ID)
private long id;

@OneToMany(mappedBy = "timetable", cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
private List<TimetableCell> timetableCells;

public List<TimetableCell> getTimetableCells() {
    return timetableCells;
}

public void setTimetableCells(List<TimetableCell> timetableCells) {
    this.timetableCells = timetableCells;
}
}

and an entity timetableCell:

@Entity
@Table(name = TableUtils.TIMETABLE_CELL)
public class TimetableCell extends AbstractElement {

private static final long serialVersionUID = -8083688091896353882L;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = ColumnUtils.WEEK_DAY_ID, nullable = false)
private WeekDay weekDay;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = ColumnUtils.LESSON_HOUR_ID, nullable = false)
private LessonHour lessonHour;

@ManyToOne(cascade = CascadeType.PERSIST)
@JoinColumn(name = ColumnUtils.TIMETABLE_ID, nullable = false)
private Timetable timetable;

...
}

At first I create timetable cells with weekDay and LessonHours. After I set this list to timetable and try to save it. But All timetable cells are saved with null weekDay and null LessonHour. I debugged it. Before save all fields are set as well.

timetableRepository.save(timetable);

Debug result showing following

Nəriman Nəhmədov :

I removed Cascade.Persist from fields in TimetableCell. It works for me but I don't know that what was problem.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=100286&siteId=1