1

I have a method where I check type of class from String and then load it from database to manipulate with data. Is it possible to put in the parameter of method generic class and take data from it? All classes extends another class BaseEntity.

Here My Code

   public String sendChangedStatus(String type,UUID entityId) throws ExecutionException, InterruptedException {

    String requestNumber="";
    String name="";
    String status="";
    String entityName="";
    PersonGroupExt initiator=null;
    switch (type){
        case "msg":
            Message msg =  dataManager
                    .load(Message.class)
                    .query("select e from hse_Message e" +
                            " where e.id = :entityId")
                    .parameter("entityId",entityId)
                    .view("message-editView")
                    .optional().orElse(null);

            requestNumber= msg.getRequestNumber();
            name="Сообщения";
            status=msg.getStatus().getLangValue();
            initiator=msg.getInitiator();
            entityName="Message";
            break;
        case "bhv":
            Behavior behavior= dataManager.loadValue("select e from hse_Behavior e" +
                    " where e.id = :entityId", Behavior.class)
                    .parameter("entityId",entityId)
                    .optional().orElse(null);
            requestNumber= behavior.getRegNumber();
            name="Обращения";
            status=behavior.getStatus().getLangValue();
            initiator = behavior.getInitiator();
            entityName="Behavior";
            break;
....
    }

Something like this I want to do:

 public String sendChangedStatus(Class<T> entity) throws ExecutionException, InterruptedException {

    String requestNumber=entity.getRequestNumber();
    String name=entity.getName();
    String status=entity.getStatus();
    String entityName=entity.getInstanceName();
    PersonGroupExt initiator=entity.getInitiator();
       
                   
....
    }
Paco Abato
  • 3,920
  • 4
  • 31
  • 54
Danik
  • 115
  • 1
  • 2
  • 12
  • PECS is somewhat relevant here. Read up on [this question](https://stackoverflow.com/questions/2723397/what-is-pecs-producer-extends-consumer-super) – Shark Aug 10 '21 at 08:58

1 Answers1

1

Without knowing the details of how data persistence is implemented in your code (it's not clear what dataManager is), the closest thing that comes to mind is polymorphic queries. For example, Hibernate allows querying based on the base type (in your case BaseEntity). If using Hibernate, have a look at how inheritance can be modeled and how it affects polymorphic querying: https://docs.jboss.org/hibernate/orm/5.5/userguide/html_single/Hibernate_User_Guide.html#entity-inheritance

As far as generics are concerned, if all you need is the Class object from which you load the entity, then you can simply do:

public String sendChangedStatus(Class<? extends BaseEntity> type, UUID entityId) throws ExecutionException, InterruptedException {

    BaseEntity entity = dataManager
                           .load(type)
                           .query("select e from BaseEntity e" +
                               " where e.id = :entityId")
                           .parameter("entityId",entityId)
                          .optional().orElse(null);
    String requestNumber = entity.getRequestNumber();
    String name = entity.getName();
    String status = entity.getStatus();
    String entityName = entity.getInstanceName();
    PersonGroupExt initiator = entity.getInitiator();
    
    ....
}

The getter methods getRequestNumber(), getName(), etc. would of course have to be defined in the BaseEntity class.

M A
  • 71,713
  • 13
  • 134
  • 174