Reputation: 508
I have a very small doubt regarding @OneToMany mapping.
I have a model student and another model attendance.
A student can have multiple attendance. but student model should only be able to retrieve the attendance info.
But when I am trying to change some student info I am getting below error as it is trying to update attendance record.
here is my mapping
@Entity
@Table(name="student_detail")
@Getter @Setter
public class StudentDetailsModel {
@Id
@Column(name="reg_no",updatable = false, nullable = false)
private String regNo;
@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL })
@JoinColumn(name = "reg_no")
private List<AttendanceModel> attendances;
}
and the exception I a getting.
update
student_detail
set
address=?,
alt_contact_number=?,
blood_group=?,
contact_number=?,
dob=?,
father_name=?,
first_name=?,
gender=?,
last_name=?,
middle_name=?,
mother_name=?,
photo_url=?,
school_id=?
where
reg_no=?
Hibernate:
update
attendance
set
reg_no=null
where
reg_no=?
2019-01-13 12:12:52.922 WARN 10708 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : SQL Error: 0, SQLState: 23502
2019-01-13 12:12:52.923 ERROR 10708 --- [nio-8081-exec-1] o.h.engine.jdbc.spi.SqlExceptionHelper : ERROR: null value in column "reg_no" violates not-null constraint
Detail: Failing row contains (null, 1, 2019-01-05, t, 2).
2019-01-13 12:12:52.926 INFO 10708 --- [nio-8081-exec-1] o.h.e.j.b.internal.AbstractBatchImpl : HHH000010: On release of batch it still contained JDBC statements
org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; constraint [reg_no]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement
my attenance model is as follows
@Entity
@Table(name="attendance")
@Getter @Setter
public class AttendanceModel {
//@EmbeddedId
//private AttendanceId attendanceId;
@Id
@Column(name="attendance_id")
private long id;
@Column(name="reg_no")
private String regNo;
@Column(name="subject_id")
private long subjectId;
@Column(name="class_date")
private Date classDate;
@Column(name="present")
private boolean present;
}
Upvotes: 0
Views: 461
Reputation: 21
Could you show me Student Model.If i look your code post : You using Unidirectional relationship.
I think it must :
@OneToMany(fetch = FetchType.LAZY , cascade = CascedeType.ALL)
@JoinColumn(name="attendance_id")
private List<AttendanceModel> attendances = new ArrayList<>();
Upvotes: 1