Iam getting this exception.
Code:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.xxx.model.Order
My tables:
ordersCREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
orderlinesCREATE TABLE `orderlines` (
`orderLineId` int(11) NOT NULL auto_increment,
`orderId` int(11) NOT NULL,
`product_id` int(11) NOT NULL,
`quantity` int(11) NOT NULL,
`product_price` decimal(19,2) NOT NULL,
`total_price` decimal(19,2) NOT NULL,
PRIMARY KEY (`orderLineId`),
KEY `FK_orders_orders` (`orderId`),
KEY `FK_orders_products` (`product_id`),
CONSTRAINT `FK_orders_orders` FOREIGN KEY (`orderId`) REFERENCES `orders` (`id`),
CONSTRAINT `FK_orders_products` FOREIGN KEY (`product_id`) REFERENCES `products` (`ID`)
);
These are my class files.Order.javaCode:
@Entity
@Table(name="orders")
public class Order {
@Id
@GeneratedValue
@Column(name="id")
private int id;
@Column(name="firstname")
private String firstName;
@Column(name="lastname")
private String lastName;
//
@OneToMany(mappedBy = "order")
private Set<OrderLine> orderLines;
//
OrderLine.javaCode:
@Entity
@Table(name="orderlines")
public class OrderLine {
@Id
@GeneratedValue
@Column(name="orderLineId")
private int orderLineId;
@ManyToOne
@JoinColumn(name = "orderId")
private Order order;
@ManyToOne(targetEntity = Product.class,
cascade = CascadeType.ALL,
fetch = FetchType.LAZY)
@JoinTable(
name="products",
joinColumns=@JoinColumn(name="product_id")
)
private Product product;
//
Controller class part Code:
@RequestMapping("checkout")
public String checkout(Map<String, Object> map) {
map.put("order", new Order());
return "checkout";
}
@RequestMapping(value = "checkout", method = RequestMethod.POST)
public String processOrder(@ModelAttribute("order") Order order,
@ModelAttribute("cart") Cart cart,
BindingResult result) {
if (!result.hasErrors()) {
Set<OrderLine> orderLines = new HashSet<OrderLine>();
for(CartLine c : cart.getCartLines()) {
OrderLine line = new OrderLine();
line.setOrder(order);
line.setProduct(c.getProduct());
line.setProductPrice(c.getProduct().getPrice());
line.setTotalPrice(c.getPrice());
orderLines.add(line);
}
orderService.save(order);
}
return "ordersuccess";
}
The exception points to the save operation.