1

I have a question. Of course, I have previously looked on the Internet and searched for the solution. Unfortunately, I have not found a solution.

I have a Spring Boot application that processes the information from customers. But this application should not point to a table as usual but to a view that requests information from two tables. How do I have to modify my entity to refenence to the view?

My View

CREATE VIEW customers_view
SELECT
    customer_id, firstname, lastname,
    (SELECT ordernumber FROM orders
     WHERE orders.customer_id = custoumer.id
     ORDER BY customer_id DESC)
FROM customers

My Entity

@Entity
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class CustomerInformation {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @EqualsAndHashCode.Exclude
    private Long id;

    @Column(name = "customer_id")
    private Long customer_id;

    @Column(name = "firstname")
    private String firstname;

    @Column(name = "lastname")
    private String firstname;
    
    @Column(name = "ordernumber")
    private String firstname; 
}
jarlh
  • 42,561
  • 8
  • 45
  • 63
Doncarlito87
  • 359
  • 1
  • 8
  • 25
  • Skip the subquery, do a LEFT JOIN instead. (As it is now you'll get an error if a customer has more than one order.) – jarlh Aug 24 '21 at 12:05
  • Sorry for the confusion. Actually it's about another entity but I didn't want to publish it. So I just created a case study. It is really only a 1 to 1 relationship to be established. – Doncarlito87 Aug 24 '21 at 12:16

1 Answers1

1

Here are some examples for referencing views using spring boot:
JPA/SpringBoot Repository for database view (not table)

Uri Loya
  • 1,181
  • 2
  • 13
  • 34