0

Attempting, using Spring Boot with JPA, to read data from an API, and then save that data into my DB with some alterations.

I have an Entity like:

@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;
    }
}

So that I could map a book to the BOOK_TABLE:

book_id, name, pageCount..., seriesId

This seriesId could be used to look up a Series object in a SERIES_TABLE. But the Series object is different than the SeriesSummary object from my Book class. The SeriesSummary is just something coming from the API that provides the URL to look up the Series from the foreign API and its Id. I have no interest in storing this URL. I just want to grab the Book object, and then I will look up the Series object, store both, and map them together in my DB.

I can't seem to get Spring + JPA to save the output of the getSeriesId function. Can you only save class variables? If so how would you work around this?

The format of these objects is structured in the same way as the foreign API so that I can download the data objects using RestTemplate. Do I need to create a separate Class just for saving the columns I want?

Thank you!

barnesm999
  • 421
  • 3
  • 13
  • 1
    http://stackoverflow.com/questions/594597/hibernate-annotations-which-is-better-field-or-property-access - can probably help you. – ChiefTwoPencils Dec 07 '15 at 01:28
  • Well, reading through that thread I encounter a lot of mixed opinions on whether to annotate the fields or the property (getters). I'm not sure if this directly answers my question. The "getNickName" from the accepted answer seems to suggest that you can save a value that is not a declared field, but it is not a complete example so I can't be sure that nick name isn't a field. And when I tried adding @Column to my getSeriesId() JPA/Spring didn't seem to notice the column at all. – barnesm999 Dec 07 '15 at 18:09
  • Actually, if I am reading correctly, it seems like you can't mix field and property access. Since I define my book's id using "@ID" on the field, I cannot then add @Column to a property like getSeriesId(). It looks like I may need to move all my column definitions to the getters to include a value that is not a defined field. I will try this later but I think this will resolve my problem. – barnesm999 Dec 07 '15 at 18:26

0 Answers0