简体   繁体   中英

Spring data JPA projection - get all rows for the projected columns

I am trying to use Spring data JPA projection to get a specific columns in my database. In my setup I have my entity class that has the fields representing the columns, and also I created a view to get projected data. I am having issues with creating the method in the repository class . I want to get all rows in database for that view (I dont want to have a query, something like getPersonViewById), but Spring complains with:

java.lang.IllegalArgumentException: Failed to create query for method

When I change the method in Repository to getPersonViewById() it works fine.

Any idea how have the query in the repository class? Here is the code I have:

@Entity
public class Person {
    
    @Id
    @Column (name = "id")
    private final UUID id;
    
    @Column (name = "name")
    private String name;
    
    @Column (name = "address")
    private String address;
    
    public UUID getId () {
        return id;
    }
    
    public String getName () {
        return name;
    }
    
    public interface PersonView {
        String getName();
        String getId();
    }
    
}

@Repository
public interface PersonRepository extends Repository<Person, Long> {
    List<PersonView> getPersonView ();
}

I found the answer for this. If I use getPersonViewBy(), without anything after 'By' in the method name, it will return all the rows.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM