Answer the question
In order to leave comments, you need to log in
Why do I get an error when saving an object through the OneToMany - ManyToOne relationship?
There are several Entities:
BaseModel Entities:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class BaseModel {
@Id
@Column(name = "id")
@Getter
@GeneratedValue(strategy = GenerationType.AUTO)
private long id;
protected BaseModel() {
}
}
@Entity
@Table(name = "class_room")
public final class ClassRoom extends BaseModel {
@OneToMany(fetch = FetchType.LAZY, mappedBy = "classRoom" )
@Getter
@Setter
private List<Teacher> teacher = new ArrayList<>();
@OneToMany(fetch = FetchType.LAZY, mappedBy = "classRoom")
@Getter
@Setter
private List<Pupil> pupil = new ArrayList<>();
@Column(nullable = false)
@Getter
@Setter
private String name;
public ClassRoom() {
}
}
@Entity
@Table(name = "pupil")
public final class Pupil extends BaseModel {
@ManyToOne
@JoinColumn(name = "pupil_id")
@Setter
private ClassRoom classRoom;
@Column(nullable = false)
@Getter
@Setter
private String name;
@Getter
@Setter
@Column(nullable = false)
private String surname;
public Pupil() {
}
}
@Entity
@Table(name = "teacher")
public final class Teacher extends BaseModel {
@ManyToOne
@JoinColumn(name = "teacher_id")
@Setter
private ClassRoom classRoom;
@Column(nullable = false)
@Getter
@Setter
private String name;
@Column(nullable = false)
@Getter
@Setter
private String surname;
@Column(nullable = false)
@Getter
@Setter
private String discipline;
public Teacher() {
}
}
org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.SchoolJournal.SpringHibernate.model.Pupil.name; nested exception is org.hibernate.PropertyValueException: not-null property references a null or transient value : com.SchoolJournal.SpringHibernate.model.Pupil.name
@SpringBootTest
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class SpringHibernateApplicationTests {
@Autowired
private PupilRepository pupilRepository;
@Autowired
private ClassRoomRepository classRoomRepository;
@Autowired
private TeacherRepository teacherRepository;
@BeforeAll
@Rollback(value = false)
public void contextLoads() {
Pupil pupil = new Pupil();
pupil.setName("Anton");
pupil.setSurname("Savridov");
Pupil pupil1 = new Pupil();
pupil.setName("Elena");
pupil.setSurname("Antonova");
Teacher teacher = new Teacher();
teacher.setName("Alla");
teacher.setSurname("Aronova");
teacher.setDiscipline("Mathematics");
ClassRoom classRoom = new ClassRoom();
classRoom.setName("1A");
pupil.setClassRoom(classRoom);
pupil1.setClassRoom(classRoom);
teacher.setClassRoom(classRoom);
classRoomRepository.save(classRoom);
teacherRepository.save(teacher);
pupilRepository.save(pupil);
pupilRepository.save(pupil1);
}
@Test
public void createEntity() {
Iterable<Pupil> allPupil = pupilRepository.findAll();
for (Pupil pupil1 : allPupil) {
System.out.println("Ученики");
System.out.println("----------------------------");
System.out.println(pupil1.getId() + " " +
pupil1.getName() + " " +
pupil1.getSurname());
System.out.println("----------------------------");
}
Iterable<Teacher> allTeacher = teacherRepository.findAll();
for (Teacher teacher1 : allTeacher) {
System.out.println("Учителя");
System.out.println("----------------------------");
System.out.println(teacher1.getId() + " " +
teacher1.getName() + " " +
teacher1.getSurname() + " " +
teacher1.getDiscipline());
System.out.println("----------------------------");
}
Iterable<ClassRoom> allClassRoom = classRoomRepository.findAll();
for (ClassRoom classRoom1 : allClassRoom) {
System.out.println("Класс");
System.out.println("----------------------------");
System.out.println(classRoom1.getId() + " "
+ classRoom1.getName());
System.out.println(classRoom1.getPupil().getId() + " " +
classRoom1.getPupil().getName() + " " +
classRoom1.getPupil().getSurname());
System.out.println(classRoom1.getTeacher().getId() + " " +
classRoom1.getTeacher().getName() + " " +
classRoom1.getTeacher().getSurname() + " " +
classRoom1.getTeacher().getDiscipline());
System.out.println("----------------------------");
}
}
Answer the question
In order to leave comments, you need to log in
Good afternoon!
I will assume that you have a problem with the fact that SQL has reserved words and when you create tables and columns, these columns are not created for you.
not-null property references a null or transient value : com.SchoolJournal.SpringHibernate.model.Pupil.name
@Column(name = '"`name`", nullable = false)
@Getter
@Setter
private String name;
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question