Search code examples
javasqlspring-bootview

How can I reference a view in the entity? (Spring Boot)


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; 
}

Solution

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