gpt4 book ai didi

java - 我面临 "java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap"

转载 作者:行者123 更新时间:2023-12-01 21:17:39 25 4
gpt4 key购买 nike

我得到:

java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap

在我的 Spring Boot 项目中,我尝试使用 Hibernate 一对多关系 保存两个 Pojo 类的数据。我正在尝试保存定义了 Collection 元素集的持久类的值。

父 Pojo 类:-

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;
}
}

子 Pojo 类:-

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;
}

}

Controller 类:-

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);
}
}

服务等级:-

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);
}
}

存储库类:-

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

postman 上的错误消息:-

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 - 我面临 "java.lang.IllegalArgumentException: Can not set int field com.example.demo.model.Customer.customerId to java.util.LinkedHashMap",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58861651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com