Partage
  • Partager sur Facebook
  • Partager sur Twitter

Mapstruct - foreign key null

    1 août 2022 à 20:27:53

    Bonjour,

    Je suis en train d'essayer de créer une appli spring boot, et je bute sur un problème :

    J'ai une entité "projets" qui peut contenir plusieurs entités "images", dans une relation OneToMany.

    J'ai essayé de reproduire la façon dont l'appli sur laquelle je bosse en agence est faite :

    controller > facade > service, en passant par des mappers

    objetVo > objetBo > entité

    lorsque je récupère de la BDD un projet, il contient bien ses objets image, qui eux-même contiennent la référence id projet.

    Mais en passant par le mapper entité vers BO, je ne récupère pas cet id, et je ne comprends pas ce qu'il se passe.

    Voici comment sont construits mes entités :

    @Entity
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Table(name = "projets")
    public class ProjetEntity {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id", nullable = false)
        private Long id;
    
        @Column(name = "title", nullable = false)
        private String title;
    
        @Lob
        @Column(name= "rich_text")
        private String richText;
    
        @OneToMany(mappedBy = "projetEntity")
        private List<ImageEntity> images;
    
        @Column(name = "type", nullable = false)
        private String type;
    }
    @Entity
    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @Builder
    @Table(name = "image")
    public class ImageEntity {
    
        @Id
        @GeneratedValue(strategy = GenerationType.AUTO)
        @Column(name = "id_image", nullable = false)
        private Long idImage;
    
        @Column(name = "name")
        private String name;
    
        @Column(name = "location", nullable = false)
        private String location;
    
        @ManyToOne(fetch = FetchType.LAZY)
        @JoinColumn(name="projets_id", referencedColumnName = "id")
        private ProjetEntity projetEntity;
    
        @Column(name = "favorite")
        private String favorite;


    mes objetsBo :

    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    public class ProjetBo {
    
        private Long id;
    
        private String title;
    
        private String richText;
    
        private List<ImageBo> images;
    
        private String type;
    }
    @Data
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    @Getter
    @Setter
    public class ImageBo {
    
        private Long idImage;
    
        private Long projetsId;
    
        private String name;
    
        private String location;
    
        private boolean favorite;
    }

    Et enfin mon mapper :

    @Mapper(componentModel="spring")
    public interface ProjetBoMapper {
        ProjetBo map(ProjetEntity projetEntity);
    
        List<ProjetBo> mapList(List<ProjetEntity> listeProjetEntities) ;
    }
    

    Si je vais voir le mapper ProjetBoMapperImpl généré, je vois que l'id projet n'est pas setté :

    protected ImageBo imageEntityToImageBo(ImageEntity imageEntity) {
            if ( imageEntity == null ) {
                return null;
            }
    
            ImageBo.ImageBoBuilder imageBo = ImageBo.builder();
    
            imageBo.idImage( imageEntity.getIdImage() );
            imageBo.name( imageEntity.getName() );
            imageBo.location( imageEntity.getLocation() );
            if ( imageEntity.getFavorite() != null ) {
                imageBo.favorite( Boolean.parseBoolean( imageEntity.getFavorite() ) );
            }
    
            return imageBo.build();
        }

    J'imagine donc qu'il manque une indication de type "source" / "target" sur mon mapper, mais je n'arrive pas à trouver les bonnes infos à indiquer... un peu d'aide serait donc la bienvenue :) N'hésitez pas à me demander si il y a besoin d'informations complémentaire ou d'un lien github vers le projet !


    -
    Edité par Nhibel 1 août 2022 à 21:16:19

    • Partager sur Facebook
    • Partager sur Twitter
      3 août 2022 à 18:52:26

      Hello !

      J'ai trouvé une solution en procédant ainsi :

      @Mapper(componentModel="spring")
      public interface ProjetBoMapper {
      
          @Mapping(source = "projetEntity", target = "images", qualifiedByName = "mapImagesBo")
          ProjetBo map(ProjetEntity projetEntity);
      
          List<ProjetBo> mapList(List<ProjetEntity> listeProjetEntities);
      
          @Named("mapImagesBo")
          default List<ImageBo> mapImagesBo(ProjetEntity projetEntity) {
              List<ImageBo> listeImagesBo = new ArrayList<>();
      
              for (ImageEntity image : projetEntity.getImages()) {
                  ImageBo imageBo = new ImageBo();
                  imageBo.setIdImage(image.getIdImage());
                  imageBo.setProjetsId(projetEntity.getId());
                  imageBo.setLocation(image.getLocation());
                  imageBo.setName(image.getName());
                  imageBo.setFavorite(BooleanConverter.convertToBoolean(image.getFavorite()));
      
                  listeImagesBo.add(imageBo);
              }
              return listeImagesBo;
          }
      }
      

      Pensez-vous que ça soit une bonne solution, ou bien qu'il est possible de procéder d'une meilleur façon ?

      -
      Edité par Nhibel 3 août 2022 à 19:01:33

      • Partager sur Facebook
      • Partager sur Twitter

      Mapstruct - foreign key null

      × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié.
      × Attention, ce sujet est très ancien. Le déterrer n'est pas forcément approprié. Nous te conseillons de créer un nouveau sujet pour poser ta question.
      • Editeur
      • Markdown