자바 스프링 애플리케이션에서 나는 최대 절전 모드와 jpa로 작업 중이며, jackson을 사용하여 DB에 데이터를 채 웁니다. 여기 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"}]
Unfortu
이.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"}
에서오고 내가 위의 줄을 제거 할 때 응용 프로그램이 잘 작동 : 내가 응용 프로그램을 실행할 때와하게도, 그것은 뿌려줍니다.
나는 사용자의 인스턴스를 만들 수 없기 때문에 불평한다. 그래서 어떻게 해결할 수 있습니까?
문제가 무엇인지 바로 알 수 있습니다. 유형 1 인 필드에 정수 1을 매핑 할 수 없습니다. 입력에 외래 키 참조 대신 사용자 객체가 있어야합니다. 또는 내가 선호하는 것을 수행하고 관심있는 필드가 들어있는 DTO를 작성한 다음 엔티티를 작동시키는 데 필요한 비즈니스 로직을 적용하십시오. –
나는 u를 얻지 않았다. 대답을 써주시겠습니까? –
비즈니스 관점에서 볼 때 일대일로 맞습니까? –