我在后端代碼中使用 Spring 資料 jpa。我已經包含了物體、dto 介面、服務和 jpa 存盤庫代碼。
現在的問題是,當我打電話getAllTopics()的TopicService。它回傳一個Topic物件串列而不是TopicDto. Topic物件包含一個串列,examples其中我沒有包含在TopicDto. 而且Topicobject 還包括一個Commentobject串列而不是CommentDto.
當我添加這只是發生Set<CommentDto> getComments()在TopicDto。如果我洗掉它,一切正常。誰能告訴我應該如何在我的服務和存盤庫類中映射 dto?為什么它回傳物體類而不是 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;
}
}
uj5u.com熱心網友回復:
第一件事是更改您的TopicRepositoryto use 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你需要做映射 from TopictoTopicDto如下:
@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());
}
}
uj5u.com熱心網友回復:
– 存盤庫管理的域型別 – 存盤庫管理的物體的 id 型別
所以回傳域物件是正常的。您將@Entity 指定為主題類。
我希望答案有幫助。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/377081.html
