我正在面对“ java.lang.IllegalArgumentException:无法将int字段com.example.demo.model.Customer.customerId设置为java.util.LinkedHashMap”

问题描述 投票:0回答:2

我正在尝试在其中保存我的Spring Boot项目中的java.lang.IllegalArgumentException:无法将int字段com.example.demo.model.Customer.customerId设置为java.util.LinkedHashMap] >> 休眠一对多关系中两个Pojo类的数据。我正在尝试保存已定义Collection元素集的持久类的值。

[家长Pojo课堂:-


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课堂:-


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

}

控制器类:-


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

服务等级:-


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

存储库类别:-


import org.springframework.data.jpa.repository.JpaRepository;

import com.example.demo.model.Vendor;

public interface VendorDataSaveRepository extends JpaRepository<Vendor, Integer> {

}

我从邮递员发送的JSON格式:-

    "vendorId" : 101,
    "vendorName" : "JAIN BOOKS",
    "children" : [{
                    "customerId" : 1,
                    "customerName" : "AMIT"
                 }]
}

来自控制台的错误消息:-

java.lang.IllegalArgumentException:无法将int字段com.example.demo.model.Customer.customerId设置为java.util.LinkedHashMap

邮递员错误消息:-

通过持久属性[com.example.demo.model.Customer#customerId]的反射访问字段[int com.example.demo.model.Customer.customerId]时出错:{customerId = 1,customerName = AMIT};嵌套异常是org.hibernate.property.access.spi.PropertyAccessException:通过对持久属性[com.example.demo.model.Customer.customerId]的反射访问字段[int com.example.demo.model.Customer.customerId]时出错:{customerId = 1,customerName = AMIT}“

我正在获取java.lang.IllegalArgumentException:无法在我试图持久保存的我的Spring Boot项目中将com.example.demo.model.Customer.customerId的int字段设置为java.util.LinkedHashMap。 >

java spring-boot spring-data-jpa hibernate-mapping hibernate-onetomany
2个回答
0
投票

您能否从VendorDataSaveRepository接口实现中发布保存方法?


0
投票

需要在设置子级集合类型中添加通用类型客户

© www.soinside.com 2019 - 2024. All rights reserved.