春季启动查询DTO

huangapple go评论89阅读模式
英文:

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();

    @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();
}

我的目标是使这个查询起作用 春季启动查询DTO

如果您认为有比解决这个问题更好的方法,请告诉我!谢谢

英文:

I wish to retrieve the information contained in the database thanks to my DTO class.
The problem is that my query doesn't work without me understanding why...

Entity from database

@Entity
@Table(name = &quot;historiquedeploiement&quot;)
@Data
@NoArgsConstructor
@AllArgsConstructor
public class HistoriqueDeploiement {
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Integer id;    

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = &quot;idnamespace&quot;, nullable = false)    
	@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property=&quot;id&quot;)
	@JsonIdentityReference(alwaysAsId=true)
	@JsonProperty(&quot;idnamespace&quot;)
	private Namespace namespace;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = &quot;idservice&quot;, nullable = false)    
	@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property=&quot;id&quot;)
	@JsonIdentityReference(alwaysAsId=true)
	@JsonProperty(&quot;idservice&quot;)
	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;

}

My Query :

@Repository
public interface HistoriqueDeploiementRepository extends JpaRepository&lt;HistoriqueDeploiement, Integer&gt; {		
	List&lt;HistoriqueDeploiement&gt; findAll();

// Problem Here 
Error creating bean with name &#39;historiqueDeploiementRepository&#39;: 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(&quot;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&quot;)

	List&lt;HistoriqueDeploiementReadingDTO&gt; findAllDeploiement();
}

My goal is to have this query working 春季启动查询DTO

If you think you have a better idea than solving this problem let me know !
Thanks

答案1

得分: 0

您的HistoriqueDeploiement实体缺少@Entity注解:

@Entity
public class HistoriqueDeploiement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;    

... 类的其余部分
}

如果没有这个标签,Spring Boot ORM将无法识别该类表示一个实体,并且无法在其上执行查询。

您可以在这里找到关于@Entity标签的文档解释:

> Customer类被注解为@Entity,表示它是一个JPA实体。(因为没有@Table注解,假定此实体被映射到名为Customer的表。)

英文:

Your HistoriqueDeploiement entity is missing the @Entity:

@Entity
public class HistoriqueDeploiement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;    

... the rest of the class
}

Without that tag Spring-boot ORM does not know that the class represents an entity and can't perform queries on it.

Here you can find the explanation on the docs about the @Entity tag:

> The Customer class is annotated with @Entity, indicating that it is a JPA entity. (Because no @Table annotation exists, it is assumed that this entity is mapped to a table named Customer.)

答案2

得分: 0

以下是翻译好的代码部分:

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;
    }
}
英文:

The solution that is working on my side is this one !

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&lt;HistoriqueDeploiementReadingDTO&gt; getAllHistorique() {
       return ((List&lt;HistoriqueDeploiement&gt;) 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;
    }
}

huangapple
  • 本文由 发表于 2020年10月28日 04:47:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/64562675.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定