[英]SpringBoot Query DTO
由於我的 DTO 課程,我希望檢索數據庫中包含的信息。 問題是如果我不明白為什么我的查詢不起作用......
來自數據庫的實體
@Entity
@Table(name = "historiquedeploiement")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HistoriqueDeploiement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "idnamespace", nullable = false)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
@JsonProperty("idnamespace")
private Namespace namespace;
@ManyToOne(fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "idservice", nullable = false)
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityReference(alwaysAsId=true)
@JsonProperty("idservice")
private Service service;
@NotEmpty
@Size(max = 100)
private String tagvalue;
}
DTO :
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HistoriqueDeploiementReadingDTO {
private Integer id;
@NotEmpty
private String namespacename;
@NotEmpty
private String servicename;
@NotEmpty
private String tagvalue;
}
我的查詢:
@Repository
public interface HistoriqueDeploiementRepository extends JpaRepository<HistoriqueDeploiement, Integer> {
List<HistoriqueDeploiement> findAll();
// Problem Here
Error creating bean with name 'historiqueDeploiementRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.example.jpa.repository.HistoriqueDeploiementRepository.findAllDeploiement()!
@Query("SELECT new com.example.jpa.dto.HistoriqueDeploiementReadingDTO(historiquedeploiement.id, namespace.namespacename, service.servicename, historiquedeploiement.tagvalue) FROM historiquedeploiement, namespace, service WHERE namespace.id = historiquedeploiement.idnamespace and service.id = historiquedeploiement.idservice")
List<HistoriqueDeploiementReadingDTO> findAllDeploiement();
}
我的目標是讓這個查詢工作:)
如果您認為您有比解決這個問題更好的主意,請告訴我! 謝謝
您的 HistoriqueDeploiement 實體缺少 @Entity:
@Entity
public class HistoriqueDeploiement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
... the rest of the class
}
如果沒有那個標簽 Spring-boot ORM 不知道該類代表一個實體,也不能對其執行查詢。
在這里你可以找到關於 @Entity 標簽的文檔的解釋:
Customer類用@Entity注解,表示它是一個JPA實體。 (因為不存在@Table 注釋,所以假設該實體映射到名為 Customer 的表。)
在我這邊工作的解決方案是這個!
package com.example.jpa.services.historiquedeploiement;
import java.util.List;
import java.util.stream.Collectors;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Service;
import com.example.jpa.repository.HistoriqueDeploiementRepository;
import com.example.jpa.dto.HistoriqueDeploiementReadingDTO;
import com.example.jpa.model.HistoriqueDeploiement;
@Service
@Configuration
public class MapService {
@Autowired
private HistoriqueDeploiementRepository historiqueDeploiementRepository;
@Autowired
private ModelMapper modelMapper;
@Bean
public ModelMapper modelMapper() {
ModelMapper modelMapper = new ModelMapper();
return modelMapper;
}
public List<HistoriqueDeploiementReadingDTO> getAllHistorique() {
return ((List<HistoriqueDeploiement>) historiqueDeploiementRepository
.findAll())
.stream()
.map(this::convertToHistoriqueDeploiementReadingDTO)
.collect(Collectors.toList());
}
private HistoriqueDeploiementReadingDTO convertToHistoriqueDeploiementReadingDTO(HistoriqueDeploiement historiqueDeploiement) {
modelMapper.getConfiguration()
.setMatchingStrategy(MatchingStrategies.LOOSE);
HistoriqueDeploiementReadingDTO historiqueDeploiementReadingDTO = modelMapper
.map(historiqueDeploiement, HistoriqueDeploiementReadingDTO.class);
return historiqueDeploiementReadingDTO;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.