无法写入JSON:当从delete方法返回实体对象时,惰性初始化集合失败



当我在练习实现@OneToMany双向关联时,我在为User实体实现删除操作时卡住了,我收到这个警告-Could not write JSON: failed to lazily initialize a collection, could not initialize proxy - no Session

所以,我有两个实体如下:

用户实体

@Entity
@Table(name = "users")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id ;
private String username ;
private String password ;
private String email ;
private String firstname ;
private String lastname ;
@OneToMany(
mappedBy = "user" ,
cascade = CascadeType.ALL ,
orphanRemoval = true
)
private List<Playlist> playlists = new ArrayList<>() ;
// getters and setters
}

播放列表实体

@Entity
@Table(name = "playlist")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class,property = "id")
public class Playlist {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
private String description;
@ManyToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "user_id")
private User user;
// getters and setters
}

UserController Class

@RestController
@RequestMapping("/api/users")
public class userController {
@Autowired
UserService userService ;
// ... other controller mappings
@PostMapping("")
public User createUser(@RequestBody User reqUser){
return userService.createUser(reqUser) ;
}
@DeleteMapping("/{userID}")
public User removeUser (@PathVariable Long userID) {
return userService.removeUser(userID) ;
}
}

UserService类中的removeUser方法

@Service
public class UserService {
@Autowired
UserRepo userRepo ;
public List<User> getAllUsers() {
List <User> users = new ArrayList<>() ;
userRepo.findAll().forEach(users::add);
return users ;
}
public User createUser(User reqUser) {
User newUser = new User() ;
newUser.setUsername(reqUser.getUsername());
newUser.setEmail(reqUser.getEmail());
newUser.setPassword(reqUser.getPassword());
newUser.setFirstname(reqUser.getFirstname());
newUser.setLastname(reqUser.getLastname());
// adding playlists
reqUser.getPlaylists().forEach( playlist -> {
newUser.addPlaylist(playlist);
});
userRepo.save(newUser) ;
return newUser ;
}

public User removeUser(Long userID) {
// check if user exists
User user = userRepo.findById(userID).orElseThrow(
() -> new EntityNotFoundException("User doesn't exists with userID " + userID)
);
userRepo.deleteById(userID) ;
return user ;
}
}

用户的Get请求获取我这个响应:
{
"id": 20,
"username": "poodie",
"password": "randomPasswrod",
"email": null,
"firstname": "Raj",
"lastname": "Bhushan",
"playlists": [
{
"id": 27,
"name": "playlist-01",
"description": "first playlist",
"user": 20,
"songs": []
},
{
"id": 28,
"name": "playlist-02",
"description": "first playlist",
"user": 20,
"songs": []
}
]
}

不幸的是,当我想删除这个用户时,即使它被删除了但我在POSTMAN
中得到这个错误
{
"timestamp": "2023-04-02T14:39:45.433+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/api/users/20"
}

下面你可以看到在堆栈跟踪中收到的警告:

2023-04-02 20:09:45.418  WARN 26500 --- [nio-8080-exec-2] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter
.HttpMessageNotWritableException: Could not write JSON: failed to lazily initialize a collection, could not initialize proxy - no Session; nested exception is com.fasterxml
.jackson.databind.JsonMappingException: failed to lazily initialize a collection, could not initialize proxy - no Session (through reference chain: com.rudra.musicStreaming
.models.User["playlists"]->org.hibernate.collection.internal.PersistentBag[0]->com.rudra.musicStreaming.models.Playlist["songs"])]

虽然这个用户被从DB中删除了,但是在堆栈跟踪中留下了这个警告。我怎样才能摆脱这个问题(Could not write JSON: failed to lazily initialize a collection, could not initialize proxy - no Session)和这个问题的原因是什么?

注意:当我修改delete方法以不返回任何User对象时,一切工作正常,但是当我在执行删除操作(如上面共享的代码所示)时尝试返回User实体的对象时,此警告发生在堆栈跟踪中。

LazyInitializationException:

尝试访问打开状态会话上下文之外未获取的数据。例如,当会话关闭后访问未初始化的代理或集合时,会发生此异常。

出现是因为您使用了存储库返回的代理对象。当您访问该对象时,代理正在尝试初始化与该对象关联的集合。但是初始化集合失败,因为该对象被删除后不再在会话中。

相关内容

  • 没有找到相关文章

最新更新