S
S
Saintka2021-06-03 04:55:23
Java
Saintka, 2021-06-03 04:55:23

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() {
        }
    }

classroom entity:

@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() {
        }
    }


Pupil Entities:

@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() {
    
        }
    }


teacher entity:

@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() {
    
        }
    }

An error is thrown in the console:

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


A test that was written to test the created objects:

@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("----------------------------");
    	}
    }


If I use the OneToOne - ManyToOne relationship, then everything works fine, but I can't add a new Pupil for example.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Orkhan, 2021-06-03
Hasanly @azerphoenix

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

In this case, this is the name of the variable Pupil.name
(line 433) - List of reserved words in SQL
And if you look in the database, you will not find the name column in the pupil table. Hence the error.
Solution: specify name in Column, escape with back single quote
@Column(name = '"`name`", nullable = false)
        @Getter
        @Setter
        private String name;

A useful resource is https://vladmihalcea.com/escape-sql-reserved-keywo...
As for OneToMany & ManyToOne, I recommend reading this article.
https://vladmihalcea.com/the-best-way-to-map-a-one...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question