立即更新父母和子女记录
我目前正在尝试更新父记录,并同时在春季同时进行记录。
我的父实体看起来像这样:
@Entity(name = "User")
@Table(name = "user")
public class User {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "UserSequence")
@GenericGenerator(
name = "UserSequence",
strategy = "com.simagdo.test.utils.SequenceIdGenerator",
parameters = {
@Parameter(name = SequenceIdGenerator.INCREMENT_PARAM, value = "1"),
@Parameter(name = SequenceIdGenerator.VALUE_PREFIX_PARAMETER, value = "100"),
@Parameter(name = SequenceIdGenerator.NUMBER_FORMAT_PARAMETER, value = "%05d")
}
)
@Column(name = "Id")
private String id;
@OneToOne(
orphanRemoval = true,
cascade = {
CascadeType.MERGE,
CascadeType.REFRESH
}
)
private Address userAddress;
地址实体看起来像这样(并非所有列):
@Entity(name = "Address")
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "AddressSequence")
@GenericGenerator(
name = "AddressSequence",
strategy = "com.simagdo.test.utils.SequenceIdGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.INCREMENT_PARAM, value = "1"),
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.VALUE_PREFIX_PARAMETER, value = "120"),
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.NUMBER_FORMAT_PARAMETER, value = "%05d")
}
)
@Column(name = "Id")
private String id;
@Column(
name = "City",
length = 40
)
private String city;
@Column(
name = "Country",
length = 80
)
我想同时更新用户实体以及用户的地址。为此,我正在使用以下代码:
@PutMapping(value = "/updateUser/{id}")
public ResponseEntity<User> updateRecord(@PathVariable(value = "id") String id, @RequestBody User updateUser) {
User user = this.userRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("User not found: " + id));
user.setAnnualRevenue(updateUser.getAnnualRevenue());
user.setBillingAddress(updateUser.getBillingAddress());
user.setDescription(updateUser.getDescription());
user.setEmployees(updateUser.getEmployees());
user.setDeleted(updateUser.isDeleted());
user.setName(updateUser.getName());
user.setPhone(updateUser.getPhone());
user.setShippingAddress(updateUser.getShippingAddress());
user.setType(updateUser.getType());
user.setWebsite(updateUser.getWebsite());
this.userRepository.saveAndFlush(user);
return ResponseEntity.ok().body(user);
}
但是当我发送PUT请求时,我会收到以下错误:
请求处理失败;嵌套异常是 java.lang.runtimeException:java.lang.stackoverflowerror
I'm currently trying to update a Parent Record and also a Child Record at the same time in Spring.
My Parent Entity looks like this:
@Entity(name = "User")
@Table(name = "user")
public class User {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "UserSequence")
@GenericGenerator(
name = "UserSequence",
strategy = "com.simagdo.test.utils.SequenceIdGenerator",
parameters = {
@Parameter(name = SequenceIdGenerator.INCREMENT_PARAM, value = "1"),
@Parameter(name = SequenceIdGenerator.VALUE_PREFIX_PARAMETER, value = "100"),
@Parameter(name = SequenceIdGenerator.NUMBER_FORMAT_PARAMETER, value = "%05d")
}
)
@Column(name = "Id")
private String id;
@OneToOne(
orphanRemoval = true,
cascade = {
CascadeType.MERGE,
CascadeType.REFRESH
}
)
private Address userAddress;
The Address Entity looks like this (Not all Columns are included):
@Entity(name = "Address")
@Table(name = "address")
public class Address {
@Id
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "AddressSequence")
@GenericGenerator(
name = "AddressSequence",
strategy = "com.simagdo.test.utils.SequenceIdGenerator",
parameters = {
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.INCREMENT_PARAM, value = "1"),
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.VALUE_PREFIX_PARAMETER, value = "120"),
@org.hibernate.annotations.Parameter(name = SequenceIdGenerator.NUMBER_FORMAT_PARAMETER, value = "%05d")
}
)
@Column(name = "Id")
private String id;
@Column(
name = "City",
length = 40
)
private String city;
@Column(
name = "Country",
length = 80
)
I want to update the User Entity and also the Address of the User at the same time. For that, I'm using the following code:
@PutMapping(value = "/updateUser/{id}")
public ResponseEntity<User> updateRecord(@PathVariable(value = "id") String id, @RequestBody User updateUser) {
User user = this.userRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("User not found: " + id));
user.setAnnualRevenue(updateUser.getAnnualRevenue());
user.setBillingAddress(updateUser.getBillingAddress());
user.setDescription(updateUser.getDescription());
user.setEmployees(updateUser.getEmployees());
user.setDeleted(updateUser.isDeleted());
user.setName(updateUser.getName());
user.setPhone(updateUser.getPhone());
user.setShippingAddress(updateUser.getShippingAddress());
user.setType(updateUser.getType());
user.setWebsite(updateUser.getWebsite());
this.userRepository.saveAndFlush(user);
return ResponseEntity.ok().body(user);
}
But when I send a PUT Request, I get the following Error:
Request processing failed; nested exception is
java.lang.RuntimeException: java.lang.StackOverflowError
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可能正在返回引用地址的用户对象,并且同一地址也在引用用户类。这就是为什么您会遇到堆栈溢出错误。我不能保证这一点,因为您没有揭露整个课程。如果这样做,请检查解决方案。
这是一个常见的错误,不用担心。
解决方案:
1-在用户类中,在地址字段上添加JSONMANAGGEREFERS注释。
2-在地址类中,在用户字段上添加JSONBACKREFERY注释。
现在,当您的应用程序将您的用户实例转换为JSON时,它将添加带有JSONMANAGGEREFERDER注释的字段,并且不会忽略字段JSONBACKREFERY和堆栈溢出。
You are likely returning a User object that is referencing an Address and this same address is also referencing the user class. That's why you are getting a stack overflow error. I can't assure this because you didn't expose the whole classes. If you did this, checkout the solution.
This is a common error, don't worry about.
SOLUTION:
1- On the users class, add the JsonManagedReference annotation on the Address field.
2- In the addresses class, add the JsonBackReference annotation on the User field.
Now, when your application convert your User instance to Json, it will add the field annotated with JsonManagedReference and will ignore the field JsonBackReference and stack overflow will not be thrown.