Issue
I want to have the relationship OneToMany between two entity but I have this error when I try to save object into database:
on console
java.lang.IllegalArgumentException: Can not set java.lang.Long field com.pi.MinuteBrico.models.Category.idCtegory to java.util.LinkedHashMap
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]
at java.base/java.lang.reflect.Field.get(Field.java:419) ~[na:na]
on psotman
org.springframework.orm.jpa.JpaSystemException: Error accessing field [private java.lang.Long com.pi.MinuteBrico.models.Category.idCtegory] by reflection for persistent property [com.pi.MinuteBrico.models.Category#idCtegory] : {name=Mecanique}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [private java.lang.Long com.pi.MinuteBrico.models.Category.idCtegory] by reflection for persistent property [com.pi.MinuteBrico.models.Category#idCtegory] : {name=Mecanique}\r\n\tat org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:331)\r\n\tat org.springframework.orm.jpa.vendor.
my entities classes:
1: Bricoleur.java
package com.pi.MinuteBrico.models;
import java.util.Map;
import java.io.Serializable;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;
@Entity
@Table(name = "Bricoleur")
public class Bricoleur implements Serializable {
/**
*@author iliass Alilou
*/
private static final long serialVersionUID = 1L;
/*@SequenceGenerator(
name = "Bricoleur_sequence",
sequenceName = "Bricoleur_sequence",
allocationSize = 1
)
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "Bricoleur_sequence"
)*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String photo;
private String firstName;
private String lastName;
private String email;
private String phone;
private String birthDate;
private String adresse;
@OneToMany(/*fetch = FetchType.LAZY , targetEntity = Category.class,*/ cascade = CascadeType.ALL)
@JoinColumn(name = "BricoCategory_Bricoleur",referencedColumnName = "id")
private List<Category> category ;
public Bricoleur() {
}
public Bricoleur(String photo,
String firstName,
String lastName,
String email,
String phone,
String birthDate,
String adresse
) {
super();
this.photo = photo;
this.firstName = firstName;
this.lastName = lastName;
this.email = email;
this.phone = phone;
this.birthDate = birthDate;
this.adresse = adresse;
}
// setters and getters
... ..
}
2:Category
package com.pi.MinuteBrico.models;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="Category")
public class Category implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long idCtegory;
private String name;
public Category() {
}
public Category(String name) {
super();
this.name = name;
}
// Stters and getters
}
JSON file to save
{
"photo":"test",
"firstName":"iliass",
"lastName":"alilou",
"email":"[email protected]",
"phone":"0654248574",
"birthDate":"18/08/1999",
"adresse":"xxxxxxxxxxxx",
"category" : [
{
"name" : "Mecanique"
},
{
"name" : "Plombie"
}
]
}
In my Controller
@CrossOrigin()
@PostMapping("/bricoleur")
public String create(@RequestBody Map<String, Object> bricoleurMap) {
System.out.println(bricoleurMap);
Bricoleur bricoleur = new Bricoleur(bricoleurMap);
bricoleurService.saveBricoleur(bricoleur);
return "Bricoleur ajouté";
}
Service
public Bricoleur saveBricoleur(Bricoleur bricoleur) {
return bricoleurRepository.save(bricoleur);
}
Solution
The field category is of type List
:
List<Category> category
but it seems you are trying to assign a LinkedHashMap
, that's a Map
.
Didn't you mean to use a LinkedList
?
We need to see how you are assigning the field category to help you more.
Answered By - Davide