Spring Data JPA - Entities
Using Views as entities
When we are using Views as entities in JPA, we have to be careful about how we define the @Id
. If we use a single column as an Id, there is a chance that the result set will not include the rows in a way that we want.
e.g. If you have a view with a Person’s id and his phone number as two separate columns, and if a Person can have many phone numbers, in the result set of the View, if each of these phone numbers are on a different row, we have to define a Composite Key with both Person.id and Person.phoneNumber in it. If not, JPA will not bring all those distinct rows into the application - and that will lead to a behavior that we do not want.
@NotNull vs @Column(nullable = false)
https://www.baeldung.com/hibernate-notnull-vs-nullable
Unable to find column position by name: column1
The column name column1 was not found in this ResultSet
If we want to read only selected fields from the table, we can’t use the entity class in the result list. Use a projection instead.