简体   繁体   中英

spring data inheritance for repository reuse

I am using Spring Data to load an object and all works well...However, I don't want to load the entire object as I am returning a list to display in a table, so I only want to load what is in the table. then, when a user selects "details" I want to make an AJAX call to the server to load the entire object. My thought was to have a Base Class "TableView" then to have a subclass "Class DetailsView extends TableView". I could probably create a new repository, so one for the TableView and one for the DetailsView, but I'm wondering if there is a way to use the same repository class? below is an example of what I'd like to do, but I'm not sure how to change the repositoryClass to achieve what I want...I get the following error:

SQLGrammarException: could not extract ResultSet at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261)

class TableView{
    String title;
}
class DetailsView extends TableView{
    String details;
}
interface ITableViewRepository extends CrudRepository<TableView, Integer>{

You can write two queries in your TableViewRepository .

One for returning id and title from you object

@Query("SELECT tv.id, tv.title FROM TableView tv") 
TableView findWithTitles();

And after that just call a method findOne with TableView id to return entire object.

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