gfg007
gfg007

Reputation: 191

How to Enable/Disable Entity Relations in Runtime?

I have a Spring Boot app that has basic CRUD services. For the read services I want to see their relations as well. There is no problem for implementing relations by @ManyToOne, @OneToOne, etc. annotations like this example.

My problem is I want to enable this relations based on a parameter in list service or I could use another endpoint as well. How can I achieve this? Any suggestions are welcome.

My entities are like;

    @Entity
    @Table(name = "employee")
    public class Employee{
        private long id;
        private String name;
        private Address address;
        
        // getter setters
    }
    
    @Entity
    @Table(name = "address")
    public class Address {
        private long id;
        private String name;
        private String postalCode;
    
        // getter setters
    }

EDIT e.g.

{
    "id": 1,
    "name": "Jane"
}
{
    "id": 1,
    "name": "Jane"
    "address": { 
                 "id":1,
                 "name": "HOME",
                 "postalCode": "11111"
                }
}

Upvotes: 3

Views: 487

Answers (3)

Alexander.Furer
Alexander.Furer

Reputation: 1869

You can annotate the relations with fetchType=Lazy . Then invoke the getters to manually load the needed relations. Another option is to eagerly load all relationships, but annotate the response with @JsonView and exclude the relations you don't need.

Upvotes: 0

Yevgen
Yevgen

Reputation: 1657

One of the ways would be to have different data transfer objects to return, depending on the REST request.

Let's assume you have the following classes, apart from entities.

class EmployeeDto {
  private Long id;
  private String name;
}

class EmployeeAddressDto {
  private Long id;
  private String name;
  private AddressDto address;
}

class AddressDto {
  private Long id;
  private String name;
  private int postalCode;
}

Then in a controller you would do something like this.

@GetMapping("/employee/list")
public ResponseEntity<?> getEmployees(@RequestParam int detailed) {

  if (detailed) {
    return employeeService.getDetailedEmployeeList();
  } else {
    return employeeService.getEmployeeList();
  }
}

Service inteface would look like this.

interface EmployeService() {

  List<EmployeeDto> getEmployeeList();

  List<EmployeeAddressDto> getDetailedEmployeeList();
}

You would also need to handle entities to transfer objects conversions.

Upvotes: 0

SYED MUSTAFA HUSSAIN
SYED MUSTAFA HUSSAIN

Reputation: 481

its some sudo code for your understanding . you can use Query Parameter and In Condition you call repo what you want : for my scenario i want different response short, medium and long

@RequestMapping(value = "/getContacts", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, headers = "Accept=application/json") public String getContact(@RequestBody ContactItemRequestInfo contactItemRequestInfo, @RequestParam(required = false) String key, String Contact) {

    if(key.equals("medium"))
    {
         return Contact="{\"responseCode\":\"02\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
         
    }
    else if(key.equals("long"))
    {
        return Contact="{\"responseCode\":\"03\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
        
    }
    else
    {
        return Contact="{\"responseCode\":\"00\",\"responseDescription\":\"Success\",\"totalCount\":2,\"contacts\":[{\"id\":114,\"firstName\":\"ali\",\"lastName\":\"kamran\"},{\"id\":115,\"firstName\":\"usman\",\"lastName\":\"khan\",\"middleName\":\"saad\"}]}";
        
    }

} It will be helpful for you !!

Upvotes: 1

Related Questions