欢迎您, 我正在使用SPRING MVC,SPRING DATA,HIBERNATE,Maven
与标题相同。当我添加第一个产品时,一切正常,但是当我想向客户添加下一个产品时,就会出现问题。
我的实体
@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="customer_id")
private int id;
@Column(name="name")
private String name;
@Column(name="email")
private String email;
@Column(name="address")
private String address;
@OneToOne(cascade=CascadeType.ALL)
private CustomerDetails customerDetails;
@ManyToMany(mappedBy="customers",cascade=CascadeType.ALL, fetch=FetchType.EAGER)
private List<Products>products;
public Customer() {
}
public Customer(CustomerDetails customerDetails)
{
this.customerDetails=customerDetails;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public CustomerDetails getCustomerDetails() {
return customerDetails;
}
public void setCustomerDetails(CustomerDetails customerDetails) {
this.customerDetails = customerDetails;
}
public void addCustomerDetails(CustomerDetails cd)
{
cd.setCustomer(this);
}
@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", address=" + address
+ ", customerDetails=" + customerDetails + "]";
}
//new added
public List<Products> getProducts() {
return products;
}
public void setProducts(List<Products> products) {
this.products = products;
}
public void addProduct(Products product)
{
if(products==null)
{
products=new ArrayList<>();
}
products.add(product);
product.addCustomer(this);
}
}
@Entity
@Table(name="Produkty")
public class Products {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column
private int id;
@Column
private String name;
@Column
private String type;
@Column
private String expiryDate;
@ManyToMany(cascade= {CascadeType.DETACH,CascadeType.MERGE,CascadeType.PERSIST,CascadeType.REFRESH}, fetch=FetchType.EAGER)
private List<Customer> customers;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getExpiryDate() {
return expiryDate;
}
public void setExpiryDate(String expiryDate) {
this.expiryDate = expiryDate;
}
public List<Customer> getCustomers() {
return customers;
}
public void setCustomers(List<Customer> customers) {
this.customers = customers;
}
@Override
public String toString() {
return "Products [id=" + id + ", name=" + name + ", type=" + type + ", expiryDate=" + expiryDate
+ ", customers=" + customers + "]";
}
public void addCustomer(Customer customer)
{
if(customers==null)
{
customers=new ArrayList<>();
}
customers.add(customer);
}
}
MyControllers:
@Controller
@RequestMapping("/customer")
public class CustomerController {
@Autowired
private CustomerService service;
@GetMapping("/home")
public String home(Model model)
{
List<Customer>customers=service.getCustomers();
model.addAttribute("message","Hello from Spring MVC");model.addAttribute("customers",customers);
return "home-page";
}
@GetMapping("/showForm")
public String showForm(Map<String,Object>model)
{
Customer customer=new Customer();
model.put("customer",customer);
return "new-customer";
}
@PostMapping("/add")
public String addCustomer(@ModelAttribute("customer") Customer customer)
{
CustomerDetails cd=new CustomerDetails();
customer.setCustomerDetails(cd);
cd.setCustomer(customer);
service.saveCustomer(customer);
return "redirect:/customer/home";
}
/*
@RequestMapping("/showCustomerDetailsForm")
public String showCustomerDetailsForm(@RequestParam int id ,Model model)
{
Customer customer=service.getCustomer(id);
model.addAttribute("customer",customer);
return "details-form";
}
@RequestMapping("/addDetails")
public String addCustomerDetails(@ModelAttribute("customerDetails") CustomerDetails customerDt)
{
serviceCD.saveCustomer(customerDt);
return "redirect:/customer/home";
}
*/
@GetMapping("/edit")
public String editCustomer(@RequestParam int id, Model model)
{
Customer customer=service.getCustomer(id);
model.addAttribute("customer",customer);
return "edit-customer";
}
@GetMapping("/delete")
public String deleteCustomer(@RequestParam int id)
{
service.deleteCustomer(id);
return "redirect:/customer/home";
}
@GetMapping("/search")
public String search(@RequestParam String keyword,Model model)
{
List<Customer>customers=service.search(keyword);
model.addAttribute("customers",customers);
return "search-page";
}
}
@Controller
@RequestMapping("/customer/product")
public class ProductController {
@Autowired
private CustomerService service;
@RequestMapping("/showForm")
public String showProductsForm(@RequestParam int id,Model model)
{
Customer customer=service.getCustomer(id);
Products product=new Products();
model.addAttribute("customer",customer);
model.addAttribute("product",product);
return "product-form";
}
@RequestMapping("/add")
public String addProductToCustomer(@RequestParam int id, @ModelAttribute("product")Products product)
{
Customer customer=service.getCustomer(id);
customer.addProduct(product);
service.saveCustomer(customer);
return "redirect:/customer/home";
}
}
我的产品形式JSP的主体:
<body>
<div align="center">
<table>
<form:form action="add?id=${customer.id}" modelAttribute="product">
<tr>
<td>${customer.id}</td>
</tr>
<tr>
<td>Name:</td>
<td><form:input path="name"/>
</td>
</tr>
<tr>
<td>Type:</td>
<td><form:input path="type"/>
</td>
</tr>
<tr>
<td>Expiry date:</td>
<td><form:input path="expiryDate"/>
</td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</form:form>
</table>
</div>
</body>
我的home-page.jsp正文
<div align="center">
<h1>Customer Manager</h1>
<form method="get" action="search">
<input type="text" name="keyword" />
<input type="submit" value="Search" />
</form>
<h3> <a href="showForm">new customer</a></h3>
<table border="1" padding="5">
<tr>
<th>ID</th>
<th>Name</th>
<th>E-mail</th>
<th>Address</th>
<th>Action</th>
</tr>
<c:forEach items="${customers}" var="customer">
<tr>
<td>${customer.id}</td>
<td>${customer.name}</td>
<td>${customer.email}</td>
<td>${customer.address}</td>
<td><a href ="edit?id=${customer.id}">Edit</a> <a href ="delete?id=${customer.id}">Delete</a></td>
<td> <a href="product/showForm?id=${customer.id}" >Add product</a>
</tr>
</c:forEach>
</table>
Stacktrace:
gru 16, 2019 10:22:47 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [/spring_hib_config_web] threw exception [Handler dispatch failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at java.lang.StringBuilder.<init>(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.Customer.toString(Customer.java:104)
at java.lang.String.valueOf(Unknown Source)
at java.lang.StringBuilder.append(Unknown Source)
at com.firstapp.entity.CustomerDetails.toString(CustomerDetails.java:78)
有人知道吗?我会很感激。谢谢
答案 0 :(得分:1)
您可以在StackTrace中看到,to Customer和CustomerDetails的toString方法导致StackoverflowError。
您必须注意双向引用,并确保在一侧调用toString不会导致循环调用。