Я хотел бы получить данные из двух таблиц @Entity с помощью репозитория Spring Boot JPA (и отобразить их в. html thymeleaf).
Задача: я не хочу иметь отдельный @ Класс сущности (таким образом, отдельная таблица) для представления объединенного результата. Это вообще возможно? Я новичок в Spring и не уверен, что это лучший метод.
Клиент класса
@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;
}
Запрос класса
@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 для сбора результата, но не сохранить его
@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;
}
Наконец-то мой репозиторий:
@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
}
Соответствующая ошибка 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)!