无法使用带参数的构造函数 NO_CONSTRUCTOR 实例化 java.util.List
Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments
我的 MongoDB 数据是这样的:
{
"college": [
{
"batch": {
"subject": [
{
"name": "DBA"
}
],
"name": "BBA",
"year": "2016"
}
},
{
"batch": [
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
},
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
},
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
}
]
}
]
}
我正在尝试使用 spring 数据 MongoDB 从 MongoDB 获取所有数据。我为 "College" 和 "Batch" 创建了实体 类。似乎 Batch as Object 在一个数组中形成,所以它 returns "Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments"。无论如何要解决这个问题? TIA
我的实体Class:
@Document(collection = "college")
public class College {
private List<Batch> batch;
public List<Batch> getBatch() {
return batch;
}
public void setBatch(List<Batch> batch) {
this.batch = batch;
}
}
并使用 mongoRepository 获取数据
for (College college: collegeRepository.findAll())
根据您在数据库中的数据,下面是您可以创建的类
public class College {
List<Batch> batches;
String name;
}
public class Batch {
List<Subject> subjects;
String name;
String year;
}
public class Subject {
String name;
}
上面的类可以read/write值到mongoDB下面的格式
{
"name": "My College",
"batches": [
{
"name": "BCA",
"year": "2016",
"subjects": [
{
"name": "ECO"
},
{
"name": "EBC"
},
{
"name": "DFG"
}
]
},
{
"name": "MBA",
"year": "2018",
"subjects": [
{
"name": "ABC"
},
{
"name": "DEF"
},
{
"name": "GHI"
}
]
}
]
}
我的 MongoDB 数据是这样的:
{
"college": [
{
"batch": {
"subject": [
{
"name": "DBA"
}
],
"name": "BBA",
"year": "2016"
}
},
{
"batch": [
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
},
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
},
{
"subject": [
{
"name": "ECO"
}
],
"name": "BCA",
"year": "2016"
}
]
}
]
}
我正在尝试使用 spring 数据 MongoDB 从 MongoDB 获取所有数据。我为 "College" 和 "Batch" 创建了实体 类。似乎 Batch as Object 在一个数组中形成,所以它 returns "Failed to instantiate java.util.List using constructor NO_CONSTRUCTOR with arguments"。无论如何要解决这个问题? TIA
我的实体Class:
@Document(collection = "college")
public class College {
private List<Batch> batch;
public List<Batch> getBatch() {
return batch;
}
public void setBatch(List<Batch> batch) {
this.batch = batch;
}
}
并使用 mongoRepository 获取数据
for (College college: collegeRepository.findAll())
根据您在数据库中的数据,下面是您可以创建的类
public class College {
List<Batch> batches;
String name;
}
public class Batch {
List<Subject> subjects;
String name;
String year;
}
public class Subject {
String name;
}
上面的类可以read/write值到mongoDB下面的格式
{
"name": "My College",
"batches": [
{
"name": "BCA",
"year": "2016",
"subjects": [
{
"name": "ECO"
},
{
"name": "EBC"
},
{
"name": "DFG"
}
]
},
{
"name": "MBA",
"year": "2018",
"subjects": [
{
"name": "ABC"
},
{
"name": "DEF"
},
{
"name": "GHI"
}
]
}
]
}