简体   繁体   中英

JpaSystemException: Error accessing field [XXXXXX] by reflection for persistent property [XXXXXX] : 1

I'm fairly new to JPA/Hibernate, so I decided to create a Library Rest Application to improve. I'm attempting to save a libraryCatalogItem with a libraryId FK so I know which Library a libraryCatalogItem belongs to.

Below is the outline for my LibraryCatalogItem.class:


import com.LibraryApplication.Library.model.Library;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.*;

//Created class abstract so this class can't be implemented
@Entity
//Implemented Single Table strategy for Hibernate Inheritance Mapping and used Discriminator values to identify different records
@DiscriminatorColumn(name = "library_catalog_item_type", discriminatorType = DiscriminatorType.STRING)
public abstract class LibraryCatalogItem {

    @Id
    @Column(name = "catalog_item_primary_key")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    protected Integer Id;


    //declared private for encapsulation
    @Getter
    @Setter
    protected String title, genre;

    @JoinColumn(name = "library_fk"/*, nullable = false*/) //TODO uncomment nullable = false, once I figure out how to have foreign key
    @ManyToOne(targetEntity = Library.class, cascade=CascadeType.ALL)
    protected Integer libraryFk;

    protected LibraryCatalogItem() {
    }

    @Override
    public String toString() {
        return "LibraryCatalogItem{" +
                "title='" + title + '\'' +
                ", genre='" + genre + '\'' +
                ", id=" + Id +
                '}';
    }

}

and this is my Library class:


import com.LibraryApplication.Library.model.LibraryCatalogItems.LibraryCatalogItem;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.*;
import java.util.List;

@Entity
public class Library {

    @Id
    @Column(name="library_primary_key")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Getter
    @Setter
    protected Integer libraryPrimaryKey;

    @Getter
    @Setter
    private Integer phoneNumber;

    @Getter
    @Setter
    private String name, address, email;

    //TODO make libraryId foreign key relate to LibraryCatalogItem Table
    @OneToMany(mappedBy = "libraryFk", targetEntity = LibraryCatalogItem.class, fetch = FetchType.LAZY)
    private List<LibraryCatalogItem> libraryCatalogItems;

}

The issue I am facing is when I send a request to save a libraryCatalogItem I receive the following issue:

org.springframework.orm.jpa.JpaSystemException: Error accessing field [protected java.lang.Integer com.LibraryApplication.Library.model.Library.libraryPrimaryKey] by reflection for persistent property [com.LibraryApplication.Library.model.Library#libraryPrimaryKey] : 1; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [protected java.lang.Integer com.LibraryApplication.Library.model.Library.libraryPrimaryKey] by reflection for persistent property [com.LibraryApplication.Library.model.Library#libraryPrimaryKey] : 1] with root cause

java.lang.IllegalArgumentException: Can not set java.lang.Integer field com.LibraryApplication.Library.model.Library.libraryPrimaryKey to java.lang.Integer
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167) ~[na:na]
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171) ~[na:na]
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.ensureObj(UnsafeFieldAccessorImpl.java:58) ~[na:na]
    at java.base/jdk.internal.reflect.UnsafeObjectFieldAccessorImpl.get(UnsafeObjectFieldAccessorImpl.java:36) ~[na:na]

Would anyone be able to point me in the right direction in resolving this issue? Thank you.

The libraryPrimaryKey property is missing a @Getter and @Setter. I think without a getter and setter, JPA won't be able to access the property. Here is some info about it: https://docs.oracle.com/cd/E19798-01/821-1841/bnbqb/index.html

"Persistent instance variables must be declared private, protected, or package-private and can be accessed directly only by the entity class's methods. Clients must access the entity's state through accessor or business methods."

This isn't an exact fix to the issue above, but what I ended up doing is taking a different approach to essentially reach the same solution. The intended goal was to ensure that each child entity had a relation to a library entity by including libraryFk column in child entity table. So instead of using SingleTableInheritance Mapping I used MappedSuperClassInheritance Mapping.

Please refer to below article: https://www.baeldung.com/hibernate-inheritance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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