Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8822093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:01:01+00:00 2026-06-14T06:01:01+00:00

I have two classes called Lecturer and Course. My Lecturer class has a List

  • 0

I have two classes called Lecturer and Course. My Lecturer class has a List of Course objects a variable as shown

@OneToMany(mappedBy = "lecturer")
@NotFound(action = NotFoundAction.IGNORE)
private List<Course> courseList = new ArrayList<Course>();

In my course class I have a Lecturer object as a variable as below.

@ManyToOne
@JoinTable(name = "course_lecturer", joinColumns = @JoinColumn(name = "course_id"), inverseJoinColumns = @JoinColumn(name = "lecturer_id"))
private Lecturer lecturer;

Now I have a spring Controller method that returns a json object of Lecturer as below.

@RequestMapping(value = "/getlecturerbyid", method = RequestMethod.POST)
public @ResponseBody
Object getLecturer(@ModelAttribute(value = "id") Lecturer lecturer) {
    Map<String, Object> response = new HashMap<String, Object>();
    response.put("message", "succeess");
    response.put("lecturer", lecturer);
    return response;
}

The problem is it throws a lazy initialization exception. Therefore I set fetch type in both variables in Lecturer and Course class to eager. Now the problem is it infinitely tries to fetch objects eagerly(if I unset fetch of either, it raises the same exception).

On the other hand if I remove the variable of either class, then the hibernate does not know that there is a relationship for another. eg: if I remove course list from Lecturer and delete one of the Lecturer objects then hibernate does not remove the course objects that are related with the deleted Lecturer object. so i have to write a hibernate query and remove it manually in the code.

Can you please suggest me on how to over come this issue?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:01:02+00:00Added an answer on June 14, 2026 at 6:01 am

    This is happening because of circular reference(parent -> child ->parent). I am sure you have got the idea.

    The simplest solution for your problem is to iterate the list of courses and set the Lecturer as null before putting it into response to break the circular relationship as below:

       Object getLecturer(@ModelAttribute(value = "id") Lecturer lecturer) {
           Map<String, Object> response = new HashMap<String, Object>();
           response.put("message", "succeess");
    
           //break the circular relationship
           if(lecturer.getCourses() != null){
              for(Course course: lecturer.getCourses()){
                   course.setLecturer(null);
              }
           }
           response.put("lecturer", lecturer);
           return response;
       }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If I have a class called A and I have two classes that derive
I have a bas class called Media with two classes that inherit from it,
I have simple ontology called campus.owl.There is a class calledLecturer and which has two
I have two classes, derived from a common class. The common class has a
So I have two classes. One is a class called Main. The Main class
I have two test classes called TT_Common and TT_Container which extends CPPUNIT_NS::TestFixture: class TT_Common
I have two classes. One is called Employee and the other EmployeeDetails that has
I have two classes, Class A called Apply and Class B called Option I
For simplicity let's say I have two classes: a class called Car and a
Say I have two classes, in two different headers, called: class TestA { public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.