Search code examples
jsonmongodbspring-bootreferencenull

Null referenced object created using Spring boot and Mongodb


I am a newbie using mongodb with springboot trying to create a simple collection with referenced object. Below are the JSON request and response(with null referenced objects). Please let me know how a Student object can be persisted in database when a StudentClass was created. Also how a StudentAddress object in Student be persisted in database.

Json request:

{
    "classNo": "1",
    "className": "First",
    "student": [
        {
            "studentId": "1",
            "studentName": "Tom",
            "age": "20",
            "rollno": "12345",
            "address": {
                "hno": "A494",
                "town": "DTown",
                "pinCode": "123456",
                "city": "New york"
            }
        }
    ]
} 

Response:

{
    "id": {
        "timestamp": 1716307508,
        "date": "2024-05-21T16:05:08.000+00:00"
    },
    "classNo": 1,
    "className": "First",
    "student": [
        {
            "id": null,
            "studentId": 1,
            "studentName": "Tom",
            "age": 20,
            "rollno": 12345,
            "studentAddress": null
        }
    ]
}

Collection created as in following image in Mongodb

I have three entities StudentClass, Student and StudentAddress.

StudentClass.java:

@Data
@AllArgsConstructor
@Document
public class StudentClass {

       @Id
       private ObjectId id;
        private Long classNo;

        private String className;

       @DocumentReference
       private List<Student> student;
    }

Student.java:

@Data
@AllArgsConstructor
@Document
public class Student {

       @Id
       private ObjectId id;
       
       @Indexed(unique = true)
       private Long studentId;
       
       private String studentName;
       private int age;
       private int rollno;

       @DocumentReference
       private StudentAddress studentAddress;
       
    }

StudentAddress.java:

@Data
@AllArgsConstructor
@Document
public class StudentAddress {
    @Id
    private ObjectId id;
    private String hno;
    private String town;
    private Long pinCode;
    private String city;
    
}

Repositories: StudentClassRepository.java:

public interface StudentClassRepository extends MongoRepository<StudentClass, ObjectId> {
       
    Optional<StudentClass> findById(ObjectId id);   
}

StudentRepository.java:

public interface StudentRepository extends MongoRepository<Student, ObjectId> {
    
    Optional<Student> findById(ObjectId id);
}

StudentAddressRepository.java:

public interface StudentAddressRepository extends MongoRepository<StudentAddress,ObjectId>{

    Optional<StudentAddress> findById(ObjectId id);
}

Controller: StudentClassController.java:


@RestController
@RequiredArgsConstructor
@RequestMapping("/studentclass")
public class StudentClassController {

       @Autowired
       private final StudentClassService studentClassService;
       
       
       @PostMapping("/create-student-and-class-in-db")
       private ResponseEntity<StudentClass> saveClassWithStudent(@RequestBody StudentClass studentClass){
           StudentClass stdClass = null;
            HttpHeaders headers = new HttpHeaders();

            stdClass = studentClassService.saveClassWithStudent(studentClass);
           return new ResponseEntity<StudentClass>(stdClass, headers, HttpStatus.CREATED);
       }



       @GetMapping("/get-all-classes")
       private ResponseEntity<List<StudentClass>> getAllClasses(){
          List<StudentClass> list = studentClassService.getAllClasses();
          return ResponseEntity.ok(list);
       }

Tried with above code but getting null references. I should be getting the response similar to the request.


Solution

  • Saved all referenced objects seaparately which resolved the issue. Now all referenced objects were created with Id's.