I have spring boot hibernate TestRestTemplate project, and running "mvn test" with it. I have two database tables: Customer and ShippingAddress:
public class Customer implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
private Integer id;
@Basic(optional = false)
@Column(name = "first_name")
private String firstName;
@Basic(optional = false)
@Column(name = "last_name")
private String lastName;
@Basic(optional = false)
private String phone;
@Basic(optional = false)
@Column(name = "email_address")
private String emailAddress;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<Product> productCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<PurchaseOrder> purchaseOrderCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer")
private Collection<Review> reviewCollection;
@OneToMany(cascade = CascadeType.ALL, mappedBy = "customer", orphanRemoval = true)
private List<ShippingAddress> shippingAddressCollection = new ArrayList<>();
And the ShippingAddress table:
public class ShippingAddress implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@Column(name = "id")
private Integer id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "address1")
private String address1;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "address2")
private String address2;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "city")
private String city;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "state")
private String state;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 30)
@Column(name = "postcode")
private String postcode;
@JoinColumn(name = "cust_id", referencedColumnName = "id") // id is the primary key in Customer table
@ManyToOne(optional = false)
private Customer customer;
ShippingAddressControllerRestTemplateTest Controller:
@Test
public void create_shipping_address_and_customer() throws Exception {
Customer newCustomer = new Customer("wendy", "hades", "myname@shipping.com", "04000000");
ShippingAddress shippingAddress1 = new ShippingAddress("unit 1", "89 hanson road", "New York", "CA", "KKKDLDL");
shippingAddress1.setCustomer(newCustomer);
// ShippingAddress shippingAddress2 = new ShippingAddress("", "111 green st", "MA", "CA", "AAAAA");
// shippingAddress2.setCustomer(newCustomer);
ArrayList<ShippingAddress> shippingAddressList = new ArrayList<>();
shippingAddressList.add(shippingAddress1);
// shippingAddressList.add(shippingAddress2);
newCustomer.setShippingAddressCollection(shippingAddressList);
System.out.println("*****CustomerControllerRestTemplateTest***in save_save_OK() **********");
HttpEntity<Customer> requestBody = new HttpEntity<>(newCustomer);
ResponseEntity<String> response = restTemplate.postForEntity("/addcustomer", requestBody, String.class);
assertEquals(HttpStatus.CREATED, response.getStatusCode());
}
Exceptions from running "mvn test":
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.069 s <<< FAILURE! - in dorinca_rest.controller.ShippingAddressControllerRestTemplateTest
[ERROR] create_shippi`enter code here`ng_address_and_customer(dorinca_rest.controller.ShippingAddressControllerRestTemplateTest) Time elapsed: 0.057 s <<< ERROR!
org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON:`enter code here` Infinite recursion (StackOverflowError);
The StackOverflowError error seems to be my two tables are referencing to each other, but it isn't the case after I take a closer look at it. And I am not sure the way I am using restTemplate.PostForEntity() is correct. I basically posting a list / collection for the PostMapping. The Customer table and the ShippingAddress table has only one way referencing, that is one to many relationship.