gpt4 book ai didi

postgresql - 在带有@Query注释的spring数据存储库方法中返回一个DTO对象

转载 作者:行者123 更新时间:2023-11-29 12:51:29 27 4
gpt4 key购买 nike

我有一个存储库,在这个接口(interface)中我有一个方法想要返回一个 CommentarioEditalDto 而不是一个 ComentarioEdital,但是当我调用这个方法时抛出一个异常,告诉我我没有转换器,如何我可以转换为返回另一个类的实例吗?

import br.edu.ifrn.rederenova.dto.ComentarioEditalDto;
import br.edu.ifrn.rederenova.model.ComentarioEdital;
import java.util.List;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

@Repository
public interface ComentarioRepository extends CrudRepository<ComentarioEdital, Long>{

@Query(value = "SELECT c.id, c.texto, c.data_ultima_edicao, c.autor_id, " +
"c.data_criacao, c.edital_id, c.excluida FROM comentario_edital c INNER JOIN edital e ON e.id = c.edital_id " +
"WHERE (c.excluida = FALSE OR c.excluida IS NULL) AND e.numero = :numero ORDER BY c.data_criacao DESC",
nativeQuery = true)
public List<ComentarioEditalDto> findAllByEdital(@Param("numero") String nuEdital);

}

我删除了 Gets 和 Sets

import br.edu.ifrn.rederenova.model.ComentarioEdital;
import br.edu.ifrn.rederenova.model.Edital;
import br.edu.ifrn.rederenova.model.Usuario;
import java.util.Calendar;
import java.util.List;

public class ComentarioEditalDto {

private Long id;
private String texto;
private Calendar dataCriacao;
private Calendar dataUltimaEdicao;
private List<ComentarioEdital> respostasComentario;
private Usuario autor;
private Edital edital;
private Boolean excluida;

}

我删除了 Gets 和 Set

import com.fasterxml.jackson.annotation.JsonIgnore;
import java.io.Serializable;
import java.util.Calendar;
import java.util.List;
import java.util.Objects;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;

@Entity
@SequenceGenerator(allocationSize = 1, initialValue = 1, name = "comentario_seq", sequenceName = "comentario_seq")
public class ComentarioEdital implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "comentario_seq")
private Long id;
@NotBlank(message = "Não deve estar vazio")
private String texto;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Calendar dataCriacao;
@Temporal(TemporalType.TIMESTAMP)
@DateTimeFormat(pattern = "dd/MM/yyyy")
private Calendar dataUltimaEdicao;
@OneToMany(cascade = {CascadeType.REMOVE, CascadeType.REFRESH}, fetch = FetchType.EAGER)
@JoinTable(name = "respostas_comentario",
joinColumns = @JoinColumn(name = "pergunta_id"),
inverseJoinColumns = @JoinColumn(name = "resposta_id"))
private List<ComentarioEdital> respostasComentario;
@ManyToOne(fetch = FetchType.EAGER)
private Usuario autor;
@JsonIgnore
@ManyToOne(fetch = FetchType.LAZY)
private Edital edital;
@JsonIgnore
private Boolean excluida;
}

有一点异常(exception)

org.springframework.core.convert.ConverterNotFoundException: No converter found capable of converting from type [org.springframework.data.jpa.repository.query.AbstractJpaQuery$TupleConverter$TupleBackedMap] to type [br.edu.ifrn.rederenova.dto.ComentarioEditalDto] at org.springframework.core.convert.support.GenericConversionService.handleConverterNotFound(GenericConversionService.java:321) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE] at org.springframework.core.convert.support.GenericConversionService.convert(GenericConversionService.java:194) ~[spring-core-5.0.7.RELEASE.jar:5.0.7.RELEASE]

最佳答案

你需要在你的@Query中返回一个dto

@Query(value = "SELECT new br.edu.ifrn.rederenova.dto.ComentarioEditalDto(c.id, c.texto, c.dataCriacao, c.dataUltimaEdicao, c.excluida) " +
"FROM ComentarioEdital c ....
  • 不要编写 native 查询,您可以使用 JPQL
  • 使用 SELECT new package.DTOClass 作为 SELECT new br.edu.ifrn.rederenova.dto.ComentarioEditalDto
  • 在您的 ComentarioEditalDto 中定义一个构造函数,其参数与您传递给 SELECT new br.edu.ifrn.rederenova.dto.ComentarioEditalDto
  • 的参数相同

关于postgresql - 在带有@Query注释的spring数据存储库方法中返回一个DTO对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52915532/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com