Привет всем! Такая ситуация:
Есть связь ManyToMany(в базе это 2 таблицы сущности и одна связующая).
При обновлении записи Product, удаляеться запись со сводной/связующей таблицы. (А не должна!) Hibernate формирует запрос на удаление... Почему? При обновлении Order, все норм - "сводная" запись не пропадает..
Вот код и запросы Hibernate. Огромное спасибо наперед, может кто тыкнет носом?)))
Код контроллера и двух сущностей: package com.blackside.group.entity;
import javax.persistence.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "`ORDER`")
public class Order {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "`ID`", nullable = false)
private Integer id;
@Column(name = "`START_ADRESS`")
private String startAdress;
@Column(name = "`END_ADRESS`")
private String endAdress;
@ManyToMany(mappedBy = "orders")
private Set<Product> products;
public Order() {
}
public Integer getId() {
return this.id;
}
public String getStartAdress() {
return this.startAdress;
}
public String getEndAdress() {
return this.endAdress;
}
public Set<Product> getProducts() {
return this.products;
}
public void setId(Integer id) {
this.id = id;
}
public void setStartAdress(String startAdress) {
this.startAdress = startAdress;
}
public void setEndAdress(String endAdress) {
this.endAdress = endAdress;
}
public void setProducts(Set<Product> products) {
this.products = products;
}
@Override
public String toString() {
return "Order{" +
"id=" + id +
", startAdress='" + startAdress + '\'' +
", endAdress='" + endAdress + '\'' +
", products=" + products +
'}';
}
}
package com.blackside.group.entity;
import javax.persistence.*;
import java.util.HashSet;
import java.util.Set;
@Entity
@Table(name = "`PRODUCT`")
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "`ID`", nullable = false)
private Integer id;
@Column(name = "`NAME`")
private String name;
@Column(name = "`DESCRIPTION`")
private String description;
@Column(name = "`PRICE`")
private int price;
@ManyToMany(
cascade = CascadeType.ALL,
fetch = FetchType.LAZY
)
@JoinTable(name = "`PRODUCT_ORDER`",
joinColumns = {
@JoinColumn(name = "`PRODUCT_ID`")
},
inverseJoinColumns = {
@JoinColumn(name = "`ORDER_ID`")
}
)
private Set<Order> orders ;
public Product() {
}
public Integer getId() {
return this.id;
}
public String getName() {
return this.name;
}
public String getDescription() {
return this.description;
}
public int getPrice() {
return this.price;
}
public Set<Order> getOrders() {
return this.orders;
}
public void setId(Integer id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public void setDescription(String description) {
this.description = description;
}
public void setPrice(int price) {
this.price = price;
}
public void setOrders(Set<Order> orders) {
this.orders = orders;
}
@Override
public String toString() {
return "Product{" +
"id=" + id +
", name='" + name + '\'' +
", description='" + description + '\'' +
", price=" + price +
", orders=" + orders +
'}';
}
}
package com.blackside.group.controller;
import com.blackside.group.entity.Order;
import com.blackside.group.entity.Product;
import com.blackside.group.service.OrderService;
import com.blackside.group.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List;
@Controller
public class ProductController {
@Autowired
@Qualifier("productService")
private ProductService productService;
@RequestMapping(value = "/product", method = RequestMethod.GET)
public String getListProduct(ModelMap model) {
List<Product> productList = productService.getAll();
model.addAttribute("productList", productList);
return "product";
}
@RequestMapping(value = "/product/new", method = RequestMethod.GET)
public String addProduct(ModelMap model) {
model.addAttribute("action", "Add new");
Product product = new Product();
model.addAttribute("product", product);
return "productCreate";
}
@RequestMapping(value = { "/product/new" }, method = RequestMethod.POST)
public String saveProduct(Product product) {
productService.add(product);
return "redirect:/product";
}
@RequestMapping(value = { "/product/delete/{idProduct}" }, method = RequestMethod.GET)
public String deleteProduct(@PathVariable int idProduct) {
productService.delete(idProduct);
return "redirect:/product";
}
@RequestMapping(value = { "/product/edit/{idProduct}" }, method = RequestMethod.GET)
public String editProduct(@PathVariable int idProduct, ModelMap model) {
model.addAttribute("action", "Edit");
Product product = productService.get(idProduct);
model.addAttribute("product", product);
model.addAttribute("edit", true);
return "productForm";
}
@RequestMapping(value = { "/product/edit/{idProduct}" }, method = RequestMethod.POST)
public String updateProduct(Product product) {
productService.update(product);
return "redirect:/product";
}
}
Запросы:
при редактировании Product:
Hibernate:
select
product0_.`ID` as ID1_2_1_,
product0_.`DESCRIPTION` as DESCRIPT2_2_1_,
product0_.`NAME` as NAME3_2_1_,
product0_.`PRICE` as PRICE4_2_1_,
orders1_.`PRODUCT_ID` as PRODUCT_1_2_3_,
order2_.`ID` as ORDER_ID2_1_3_,
order2_.`ID` as ID1_0_0_,
order2_.`END_ADRESS` as END_ADRE2_0_0_,
order2_.`START_ADRESS` as START_AD3_0_0_
from
`PRODUCT` product0_
left outer join
`PRODUCT_ORDER` orders1_
on product0_.`ID`=orders1_.`PRODUCT_ID`
left outer join
`
ORDER` order2_
on orders1_.`ORDER_ID`=order2_.`ID` where
product0_.`ID`=?
Hibernate:
delete
from
`PRODUCT_ORDER`
where
`PRODUCT_ID`=?
Hibernate:
select
this_.`ID` as ID1_2_0_,
this_.`DESCRIPTION` as DESCRIPT2_2_0_,
this_.`NAME` as NAME3_2_0_,
this_.`PRICE` as PRICE4_2_0_
from
`PRODUCT` this_
при редактировании Order:
Hibernate:
select
order0_.`ID` as ID1_0_1_,
order0_.`END_ADRESS` as END_ADRE2_0_1_,
order0_.`START_ADRESS` as START_AD3_0_1_,
products1_.`ORDER_ID` as ORDER_ID2_0_3_,
product2_.`ID` as PRODUCT_1_1_3_,
product2_.`ID` as ID1_2_0_,
product2_.`DESCRIPTION` as DESCRIPT2_2_0_,
product2_.`NAME` as NAME3_2_0_,
product2_.`PRICE` as PRICE4_2_0_
from
`
ORDER` order0_ left outer join
`PRODUCT_ORDER` products1_
on order0_.`ID`=products1_.`ORDER_ID`
left outer join
`PRODUCT` product2_
on products1_.`PRODUCT_ID`=product2_.`ID`
where
order0_.`ID`=?
Hibernate:
select
this_.`ID` as ID1_0_0_,
this_.`END_ADRESS` as END_ADRE2_0_0_,
this_.`START_ADRESS` as START_AD3_0_0_
from
`
ORDER` this_
Айфон мало держит заряд, разбираемся с проблемой вместе с AppLab
Перевод документов на английский язык: Важность и ключевые аспекты
Ни в примерах, ни в документации не нашел о том как добавить на экран информацию о масштабеВ стандартном приложении справа снизу она отображается
В одном из полей сущности хранится музыкальная композиция (также есть текст и картинка), сущность хранится в базеAjax запросом обращаюсь к Spring...
Я пытаюсь написать дождь на java, но пока ни как не могу заставить дождинку двигаться:
http://bookboxsgzzz