1

I am recently working on a project where there is @OneToMany connection between teacher and their subjects. There is no problem with register, however, whenever I want to fetch teachers who teach selected subject, I get this error:

"object references an unsaved transient instance - save the transient instance before flushing"

Here are my entities:


import javax.persistence.*;
import java.util.List;

@Entity
@Table
public class Teacher {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    @Column
    private Long teacherId;

    @Column(nullable = false)
    private String teacherName;

    @Column(nullable = false)
    private String teacherSurname;

    @Column(nullable = false, unique = true)
    private String number;

    @Column(nullable = false)
    private String  password;

    @Column(nullable = false)
    private boolean isActive;

    @Column(nullable = false)
    private String roles;

    @Column
    @OneToMany(targetEntity = Subject.class, cascade = CascadeType.ALL)
    private List<Subject> subjectList;

    public Teacher() { }

    public Teacher(String teacherName, String teacherSurname, String number, String password) {
        this.teacherName = teacherName;
        this.teacherSurname = teacherSurname;
        this.number = "+994" + number;
        this.password = password;
    }

    public Long getTeacherId() {
        return teacherId;
    }

    public void setTeacherId(Long teacherId) {
        this.teacherId = teacherId;
    }

    public String getTeacherName() {
        return teacherName;
    }

    public void setTeacherName(String teacherName) {
        this.teacherName = teacherName;
    }

    public String getTeacherSurname() {
        return teacherSurname;
    }

    public void setTeacherSurname(String teacherSurname) {
        this.teacherSurname = teacherSurname;
    }

    public String getNumber() {
        return number;
    }

    public void setNumber(String number) {
        this.number = number;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public boolean isActive() {
        return isActive;
    }

    public void setActive(boolean active) {
        isActive = active;
    }

    public String getRoles() {
        return roles;
    }

    public void setRoles(String roles) {
        this.roles = roles;
    }

    public List<Subject> getSubjectList() {
        return this.subjectList;
    }

    public void setSubjectList(List<Subject> subjectList) {
        this.subjectList = subjectList;
    }
}

import javax.persistence.*;

@Entity
@Table
public class Subject {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE)
    private Long subjectId;

    @Column(nullable = false)
    private String subjectName;

    public Subject() {}

    public Subject(String subjectName) {
        this.subjectName = subjectName;
    }

    public Long getSubjectId() {
        return subjectId;
    }

    public void setSubjectId(Long subjectId) {
        this.subjectId = subjectId;
    }

    public String getSubjectName() {
        return subjectName;
    }

    public void setSubjectName(String subjectName) {
        this.subjectName = subjectName;
    }
}

And this is my repository class where the findTeachersBySubjectListContains method causes the error:


import app.entitity.Subject;
import app.entitity.Teacher;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface TeacherRepo extends CrudRepository<Teacher, Long> {
    Optional<Teacher> findByNumber(String number);

    @Override
    <S extends Teacher> S save(S s);

    Iterable<Teacher> findTeachersBySubjectListContains(Subject subject);
}

I tried @Modifying(clearAutomatically = true), but it didn't work either.

Yogesh Prajapati
  • 4,770
  • 2
  • 36
  • 77
  • 1
    Does this answer your question? [How to fix the Hibernate "object references an unsaved transient instance - save the transient instance before flushing" error](https://stackoverflow.com/questions/2302802/how-to-fix-the-hibernate-object-references-an-unsaved-transient-instance-save) – rdj7 Mar 12 '20 at 12:53
  • Hello. Sadly, no. – countmerloin Mar 12 '20 at 16:30

0 Answers0