简体   繁体   English

用于存储库重用的 spring 数据继承

[英]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.我正在使用 Spring Data 加载一个对象并且一切正常......但是,我不想加载整个对象,因为我正在返回一个列表以显示在表格中,所以我只想加载桌子。 then, when a user selects "details" I want to make an AJAX call to the server to load the entire object.然后,当用户选择“详细信息”时,我想对服务器进行 AJAX 调用以加载整个对象。 My thought was to have a Base Class "TableView" then to have a subclass "Class DetailsView extends TableView".我的想法是有一个基类“TableView”,然后有一个子类“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?我可能会创建一个新的存储库,一个用于 TableView,一个用于 DetailsView,但我想知道是否有办法使用相同的存储库类? 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:下面是我想要做的一个例子,但我不确定如何更改 repositoryClass 以实现我想要的......我收到以下错误:

SQLGrammarException: could not extract ResultSet at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261) SQLGrammarException:无法在 org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:261) 处提取 ResultSet

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

You can write two queries in your TableViewRepository .您可以在TableViewRepository编写两个查询。

One for returning id and title from you object一个用于从您的对象返回idtitle

@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.之后只需调用带有TableView id的方法findOne即可返回整个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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