我面对 "java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap"
I Am Facing "java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap"
我得到:
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
在我的 Spring 引导项目中,我在其中尝试使用 Hibernate One-To-Many 关系 来保存两个 Pojo classes 的数据。我正在尝试保存已定义 Collection 元素集的持久 class 的值。
Parent Pojo Class:-
package com.example.demo.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "vendor")
public class Vendor {
@Id
int vendorId;
@Column
String vendorName;
@OneToMany(fetch = FetchType.LAZY, targetEntity = Customer.class, cascade = CascadeType.ALL)
@JoinColumn(name = "vendorId")
Set children;
public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Child 波乔 Class:-
package com.example.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GeneratorType;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@Entity
@Table(name = "customer")
public class Customer {
@Id
int customerId;
@Column
String customerName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
控制器Class:-
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Vendor;
import com.example.demo.service.VendorDataSaveService;
@RestController
public class VendorSaveController {
@Autowired
private VendorDataSaveService dataSaveService;
@PostMapping("/save")
public void saveVendor(@RequestBody Vendor vendor) {
dataSaveService.saveVendorRecord(vendor);
}
}
服务Class:-
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.Vendor;
import com.example.demo.repository.VendorDataSaveRepository;
@Service
public class VendorDataSaveService {
@Autowired
private VendorDataSaveRepository repository;
public void saveVendorRecord(Vendor vendor) {
repository.save(vendor);
}
}
存储库Class:-
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Vendor;
public interface VendorDataSaveRepository extends JpaRepository<Vendor, Integer> {
}
我从 Postman 发送的 JSON 格式:-
{
"vendorId" : 101,
"vendorName" : "JAIN BOOKS",
"children" : [{
"customerId" : 1,
"customerName" : "AMIT"
}]
}
来自控制台的错误消息:-
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
邮递员的错误信息:-
Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}"
需要在设置子集合类型中添加通用类型 Customer 参数。
Set<Customer> children;
我得到:
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
在我的 Spring 引导项目中,我在其中尝试使用 Hibernate One-To-Many 关系 来保存两个 Pojo classes 的数据。我正在尝试保存已定义 Collection 元素集的持久 class 的值。
Parent Pojo Class:-
package com.example.demo.model;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "vendor")
public class Vendor {
@Id
int vendorId;
@Column
String vendorName;
@OneToMany(fetch = FetchType.LAZY, targetEntity = Customer.class, cascade = CascadeType.ALL)
@JoinColumn(name = "vendorId")
Set children;
public int getVendorId() {
return vendorId;
}
public void setVendorId(int vendorId) {
this.vendorId = vendorId;
}
public String getVendorName() {
return vendorName;
}
public void setVendorName(String vendorName) {
this.vendorName = vendorName;
}
public Set getChildren() {
return children;
}
public void setChildren(Set children) {
this.children = children;
}
}
Child 波乔 Class:-
package com.example.demo.model;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GeneratorType;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
@Entity
@Table(name = "customer")
public class Customer {
@Id
int customerId;
@Column
String customerName;
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
}
控制器Class:-
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.Vendor;
import com.example.demo.service.VendorDataSaveService;
@RestController
public class VendorSaveController {
@Autowired
private VendorDataSaveService dataSaveService;
@PostMapping("/save")
public void saveVendor(@RequestBody Vendor vendor) {
dataSaveService.saveVendorRecord(vendor);
}
}
服务Class:-
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.Vendor;
import com.example.demo.repository.VendorDataSaveRepository;
@Service
public class VendorDataSaveService {
@Autowired
private VendorDataSaveRepository repository;
public void saveVendorRecord(Vendor vendor) {
repository.save(vendor);
}
}
存储库Class:-
package com.example.demo.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.example.demo.model.Vendor;
public interface VendorDataSaveRepository extends JpaRepository<Vendor, Integer> {
}
我从 Postman 发送的 JSON 格式:-
{
"vendorId" : 101,
"vendorName" : "JAIN BOOKS",
"children" : [{
"customerId" : 1,
"customerName" : "AMIT"
}]
}
来自控制台的错误消息:-
java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap
邮递员的错误信息:-
Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}; nested exception is org.hibernate.property.access.spi.PropertyAccessException: Error accessing field [int com.example.demo.model.Customer.customerId] by reflection for persistent property [com.example.demo.model.Customer#customerId] : {customerId=1, customerName=AMIT}"
需要在设置子集合类型中添加通用类型 Customer 参数。
Set<Customer> children;