【问题标题】:Getting InvalidDefinitionException exception in Spring boot在 Spring Boot 中获取 InvalidDefinitionException 异常
【发布时间】:2022-01-25 07:50:56
【问题描述】:

我正在使用 Spring boot 和 mysql 作为数据库制作一个小的 Rest API。我收到“未找到序列化程序”错误,获取类型为惰性。我的应用程序在 fetch type eager 上运行良好,但我希望 fetch type 变得惰性,所以我该如何解决这个问题。

用户模型:

@Entity
@Table(name = "users")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable = false,length = 50)
private String firstName;
@Column(nullable = false,length = 50)
private String lastName;
@Column(nullable = false,unique = true)
private String email;
@OneToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JoinColumn(name = "address_id",referencedColumnName = "id")
@JsonIgnoreProperties("user")
private Address address;
}

地址模型:

@Entity
@Table(name = "addresses")
public class Address {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Column(nullable = false,length = 255)
private String street;
@Column(nullable = false)
private int postalCode;
@Column(nullable = false,length = 100)
private String city;
@OneToOne(mappedBy = "address",fetch = FetchType.LAZY,cascade = CascadeType.ALL)
@JsonIgnoreProperties("address")
private User user;
}

用户服务:

@Service
public class UserService {

private final UserRepository userRepository;

@Autowired
public UserService(UserRepository userRepository) {
    this.userRepository = userRepository;
}

public User save(User user){
    return userRepository.save(user);
}

public List<User> find(){
    return userRepository.findAll();
}

public User find(Integer id){
    return userRepository.findById(id).get();
}

public void delete(Integer id){
    userRepository.deleteById(id);
}
}

用户控制器:

@RestController
@RequestMapping(path = "users")
public class UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
    this.userService = userService;
}

@GetMapping
public List<User> get(){
    return userService.find();
}

@GetMapping(path = "{id}")
public User get(@PathVariable Integer id){
    return userService.find(id);
}

@PostMapping
public User post(@RequestBody User user){
    return userService.save(user);
}

@DeleteMapping(path = "{id}")
public boolean delete(@PathVariable Integer id){
    userService.delete(id);
    return true;
}
}

堆栈跟踪:

com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.hibernate.proxy.pojo.bytebuddy.ByteBuddyInterceptor and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain: java.util.ArrayList[0]->com.kumasroh.usersapp.models.User["address"]->com.kumasroh.usersapp.models.Address$HibernateProxy$VM2Pif4w["hibernateLazyInitializer"])
at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77) ~[jackson-databind-2.13.1.jar:2.13.1]
at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300) ~[jackson-databind-2.13.1.jar:2.13.1]
at com.fasterxml.jackson.databind.DatabindContext.reportBadDefinition(DatabindContext.java:400) ~[jackson-databind-2.13.1.jar:2.13.1]

【问题讨论】:

  • 你可以看到这个链接:stackoverflow.com/questions/2990799/…
  • 我知道 fetch 是如何工作的,但是当我将 fetch 类型设置为惰性时出现错误。你能帮我解决这个问题吗?
  • 我认为 fetchType Lazy 用于 OneToMany 关系,而 fetchType Eager 用于 OneToOne 关系。

标签: java spring spring-boot hibernate spring-data-jpa


【解决方案1】:

我认为 fetchType Lazy 用于 OneToMany 关系,而 fetchType Eager 用于 OneToOne 关系。

【讨论】:

  • 我得到了答案。非常感谢你
  • 我还有一个问题。你能告诉我为什么使用@Transactional注解吗?
猜你喜欢
  • 2019-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-02
  • 2019-04-09
  • 2020-02-05
  • 2019-01-06
相关资源
最近更新 更多