McAviti
McAviti

Reputation: 69

Hibernate/reflection issue in OneToMany relation: IllegalArgumentException

When I try to save an object on the n-side that has the object on the 1-side set, I receive the following error:

Dec 07, 2018 10:37:07 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: Error accessing field [public java.lang.String eu.jaas.document.Document.id] by reflection for persistent property [eu.jaas.document.Document#id] : 17842965-4ea8-41b6-9653-110965f6db13; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [public java.lang.String org.myorg.document.Document.id] by reflection for persistent property [org.myorg.document.Document#id] : doc123] with root cause
java.lang.IllegalArgumentException: Can not set java.lang.String field eu.jaas.document.Document.id to java.lang.String
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:167)
    at java.base/jdk.internal.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:171)

The 1-side Java object is defined as follows:

@Entity
public class LawCase {
    ..
    @Id
    public String id;

    @OneToMany(mappedBy = "lawcase")
    public @Setter Set<Document> documents = new HashSet<>();
    ..
}

And the n-side:

@Entity
public class Document {
    ..
    @Id
    public String id;

    @ManyToOne
    @JoinColumn(name="lawcase")
    public LawCase lawcase;
    ..
}

The IDs are generated by the application itself during the initializiation of the object, so at the point when I try to save the objects, both LawCase and Document objects have a valid id (UUID).

I use springBoot.version 2.1.1.RELEASE and tried with Java8 and Java11. It looks very much like a common hibernate use-case to me, the only "anomaly" might be that I have also lombok annotations on the data classes. I am sure I overlooked some very simple issue, but was blind for the last few days.

Upvotes: 0

Views: 611

Answers (1)

Amila Karunathilaka
Amila Karunathilaka

Reputation: 61

Default ID GenerationType is AUTO. but the problem is this GenerationType didn't support string. This GenerationType support long, int and short data types.

If you didn't set @GeneratedValue annotation, the default is assigned generator which means the value of identifier has to be set by the application.

If you can solve this error using custom GenerationType. You can refer here

Upvotes: 1

Related Questions