Springboot JPA HQL 查询

问题描述 投票:0回答:1

我正在修补以了解有关 springboot jpa 和 mapstruct 的更多信息

我有 2 个实体类并使用 pgadmin4 作为 postgresql。

PostQuestions 有一个到 Users 表的外键 userId

我目前想做一个 @Query 来获取所有用户和 PostQuestions 表

将它们映射到我的前端可以显示正在发布的问题以及由谁附加 json 响应以了解它应该如何显示,我将在哪里映射和显示所有问题和用户的电子邮件,但出于通用目的,我将查询两者表并选择我想要显示的值,因为我也想了解有关映射结构映射的更多信息

但是我面临错误,目前不知道如何纠正我不熟悉堆栈跟踪错误,并且不知道应该从哪里开始纠正

创建名称为“userRepository”的 bean 时出错 io.tracker.track_app.repos.UserRepository 定义于 在 DomainConfig 上声明的@EnableJpaRepositories:无法创建 查询公共抽象 java.util.List io.tracker.track_app.repos.UserRepository.getUsersAndPosts();原因: 方法 public Abstract java.util.List 的查询验证失败 io.tracker.track_app.repos.UserRepository.getUsersAndPosts()

原因:java.lang.IllegalArgumentException:无法比较测试 类型为 [Users] 的表达式,元素类型为 [BasicSqmPathSource(用户 ID:整数)]

我已将我的实体和 dto 类以及我编写 JPA @Query 的存储库附加在一起

图像是我的前端将收到的 json 响应,用于显示我要选择和显示的值

发帖提问

@Entity
@Table(name = "posts", schema = "public")
public class PostQuestion {
    @Id
    @Column(name = "post_id", nullable = false, updatable = false)
    @SequenceGenerator(
            name = "primary_sequence",
            sequenceName = "primary_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    private Integer postId;

    @Column(name="questionnum", nullable = false)
    private Long questionnum;

    @Column(name="title", nullable = false)
    private String title;

    @Column(name="url", nullable = false)
    private String url;

    @Column(name="content" , nullable = false)
    private String content;

    @Column(name = "timestamp")
    private LocalDate timestamp;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    private Users userId;

发布问题DTO

public class PostQuestionDTO {

private Integer postId;

@NotNull
private Long questionnum;

@NotNull
@Size(max = 255)
private String title;

@NotNull
@Size(max = 255)
private String url;

@NotNull
@Size(max = 255)
private String content;

private LocalDate timestamp;

private Integer userId;

private String email;

用户

@Entity
@Table(name = "users", schema="public")
public class Users {

    @Id
    @Column(name = "user_id", nullable = false, updatable = false)
    @SequenceGenerator(
            name = "primary_sequence",
            sequenceName = "primary_sequence",
            allocationSize = 1
    )
    @GeneratedValue(
            strategy = GenerationType.IDENTITY
    )
    private Integer userId;

    @Column(name = "email", nullable = false, unique = true)
    private String email;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "role", nullable = false)
    private String role;

    @OneToMany(mappedBy = "userId")
    private List<PostQuestion> postId;

用户DTO

public class UserDTO {

private Integer userId;

@NotNull
@Size(max = 255)
private String email;

@NotNull
@Size(max = 255)
private String password;

@NotNull
@Size(max = 255)
private String role;

private List<PostQuestionDTO> postQuestionDTO;

用户存储库

public interface UserRepository extends JpaRepository<Users, Integer> {


    @Query("SELECT p, u FROM PostQuestion p INNER Join Users u ON p.userId = u.userId")
    List<Users> getUsersAndPosts();

}

映射器类

@Mapper
public interface UserMapper {

    UserDTO mapToDTO(Users user);

    Users mapToEntity(UserDTO userDTO);
}
java spring spring-boot jpa mapstruct
1个回答
0
投票

这是因为您

JPQL
查询正在查询错误的基表。您的用户存储库已映射到用户实体:

public interface UserRepository extends JpaRepository<Users, Integer> {

}

当然,您的所有查询都需要通过基表到达链接到

User
实体的所有其他实体。

因此,一种解决方案可能是使用派生查询来查询

PostQuestions
不为空的所有用户。

@Repository
public interface UserRepository extends JpaRepository<Users, Integer> {
   
   List<Employee> getUsersByPostIdNotNull();

}

© www.soinside.com 2019 - 2024. All rights reserved.