首页 > 解决方案 > 如何保存列表的jpa列表

问题描述

我想在我的春季项目中保存列表数据列表

我的项目前面是vue

像这样的前端发送数据结构

 data=[[{}],[{},{}],[{},{}]]

这是我的数据

"option1": [
    {
        "firstOption": "s",
        "quantity": 1,
     
        "option2": [
            [
                {
                    "optionsDetail": "ss",
                    "leaseType": "1",
                    "needType": 10,
                
                    "monthlyPayment": 4412,
                    "total": 158832,
                    "prePayment": "0",
                  
                    "state": "-",
                    "used": true
                },
                {
                    "optionsDetail": "ss",
                    "leaseType": "1",
                    "needType": 11,
                   
                    "monthlyPayment": 4412,
                    "total": 158832,
                    "prePayment": "0",
               
                    "state": "-",
                    "used": true
                }
            ],
            [
                {
                    "optionsDetail": "dd",
                    "leaseType": "1",
                    "needType": 10,
                   
                    "monthlyPayment": 88,
                    "total": 3168,
                    "prePayment": "0",
                    
                    "state": "-",
                    "used": true
                },
                {
                    "optionsDetail": "dd",
                    "leaseType": "1",
                    "needType": 11,
                 
                    "monthlyPayment": 88,
                    "total": 3168,
                    "prePayment": "0",
                 
                    "state": "-",
                    "used": true
                }
            ],
            [
                {
                    "optionsDetail": "f",
                    "leaseType": "1",
                    "needType": 10,
                    "monthlyPayment": 4412,
                    "total": 158832,
                    "prePayment": "0",
                    "state": "-",
                    "used": true
                },
                {
                    "optionsDetail": "f",
                    "leaseType": "1",
                    "needType": 11,
                   
                    "monthlyPayment": 4412,
                    "total": 158832,
                    "prePayment": "0",
                    "state": "-",
                    "used": true
                }
            ]
        ],
        "itemIdx": 165
    },
    {
        "firstOption": "aa",
        "quantity": 1,

        "option2": [
            [
                {
                    "optionsDetail": "sdf",
                    "leaseType": "1",
                    "needType": 10,
                    
                    "monthlyPayment": 1000000,
                    "total": 1000000,
                    "prePayment": "0",
                    "state": "-",
                    "used": true
                },
                {
                    "optionsDetail": "sdf",
                    "leaseType": "1",
                    "needType": 11,
                   
                    "monthlyPayment": 1000000,
                    "total": 1000000,
                    "prePayment": "0",
                    "state": "-",
                    "used": true
                }
            ],
            [
                {
                    "optionsDetail": "ee",
                    "leaseType": "1",
                    "needType": 10,
                    
                    "monthlyPayment": 25885,
                    "total": 931860,
                    "prePayment": "0",
                    "state": "-",
                   "used": true    
                },
                {
                    "optionsDetail": "ee",
                    "leaseType": "1",
                    "needType": 11,
                  
                    "monthlyPayment": 25885,
                    "total": 931860,
                    "prePayment": "0",
                    "state": "-",
                    "used": true    
                }
            ]
        ],
        "itemIdx": 108
    }

我想使用弹簧数据 jpa 保存

一种方法是

@JsonIgnoreProperties(ignoreUnknown = true)
@OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "opt_idx" )
private List<RentTypeList> option2 = new ArrayList<>();

其他 @ElementCollection(fetch=FetchType.EAGER) 私有列表 option2 = new ArrayList<>();

我使用两种方式但是会发生错误

JSON parse error: Cannot deserialize instance of `EntityList` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: 
    Cannot deserialize instance of `option2` out of START_ARRAY token
    at [Source: (PushbackInputStream); line: 9, column: 13] (through reference chain:
    EntityList["option1"]->java.util.ArrayList[0]->option2["option2"]->java.util.ArrayList[0])]

我该如何改变?

标签: arraylistspring-data-jpaentity

解决方案


推荐阅读