Retrive a object of parent class into child class in spring boot application

1.2k Views Asked by At

I am building REST API using spring boot application. I have connected application to Mongodb database. I have created a database named "Employee" and collection as "Employee" itself. Now i want to create a document. I have three class. Class A, Class B and class C. Class A is the parent Class having property (id,name,password). Class B is child class and extends Class A with property(address,phoneNumber) and class C is child class which also extends class A with property (fatherName,MotherName).

Now i want to add the data to database as object of B or object of C and also want to retrive the data from database as object of B or Object of C.

here is code of Class A:

package com.example.webproject;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection="Employee")
public class A {
  
  @Id
  private String id;
  private String passwd;
  private String username;

  public String getId() {
      return id;
  }
  public void setIp(String string) {
      this.ip = string;
  }
  public String getPasswd() {
      return passwd;
  }
  public void setPasswd(String passwd) {
      this.passwd = passwd;
  }
  public String getUsername() {
      return username;
  }
  public void setUsername(String username) {
      this.username = username;
  }

class B:

package com.example.webproject;
 public class B extends A {
   private String address;
   private String phoneNumber;
   public String getAddress() {
       return address;
   }
   public void setAddress(String address) {
       this.address = address;
   }
   public String getPhoneNumber() {
       return phoneNumber;
   }
   public void setPhoneNumber(String phoneNumber) {
       this.phoneNumber= phoneNumber;
   }
}

Class C :

package com.example.webproject;

public class C extends A {
    private String fatherName;
    private String motherName;
    
    public String getFatherName() {
        return fatherName;
    }
    public void setFatherName(String fatherName) {
        this.fatherName = fatherName;
    }

    public String getMotherName() {
        return motherName;
    }
    public void setMotherName(String motherName) {
        this.motherName = motherName;
    }
}

EmployeeRepository.java

package com.example.webproject;

import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface EmployeeRepository extends MongoRepository<A,String> {}

EmployeeController.java

@RestController
public class EmployeeController {
    @Autowired
    private EmployeeRepository repo;

    @PostMapping("/addByB")
    public String addDataByB(@RequestBody B res) {
        repo.save(res);
        return "added";
    }

    @PostMapping("/addByC")
    public String addDataByC(@RequestBody C res) {
        repo.save(res);
        return "added";
    }
    

    @GetMapping("/getByB")
    public List<B> getDataByB(){
        List<B> b= repo.findAll();   #Here it throws error because repo.findAll return object of A.
        return b;
    }

When i try to add data as B object or C object using swagger , the data is getting stored in database. Now i want to retrieve the data as B object or C object, how to achieve this?

1

There are 1 best solutions below

0
On

Because you just create Repository of class A and call it, you nedd to creat two another repo of class B and C then call them like you call " EmployeeRepository " so you can use them and get the data.