Social Media

Tag Archives for " database views "

Calling Database Views From Spring Data JPA

Im not going to go into pro’s and con’s as to whether database views are the correct approach with a JPA subsystem, but will say that a lot of the time there isnt a discussion as the view already exists and you will just need to use it to complete your task

Lets consider this view –

SELECT my_view_id, my_view_name FROM my_table;

We map this to a JPA object as –

@Table(name = "my_view")
public class MyView implements Serializable {
private static final long serialVersionUID = 1L;
@Column(name = "my_view_id")
private Long myViewId;
@Column(name = "my_view_name")
private String myViewName;

We then create a repository –

public interface MyViewRepository extends CrudRepository<MyView, Long> {

We can then call this –

private MyViewRepository myViewRepository;
// ...
long count = myViewRepository.count();