Mapping AbstractEntity to SubClassDTO - Single Table Strategy












1















I'm trying to mapp Entity (TrainingEntity) to DTO, where one of the fields is a Set with ManyToMany reference to an AbstractEntity (CoachEntity) divided by Single Table into two subclasses: ExternalCoach and InternalCoach.



Both subclasses have different data, therefore require two different mappers.



@Entity
@Table(name = "TRAINING")
public class TrainingEntity extends AbstractEntity {

@ManyToMany()
@JoinTable(name = "TRAINING_EMPLOYEE", joinColumns = { @JoinColumn(name = "TRAINING_ID") }, inverseJoinColumns = {
@JoinColumn(name = "COACH_ID") })
private Set<CoachEntity> coachEntities;

@Column(nullable = false)
private TrainingType trainingType;

......some data....

}


Abstract Coach Entity



    @Entity
@Table(name = "COACH")
@DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
public abstract class CoachEntity extends AbstractEntity {

......some data....

}


Coach Subclasses:



@Entity
@DiscriminatorValue("EXTERNAL")
public class ExternalCoachEntity extends CoachEntity {
......some data....
}



@Entity
@DiscriminatorValue("INTERNAL")
public class InternalCoachEntity extends CoachEntity {
......some data....
}


One mapper for Abstract Coach class won't have acces to subclasses methods and fields, so I need two different for External and Internal. Than I would have to use them in TrainingMapper class, but (example of internal):



public class CustomTrainingMapper {

public static TrainingDto toTrainingDto(TrainingEntity trainingEntity){
if(trainingEntity == null){
return null;
}

if(trainingEntity.getTrainingType().equals(TrainingType.INTERNAL)){
Set<CoachEntity> coachEntities = trainingEntity.getCoachEntities();


Set<CoachDto> coachDtos = CustomInternalCoachMapper.toDTOSet((Set<InternalCoachEntity>)coachEntities);
}


I get:



  cannot cast from Set<CoachEntity> to Set<InternalCoachEntity>


Without cast it simply doesn't see mappers methods with subclass input.



The method toDTOSet(Set<InternalCoachEntity>) in the type CustomInternalCoachMapper is not applicable for the arguments (Set<CoachEntity>)


When in mapper I change method input to abstract Coach Class it doesn't see subclasses methods and fields.



Part of InternalMapper:



public class CustomInternalCoachMapper {

public static CoachDto toCoachDto(InternalCoachEntity coachEntity) {
if (coachEntity == null) {
return null;
}

EmployeeDto employeeDto = CustomEmployeeMapper.toEmployeeDto(coachEntity.getEmployeeEntity());

return new InternalCoachDto(coachEntity.getId(), coachEntity.getVersion(), coachEntity.getCreateDate(),
coachEntity.getUpdateDate(), coachEntity.getName(), coachEntity.getSurname(), employeeDto);
}


enter image description here



Is it possible to mapp this AbstractEntity Set into subclasses DTOs?
I also tried with AbstractDto for Coaches, but then I'm facing the same problem with no access to subclasses getters and setters.










share|improve this question





























    1















    I'm trying to mapp Entity (TrainingEntity) to DTO, where one of the fields is a Set with ManyToMany reference to an AbstractEntity (CoachEntity) divided by Single Table into two subclasses: ExternalCoach and InternalCoach.



    Both subclasses have different data, therefore require two different mappers.



    @Entity
    @Table(name = "TRAINING")
    public class TrainingEntity extends AbstractEntity {

    @ManyToMany()
    @JoinTable(name = "TRAINING_EMPLOYEE", joinColumns = { @JoinColumn(name = "TRAINING_ID") }, inverseJoinColumns = {
    @JoinColumn(name = "COACH_ID") })
    private Set<CoachEntity> coachEntities;

    @Column(nullable = false)
    private TrainingType trainingType;

    ......some data....

    }


    Abstract Coach Entity



        @Entity
    @Table(name = "COACH")
    @DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
    @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
    public abstract class CoachEntity extends AbstractEntity {

    ......some data....

    }


    Coach Subclasses:



    @Entity
    @DiscriminatorValue("EXTERNAL")
    public class ExternalCoachEntity extends CoachEntity {
    ......some data....
    }



    @Entity
    @DiscriminatorValue("INTERNAL")
    public class InternalCoachEntity extends CoachEntity {
    ......some data....
    }


    One mapper for Abstract Coach class won't have acces to subclasses methods and fields, so I need two different for External and Internal. Than I would have to use them in TrainingMapper class, but (example of internal):



    public class CustomTrainingMapper {

    public static TrainingDto toTrainingDto(TrainingEntity trainingEntity){
    if(trainingEntity == null){
    return null;
    }

    if(trainingEntity.getTrainingType().equals(TrainingType.INTERNAL)){
    Set<CoachEntity> coachEntities = trainingEntity.getCoachEntities();


    Set<CoachDto> coachDtos = CustomInternalCoachMapper.toDTOSet((Set<InternalCoachEntity>)coachEntities);
    }


    I get:



      cannot cast from Set<CoachEntity> to Set<InternalCoachEntity>


    Without cast it simply doesn't see mappers methods with subclass input.



    The method toDTOSet(Set<InternalCoachEntity>) in the type CustomInternalCoachMapper is not applicable for the arguments (Set<CoachEntity>)


    When in mapper I change method input to abstract Coach Class it doesn't see subclasses methods and fields.



    Part of InternalMapper:



    public class CustomInternalCoachMapper {

    public static CoachDto toCoachDto(InternalCoachEntity coachEntity) {
    if (coachEntity == null) {
    return null;
    }

    EmployeeDto employeeDto = CustomEmployeeMapper.toEmployeeDto(coachEntity.getEmployeeEntity());

    return new InternalCoachDto(coachEntity.getId(), coachEntity.getVersion(), coachEntity.getCreateDate(),
    coachEntity.getUpdateDate(), coachEntity.getName(), coachEntity.getSurname(), employeeDto);
    }


    enter image description here



    Is it possible to mapp this AbstractEntity Set into subclasses DTOs?
    I also tried with AbstractDto for Coaches, but then I'm facing the same problem with no access to subclasses getters and setters.










    share|improve this question



























      1












      1








      1








      I'm trying to mapp Entity (TrainingEntity) to DTO, where one of the fields is a Set with ManyToMany reference to an AbstractEntity (CoachEntity) divided by Single Table into two subclasses: ExternalCoach and InternalCoach.



      Both subclasses have different data, therefore require two different mappers.



      @Entity
      @Table(name = "TRAINING")
      public class TrainingEntity extends AbstractEntity {

      @ManyToMany()
      @JoinTable(name = "TRAINING_EMPLOYEE", joinColumns = { @JoinColumn(name = "TRAINING_ID") }, inverseJoinColumns = {
      @JoinColumn(name = "COACH_ID") })
      private Set<CoachEntity> coachEntities;

      @Column(nullable = false)
      private TrainingType trainingType;

      ......some data....

      }


      Abstract Coach Entity



          @Entity
      @Table(name = "COACH")
      @DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      public abstract class CoachEntity extends AbstractEntity {

      ......some data....

      }


      Coach Subclasses:



      @Entity
      @DiscriminatorValue("EXTERNAL")
      public class ExternalCoachEntity extends CoachEntity {
      ......some data....
      }



      @Entity
      @DiscriminatorValue("INTERNAL")
      public class InternalCoachEntity extends CoachEntity {
      ......some data....
      }


      One mapper for Abstract Coach class won't have acces to subclasses methods and fields, so I need two different for External and Internal. Than I would have to use them in TrainingMapper class, but (example of internal):



      public class CustomTrainingMapper {

      public static TrainingDto toTrainingDto(TrainingEntity trainingEntity){
      if(trainingEntity == null){
      return null;
      }

      if(trainingEntity.getTrainingType().equals(TrainingType.INTERNAL)){
      Set<CoachEntity> coachEntities = trainingEntity.getCoachEntities();


      Set<CoachDto> coachDtos = CustomInternalCoachMapper.toDTOSet((Set<InternalCoachEntity>)coachEntities);
      }


      I get:



        cannot cast from Set<CoachEntity> to Set<InternalCoachEntity>


      Without cast it simply doesn't see mappers methods with subclass input.



      The method toDTOSet(Set<InternalCoachEntity>) in the type CustomInternalCoachMapper is not applicable for the arguments (Set<CoachEntity>)


      When in mapper I change method input to abstract Coach Class it doesn't see subclasses methods and fields.



      Part of InternalMapper:



      public class CustomInternalCoachMapper {

      public static CoachDto toCoachDto(InternalCoachEntity coachEntity) {
      if (coachEntity == null) {
      return null;
      }

      EmployeeDto employeeDto = CustomEmployeeMapper.toEmployeeDto(coachEntity.getEmployeeEntity());

      return new InternalCoachDto(coachEntity.getId(), coachEntity.getVersion(), coachEntity.getCreateDate(),
      coachEntity.getUpdateDate(), coachEntity.getName(), coachEntity.getSurname(), employeeDto);
      }


      enter image description here



      Is it possible to mapp this AbstractEntity Set into subclasses DTOs?
      I also tried with AbstractDto for Coaches, but then I'm facing the same problem with no access to subclasses getters and setters.










      share|improve this question
















      I'm trying to mapp Entity (TrainingEntity) to DTO, where one of the fields is a Set with ManyToMany reference to an AbstractEntity (CoachEntity) divided by Single Table into two subclasses: ExternalCoach and InternalCoach.



      Both subclasses have different data, therefore require two different mappers.



      @Entity
      @Table(name = "TRAINING")
      public class TrainingEntity extends AbstractEntity {

      @ManyToMany()
      @JoinTable(name = "TRAINING_EMPLOYEE", joinColumns = { @JoinColumn(name = "TRAINING_ID") }, inverseJoinColumns = {
      @JoinColumn(name = "COACH_ID") })
      private Set<CoachEntity> coachEntities;

      @Column(nullable = false)
      private TrainingType trainingType;

      ......some data....

      }


      Abstract Coach Entity



          @Entity
      @Table(name = "COACH")
      @DiscriminatorColumn(name = "TYPE", discriminatorType = DiscriminatorType.STRING)
      @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
      public abstract class CoachEntity extends AbstractEntity {

      ......some data....

      }


      Coach Subclasses:



      @Entity
      @DiscriminatorValue("EXTERNAL")
      public class ExternalCoachEntity extends CoachEntity {
      ......some data....
      }



      @Entity
      @DiscriminatorValue("INTERNAL")
      public class InternalCoachEntity extends CoachEntity {
      ......some data....
      }


      One mapper for Abstract Coach class won't have acces to subclasses methods and fields, so I need two different for External and Internal. Than I would have to use them in TrainingMapper class, but (example of internal):



      public class CustomTrainingMapper {

      public static TrainingDto toTrainingDto(TrainingEntity trainingEntity){
      if(trainingEntity == null){
      return null;
      }

      if(trainingEntity.getTrainingType().equals(TrainingType.INTERNAL)){
      Set<CoachEntity> coachEntities = trainingEntity.getCoachEntities();


      Set<CoachDto> coachDtos = CustomInternalCoachMapper.toDTOSet((Set<InternalCoachEntity>)coachEntities);
      }


      I get:



        cannot cast from Set<CoachEntity> to Set<InternalCoachEntity>


      Without cast it simply doesn't see mappers methods with subclass input.



      The method toDTOSet(Set<InternalCoachEntity>) in the type CustomInternalCoachMapper is not applicable for the arguments (Set<CoachEntity>)


      When in mapper I change method input to abstract Coach Class it doesn't see subclasses methods and fields.



      Part of InternalMapper:



      public class CustomInternalCoachMapper {

      public static CoachDto toCoachDto(InternalCoachEntity coachEntity) {
      if (coachEntity == null) {
      return null;
      }

      EmployeeDto employeeDto = CustomEmployeeMapper.toEmployeeDto(coachEntity.getEmployeeEntity());

      return new InternalCoachDto(coachEntity.getId(), coachEntity.getVersion(), coachEntity.getCreateDate(),
      coachEntity.getUpdateDate(), coachEntity.getName(), coachEntity.getSurname(), employeeDto);
      }


      enter image description here



      Is it possible to mapp this AbstractEntity Set into subclasses DTOs?
      I also tried with AbstractDto for Coaches, but then I'm facing the same problem with no access to subclasses getters and setters.







      java spring jpa mapping abstract






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '18 at 21:29







      td1

















      asked Nov 16 '18 at 21:09









      td1td1

      113




      113
























          0






          active

          oldest

          votes











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345393%2fmapping-abstractentity-to-subclassdto-single-table-strategy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53345393%2fmapping-abstractentity-to-subclassdto-single-table-strategy%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini