Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用SpringJPA,我可以在实体中保存函数的返回值吗?_Java_Spring_Hibernate_Jpa - Fatal编程技术网

Java 使用SpringJPA,我可以在实体中保存函数的返回值吗?

Java 使用SpringJPA,我可以在实体中保存函数的返回值吗?,java,spring,hibernate,jpa,Java,Spring,Hibernate,Jpa,尝试使用SpringBoot和JPA从API读取数据,然后通过一些修改将数据保存到MyDB中 我有一个实体,如: @Entity @Table(name="BOOK_TABLE") public class Book{ //...some normal variables (name, pageCount, etc.) //I don't want this in the table @Transient SeriesSummary seriesSummary;

尝试使用SpringBoot和JPA从API读取数据,然后通过一些修改将数据保存到MyDB中

我有一个实体,如:

@Entity
@Table(name="BOOK_TABLE")
public class Book{
    //...some normal variables (name, pageCount, etc.)

    //I don't want this in the table
    @Transient SeriesSummary seriesSummary;

    //I want to save the output of this to the table
    public Integer getSeriesId() {
        //... does some stuff that figures out the id from the SeriesSummary
        return seriesId;
    }
}
这样我就可以将一本书映射到book_表:

图书id、名称、页数…、序列id

此seriesId可用于在Series_表中查找Series对象。但是Series对象不同于Book类中的SeriesSummary对象。SeriesSummary只是来自API的东西,它提供了从外部API及其Id查找系列的URL。我对存储此URL不感兴趣。我只想抓取Book对象,然后我将查找Series对象,存储它们,并将它们映射到我的DB中

我似乎无法让Spring+JPA保存getSeriesId函数的输出。只能保存类变量吗?如果是这样,你将如何解决这个问题

这些对象的格式与外部API的结构相同,因此我可以使用RestTemplate下载数据对象。我是否需要创建一个单独的类来保存我想要的列


谢谢大家!

-可能可以帮助您。好吧,通过阅读该线程,我遇到了很多关于是注释字段还是属性(getter)的不同意见。我不确定这是否直接回答了我的问题。接受答案中的“get昵称”似乎暗示您可以保存一个不是声明字段的值,但这不是一个完整的示例,因此我不能确定nick name不是字段。当我尝试将@Column添加到我的getSeriesId()时,JPA/Spring似乎根本没有注意到该列。事实上,如果我阅读正确,您似乎无法混合使用字段和属性访问。因为我在字段中使用“@id”定义了我的书的id,所以我不能将@Column添加到像getSeriesId()这样的属性中。看起来我可能需要将所有列定义移动到getter,以包含一个未定义字段的值。我稍后会尝试,但我认为这会解决我的问题。