user331465
user331465

Reputation: 3094

Spring Boot Way to Replace Views in SQL

Context

I've inherited a small Spring-based project --it tracks servers, software installations, etc for or teams internal use. It uses Angular front end and Spring/java/mysql back end for a restful back end.

I've converted it to Spring boot over the last few months. I'm new to Spring and Spring boot. So I'm learning as I go along.

I started replacing the 'hand coded' sql with JPA ("spring-data-rest"). JPA has been easy for some tables, but not for others. For these "others": The hand-coded SQL uses "views" when retrieving data .

Approach

Specifically, it uses this approach:

Questions

thanks

Upvotes: 1

Views: 2982

Answers (1)

Cepr0
Cepr0

Reputation: 30329

I assume you can use the usual projection with native query to get data from views. Something like this:

public interface ViewResult {
    String getField1();
    Integer getField2();
    Double getField3();
    //...
}

public interface SomeRepository extends JpaRepository<SomeEntity, Long> {
    @Query(value = "select v.field1 as field1, v.field2 as field2, v.field3 as field3 from some_view v", nativeQuery = true)
    List<ViewResult> getViewResult();
}

To work with complex query you can also use projections:

@Query("select a as abc, b as bcd, c as cde from Abc a join a.bcd b join a.cde c")
List<AbcBcdCde> getComplexData();

where AbcBcdCde is a projection:

public interface AbcBcdCde {
    Abc getAbc();
    Bcd getBcd();
    Cde getCde();
}

Upvotes: 2

Related Questions