无法通过反射设置字段值[49]值

时间:2017-08-18 13:21:13

标签: hibernate spring-boot spring-data-jpa

有人可以帮我解决这个问题,我正在使用基于Spring Boot 1.5.2的项目吗?

我有父级和类别实体 父母,直截了当,有:

    @Id
    @GeneratedValue
    @Column(name = "parent_id")
    private Long id;

    @JsonManagedReference
    @OneToMany(mappedBy = "parent", fetch = FetchType.LAZY)
    @Cascade({org.hibernate.annotations.CascadeType.SAVE_UPDATE,
            org.hibernate.annotations.CascadeType.DELETE,
            org.hibernate.annotations.CascadeType.MERGE,
            org.hibernate.annotations.CascadeType.PERSIST,
            org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
    private List<Category> categories;

    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }
...setters, getters, etc
}

Categories表有一个复合键(parent_id,category_code) 所以我定义了一个Embeddable类

@Embeddable
public class CategoryKey implements Serializable {
    private String categoryCode;

    private Long parentId;

    ... getters, setters, overriden equals and hashCode method
}

在我有的类别实体中

public class Category implements Serializable {
    @EmbeddedId
    private CategoryKey categoryKey;

    @JsonBackReference
    @ManyToOne
    @MapsId("parentId")
    @JoinColumn(name = "parent_id", insertable = false, updatable = false)
    private Parent parent;

    ... getters, setters,no-arg constructor
}

当我尝试发出POST操作以保存新的Parent时,它会抛出

无法通过反射设置字段值[此处为新的parent_id]值

如何克服这个问题?

1 个答案:

答案 0 :(得分:0)

使用String代替long

private Long id; ===> private String id;