I have tried many way to solve this problem but Can't, If I remove custome repository and use JpaRepository then it is woking fine without changing single digit code. I don't know why this is happening. When I'm trying to fetch department list then it is occuring error on postman
Please help me as soon as possible
Here is the my code please check it
College.java
@Entity
@Table(name = "colleges")
public class College {
@Id
@GeneratedValue(
strategy = GenerationType.IDENTITY
)
@Column(name = "college_id")
private Long id;
@Column(name = "college_name", nullable = false)
private String collegeName;
@Column(name = "address", nullable = false)
private String address;
@OneToMany(mappedBy = "college", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Department> departments = new HashSet<>();
@OneToMany(mappedBy = "faccollege", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Faculty> faculties = new HashSet<>();
//Getter setters
}
Department.java
@Entity
@Table(name = "departments")
public class Department {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "department_id")
private long id;
private String departmentName;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "college_id", nullable = false)
private College college;
@OneToMany(mappedBy = "department", cascade = CascadeType.ALL, orphanRemoval = true)
private Set<Faculty> faculties = new HashSet<>();
//Getter and Setters
}
DepartmentRepository
@Repository
@Transactional
public class DepartmentRepository{
@Autowired
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
public List<Department> findAll(){
Session session = this.sessionFactory.getCurrentSession();
List<Department> departmentList = session.createQuery("from Department").list();
return departmentList;
}
@Transactional
public Department save(Department department) {
Session session = this.sessionFactory.getCurrentSession();
session.save(department);
return department;
}
public Department findById(long id) {
Session session = this.sessionFactory.getCurrentSession();
Department department = (Department) session.get(Department.class, id);
return department;
}
}