[英]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.