繁体   English   中英

spring boot jpa:从与表架构无关的jpa查询返回自定义对象

[英]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.

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