简体   繁体   English

我是否必须为连接的查询创建单独的 @Entity 类才能提取结果? 存储库

[英]Do I have to create separate @Entity class for a joined query to pull the result? JpaRepository

I would like to pull data from two @Entity tables using Spring Boot JPA Repository (and display it in .html thymeleaf).我想使用 Spring Boot JPA Repository 从两个 @Entity 表中提取数据(并将其显示在 .html thymeleaf 中)。

Challenge: I don't really want to have a separate @Entity class (thus a separate table) to represent the joined result.挑战:我真的不想有一个单独的 @Entity 类(因此是一个单独的表)来表示连接的结果。 Is it even possible?甚至有可能吗? I'm new to Spring and unsure what's the best practice.我是 Spring 的新手,不确定什么是最佳实践。

Class Client班级客户

@Data
@Entity
@Table(name="client", uniqueConstraints={@UniqueConstraint(columnNames={"client_id"})})
public class Client {

    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "client_seq")
    @SequenceGenerator(name = "client_seq", initialValue = 50)
    @Column(name="client_id", nullable = false, unique = true, length = 9)
    private long client_id;
    @Column(name="NAME", nullable = false,  length = 20)
    private String name;
    @Column(name="SURNAME", nullable = false,  length = 20)
    private String surname;
    @Column(name="CITY", nullable = false,  length = 20)
    private String city;
    @Column(name="COUNTRY", nullable = false,  length = 20)
    private String country;
    @Column(name="BIO", nullable = false,  length = 200)
    private String bio;

    @OneToMany(mappedBy = "client")
    private Set<Request> requests;

    public Client() {
    }

    public Client(String name, String surname, String city, String country, String bio) {
        this.name = name;
        this.surname = surname;
        this.city = city;
        this.country = country;
        this.bio = bio;
    }

Class Request课程要求

@Entity
@Data
@Table(name="request",uniqueConstraints = {@UniqueConstraint(columnNames = "request_id")})
public class Request {

    public enum Status
    {
        PENDING, BACKED, DONE;
    }

    @Id
    @Column(name = "request_id", unique = true, nullable = false, length = 9)
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long requestId;

    @ManyToOne
    @JoinColumn(name="client", referencedColumnName = "client_id")
    private Client client;

    @Column(name="request_short", nullable = false,  length = 100)
    private String requestShort;

    @Column(name = "request_long", nullable = false,  length = 250)
    private String requestLong;

    @Column(name = "request_date", nullable = false)
    private LocalDate requestDate;

    @Column(name = "status")
    private Status status;

    public Request(String requestShort, String requestLong, LocalDate requestDate, Status status) {
        this.requestShort = requestShort;
        this.requestLong = requestLong;
        this.requestDate = requestDate;
        this.status = status;
    }

}

DEMO class to collect the result but do not store it DEMO 类收集结果但不存储它

@Data
@Component
public class ClientRequest {

    private long client_id;
    private long requestId;

    public ClientRequest(){};

    public ClientRequest(long client_id, long requestId) {
        this.client_id = client_id;
        this.requestId = requestId;
}

Finally my repository:最后我的存储库:

@Repository
public interface ClientRepository extends JpaRepository<Client, Long> {

    List<Client> findByName(String name);    // **works**

    @Query("SELECT a FROM Client a WHERE client_id > ?1")
    List<Client> findByUser_id(Long user_id);             // **works**

    @Query("SELECT a.client_id, b.request_id FROM Client a JOIN a.Request b WHERE a.client_id > ?1")
    List<ClientRequest> findPendingRequestsWithUserData(Long user_id);     // fails

  }

Relevant error MSG:相关错误味精:

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webController': Unsatisfied dependency expressed through field 'clientRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientRepository': Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Validation failed for query for method public abstract java.util.List com.dev.repository.ClientRepository.findPendingRequestsWithUserData(java.lang.Long)!

Try this.尝试这个。 This should work.这应该有效。

@Query("SELECT new com.path.to.ClientRequest(a.client_id, b.request_id) FROM Client a JOIN a.requests b WHERE a.client_id > ?1")
List<ClientRequest> findPendingRequestsWithUserData(Long user_id); 

You need to create new object using new and full path of the class.您需要使用类的new路径和完整路径创建新对象。 That is what is missing.这就是所缺少的。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在内部 class 上创建 2 个单独的实例作为外部 class 的单独属性,这是一个 JPA 实体? - How do I create 2 separate instances on an inner class as separate properties of the outer class which is a JPA entity? 在JPARepository中获取汇总的查询结果 - Get aggregated query result in JPARepository 是否必须为我创建的每个组件创建一个单独的ActionListener? - Do I have to create a separate ActionListener for each component I create? 我可以 map 对不是实体的 class 进行休眠/jpa 查询的结果吗? - Can I map the result of a hibernate/jpa query to a class that is not an entity? 我怎样才能有单独的 gradle 项目的调试和发布版本,它们可以引入相同 class 的不同版本? - How can I have separate debug and release versions of gradle project that pull in different versions of the same class? 我是否为集合创建了一个单独的类或将它们存储在同一个类中? - Do I create a separate class for collection or store them in the same class? 使用@Query时如何使用Spring Data JpaRepository构造Entity类 - How to construct an Entity class using Spring Data JpaRepository when using @Query 我的实体 class 中有一个 GregorianCalender,如何创建一个 object,它在 toString() 中具有特定日期和特定格式显示? - I have a GregorianCalender in my entity class, how do i create an object which has specific date and display in specific format in toString()? 我总是需要为hibernate中的manymanmany关系创建单独的类 - Do i always need to create separate class for manytomany relation in hibernate 如果我的所有业务逻辑都在服务类中,我是否需要拥有域/实体类? - Do I need to have domain/entity class if all of my business logic is in the service class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM