簡體   English   中英

Spring jpa 存儲庫返回實體 class 而不是 dto 接口

[英]Spring jpa repository returns the entity class instead of the dto interface

我在后端代碼中使用 Spring 數據 jpa 。 我已經包含了實體、dto 接口、服務和 jpa 存儲庫代碼。

現在的問題是,當我在TopicService中調用getAllTopics()時。 它返回Topic object 而不是TopicDto的列表。 Topic object 包含一個examples列表,我不包含在TopicDto中。 並且Topic object 還包括一個Comment object 而不是CommentDto的列表。

這只發生在我在TopicDto添加Set<CommentDto> getComments()時。 如果我刪除它,一切正常。 誰能告訴我我應該如何 map 我的服務和存儲庫 class 中的 dto? 為什么它返回實體 class 而不是 dto?

@Entity
@Table(name = "TOPIC")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Topic implements Serializable {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;

    @OneToMany(
            fetch = FetchType.LAZY, 
            cascade = CascadeType.REMOVE,
            mappedBy = "topic"
        )
    private Set<Comment> comments= new HashSet<>();

    @OneToMany(
            fetch = FetchType.LAZY, 
            cascade = CascadeType.REMOVE,
            mappedBy = "topic"
        )
    private Set<Example> examples = new HashSet<>();

}

@Entity
@Table(name = "COMMENT")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Comment implements Serializable {
    
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private Long id;
        
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "Topic_ID")
    private Topic topic;

    @OneToMany(
            fetch = FetchType.LAZY, 
            cascade = CascadeType.REMOVE,
            mappedBy = "comment"
        )
    private Set<AnotherExample> anotherExamples = new HashSet<>();  
}

public interface TopicDto{
    Long getId();
    Set<CommentDto> getComments();
}

public interface CommentDto{
    Long getId();
}

public interface TopicRepository extends JpaRepository<Topic, Long> {   
    List<TopicDto> findAllBy(Sort sort);
}

@Service
@Transactional
public class TopicService {
   private final TopicRepository topicRepository ;

   public TopicService(TopicRepository topicRepository ) {
      this.topicRepository = topicRepository ;
   }

   @Transactional(readOnly = true)
   public List<TopicDto> getAllTopics(Sort sort) {
      List<TopicDto> l = topicRepository.findAllBy(sort);
      return l;
   } 
}

首先是將您的TopicRepository更改為使用實際實體Topic ,而不是TopicDto

public interface TopicRepository extends JpaRepository<Topic, Long> {   
    List<Topic> findAllBy(Sort sort);
}

然后,您需要以下 DTO 類:

import java.util.HashSet;
import java.util.Set;

public class TopicDto {
    private Long id;
    private Set<CommentDto> comments= new HashSet<>();

    public TopicDto(Long id, Set<CommentDto> comments) {
        this.id = id;
        this.comments = comments;
    }
}
public class CommentDto {
    private Long id;

    public CommentDto(Long id) {
        this.id = id;
    }
}

最后,在您的TopicService中,您需要執行從TopicTopicDto的映射,如下所示:

@Service
@Transactional
public class TopicService {
   private final TopicRepository topicRepository ;

   public TopicService(TopicRepository topicRepository ) {
      this.topicRepository = topicRepository ;
   }

   @Transactional(readOnly = true)
   public List<TopicDto> getAllTopics(Sort sort) {
      List<Topic> topics = topicRepository.findAllBy(sort);
      return topics.stream()
                .map(topic -> {
                    Set<CommentDto> commentsDto = topic.getComments().stream()
                            .map(comment -> new CommentDto(comment.getId()))
                            .collect(Collectors.toSet());
                    return new TopicDto(topic.getId(), commentsDto);
                })
                .collect(Collectors.toList());
   } 
}

– 存儲庫管理的域類型 – 存儲庫管理的實體 id 的類型

所以返回域 object 是正常的。 您指定了@Entity 主題 Class。

我希望答案是有幫助的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM