在我的 java spring 应用程序中,我正在使用 hibernate 和 jpa,并使用 jackson 来填充数据库中的数据。
这是用户类:
@Data
@Entity
public class User{
@Id
@GeneratedValue
Long id;
String username;
String password;
boolean activated;
public User(){}
}
第二类是:
@Entity
@Data
public class Roles {
@Id
@GeneratedValue
Long id;
@OneToOne
User user;
String role;
public Roles(){}
}
在角色类中,我有一个用户属性然后我制作了一个json文件来存储数据:
[ {"_class" : "com.example.domains.User", "id": 1, "username": "Admin", "password": "123Admin123","activated":true}
,
{"_class" : "com.example.domains.Roles", "id": 1,"user":1, "role": "Admin"}]
不幸的是,当我运行该应用程序时,它抱怨:
.RuntimeException: com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of com.example.domains.User: no int/Int-argument constructor/factory method to deserialize from Number value (1)
at [Source: N/A; line: -1, column: -1] (through reference chain: com.example.domains.Roles["user"])
问题来自于
{"_class" : "com.example.domains.Roles", "id": 1,"user":1, "role": "Admin"}
当我删除上面的行时,应用程序运行良好。
我认为,它抱怨是因为它无法创建用户实例。
所以,我该如何修复它?