![](/img/trans.png)
[英]How to return a custom object from a Spring Data JPA GROUP BY query
[英]spring boot jpa: return custom object from jpa query not related to table schema
這是我的配置:Java: 1.8 Spring Boot :: v2.3.1.RELEASE
我有以下模型/類:
書籍:書籍類如下。 請注意,這不是與 DB 中的表相關的實體,也可以將其視為從多個表中提取的數據,僅具有必需的屬性。 簡而言之,我不會有以下對象的表模式。
package com.myservice.model;
public class Book {
private long bookId;
private String bookName;
private String bookType;
private String author;
public Book(long bookId, String bookName, String bookType, String author) {
this.bookId = bookId;
this.bookName = bookName;
this.bookType = bookType;
this.author = author;
}
public long getBookId() {
return bookId;
}
public void setBookId(long bookId) {
this.bookId = bookId;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public String getBookType() {
return bookType;
}
public void setBookType(String bookType) {
this.bookType = bookType;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
與上面類似,我還有一個班級,
package com.myservice.model;
public class Business {
private String businessName;
private String businessType;
private String owner;
public Book(String businessName, String businessType, String owner) {
this.businessName = businessName;
this.bookType = bookType;
this.owner = owner;
}
public String getBusinessName() {
return businessName;
}
public void setBusinessName(String businessName) {
this.businessName = businessName;
}
public String getBusinessType() {
return businessType;
}
public void setBusinessType(String businessType) {
this.businessType = businessType;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
我需要作為以下對象的輸出
import java.util.List;
public class Configuration {
private List<Book> books;
private List<Business> businesses;
private List<String> authors;
private List<String> owners;
// Constructor and getters/setters omitted for brevity
}
所以,我試圖擁有一個存儲庫
import com.myservice.model.Configuration;
import org.springframework.data.jpa.repository.JpaRepository;
public interface ConfigurationRepository extends JpaRepository<Configuration, Long>, ConfigurationRepositoryCustom {
}
自定義界面
import com.myservice.model.Configuration;
public interface ConfigurationRepositoryCustom {
Configuration getConfigData();
}
執行:
@Repository
@Transactional(readOnly = true)
public class ConfigurationRepositoryImpl implements ConfigurationRepositoryCustom {
@PersistenceContext
EntityManager entityManager;
private List<Book> getBooks() {
Query query = entityManager.createNativeQuery(
"SELECT book_id, book, bookType, author FROM TABLE1 JOIN TABLE2 <Other criterias>"
, Book.class
);
return query.getResultList();
}
private List<Business> getBusinesses() {
List<Business> businesses = new ArrayList<>();
businesses.add(new Business("ABC", "Constructions", "ABC PQR" ));
businesses.add(new Business("XYZ", "Constructions", "PQR CCC" ));
businesses.add(new Business("PQR", "Education", "ABC PQR" ));
return businesses;
}
@Override
public Configuration getConfigData() {
List<Book> books = getBooks();
List<Business> businesses = getBusinesses();
List<String> authors = books.stream().map(Book::getAuthor).collect(Collectors.toList());
List<String> owners = businesses.stream().map(Book::getOwner).collect(Collectors.toList());
Configuration config = new Configuration(
books,
businesses,
authors,
owners
);
return config;
}
}
基本上,如果您看到 Configuration 對象不在 DB 中。
當我啟動應用程序時,它首先拋出了這個錯誤:
Caused by: java.lang.IllegalArgumentException: Not a managed type: class com.myservice.model.Configuration
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:582) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.metamodel.internal.MetamodelImpl.managedType(MetamodelImpl.java:85) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.springframework.data.jpa.repository.support.JpaMetamodelEntityInformation.<init>(JpaMetamodelEntityInformation.java:75) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaEntityInformationSupport.getEntityInformation(JpaEntityInformationSupport.java:66) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getEntityInformation(JpaRepositoryFactory.java:229) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:179) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:162) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
at org.springframework.data.jpa.repository.support.JpaRepositoryFactory.getTargetRepository(JpaRepositoryFactory.java:72) ~[spring-data-jpa-2.3.1.RELEASE.jar:2.3.1.RELEASE]
對於上述,我添加了@Entity? 對我的 Configuration 類的注釋
接下來,它拋出了錯誤:
Caused by: org.hibernate.AnnotationException: No identifier specified for entity: com.myservice.model.Configuration
at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.cfg.InheritanceState.getElementsToProcess(InheritanceState.java:211) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:781) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
現在,我的配置不是數據庫中的表,我仍然添加了屬性“id”
@Id
@GeneratedValue
private long id;
接下來,我收到此錯誤:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.List, at table: configuration, for columns: [org.hibernate.mapping.Column(books)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:499) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:466) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
at org.hibernate.mapping.Property.isValid(Property.java:227) ~[hibernate-core-5.4.17.Final.jar:5.4.17.Final]
我的數據庫中當然沒有這樣的表。
因此,基本上我正在尋找一種基於其他對象(很少來自 DB,很少來自其他調用等)創建自定義(此處為配置)對象的方法,並且此自定義對象不在 DB 中。 那么如何實現相同的呢?
你不需要這個接口
public interface ConfigurationRepository extends JpaRepository<Configuration, Long>, ConfigurationRepositoryCustom {
}
相反,直接在您的代碼中使用ConfigurationRepositoryCustom
,您也可以使用名稱ConfigurationRepository
(無需以 custom 為后綴)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.