'Update Spring Embedded Entity throws Stack Overflow Entity
I am currently trying to update a Record which contains an Embedded Entity. When trying this, i am getting the following Error:
Request processing failed; nested exception is java.lang.RuntimeException: java.lang.StackOverflowError
When i create the Entity, everything is working fine.
This is what i have right now:
My Main Entity:
@Entity(name = "Account")
@Table(name = "account")
@DynamicUpdate
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Account extends Auditable<String> {
@Id
@Column(name = "Id")
private String id;
@Column(
name = "Description",
length = 512)
private String description;
@Column(
name = "AnnualRevenue",
length = 18)
private String annualRevenue;
@Embedded
@AttributeOverrides(value = {
@AttributeOverride(name = "city", column = @Column(name = "billingAddressCity")),
@AttributeOverride(name = "country", column = @Column(name = "billingAddressCountry")),
@AttributeOverride(name = "latitude", column = @Column(name = "billingAddressLatitude")),
@AttributeOverride(name = "longitude", column = @Column(name = "billingAddressLongitude")),
@AttributeOverride(name = "postalCode", column = @Column(name = "billingAddressPostalCode")),
@AttributeOverride(name = "state", column = @Column(name = "billingAddressState")),
@AttributeOverride(name = "street", column = @Column(name = "billingAddressStreet"))
})
private Address billingAddress;
@Column(name = "Industry")
private String industry;
@Column(name = "IsDeleted")
private boolean isDeleted;
@Column(
name = "Employees",
length = 8
)
private int employees;
@Column(name = "Name")
private String name;
@Column(name = "Phone")
private String phone;
@Column(name = "Type")
private String type;
@Column(name = "Website")
private String website;
@OneToMany(
orphanRemoval = true,
fetch = FetchType.LAZY,
cascade = {
CascadeType.ALL
}
)
@JsonIgnoreProperties("account")
private final List<Contact> contacts = new ArrayList<>();
}
Here, i am using an Embedded Entity for the Address which looks like this:
@Embeddable
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class Address {
private String city;
private String country;
private Double latitude;
private Double longitude;
private String postalCode;
private String state;
private String street;
}
Furthermore, the Account can have multiple Contacts which is is defined like this:
@Entity(name = "Contact")
@Table(name = "contact")
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@JsonInclude
public class Contact extends Auditable<String> {
@Id
@Column(name = "Id")
private String id;
@Column(name = "Department")
private String department;
@Column(
name = "Email"
)
private String email;
@Column(name = "FirstName")
private String firstName;
@Column(name = "Name")
private String name;
@Column(name = "Salutation")
private String salutation;
@ManyToOne
@JoinColumn(
name = "accountId",
nullable = false,
referencedColumnName = "Id",
foreignKey = @ForeignKey(
name = "AccountContactFK"
)
)
@JsonIgnoreProperties("contacts")
private Account account;
}
For the Account, i am using the DTO Principle to map the Fields which looks like this:
DTO for Creation:
@Getter
@Setter
public class AccountCreationDTO {
private String description;
private String annualRevenue;
private Address billingAddress;
private String industry;
private boolean isDeleted;
private int employees;
private String name;
private String phone;
private String type;
private String website;
}
DTO for Update:
@Getter
@Setter
public class AccountUpdateDTO {
@Id
private String id;
private String description;
private String annualRevenue;
@Embedded
private Address billingAddress;
private String industry;
private boolean isDeleted;
private int employees;
private String name;
private String phone;
private String type;
private String website;
}
And here is my Controller:
@RestController
@RequestMapping("/api/v1")
@RequiredArgsConstructor
@Slf4j
public class AccountController {
private final AccountRepository accountRepository;
@RequestMapping(value = "/createAccount", method = RequestMethod.POST)
public Account createObject(@RequestBody @DTO(AccountCreationDTO.class) Account account) {
//account.setBillingAddress(account.getBillingAddress());
//this.addressRepository.save(account.getBillingAddress());
Account createdAccount = this.accountRepository.saveAndFlush(account);
log.info("Created Account: {}", createdAccount);
return createdAccount;
}
@PutMapping(value = "/updateAcc")
public ResponseEntity<Account> updateAccount(@DTO(AccountUpdateDTO.class) Account account) {
Account updatedAccount = this.accountRepository.saveAndFlush(account);
log.info("Updated Account: {}", updatedAccount);
return ResponseEntity.ok().body(updatedAccount);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
