-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: ConstraintViolationException
PostPosted: Tue Jul 15, 2014 7:34 am 
Newbie

Joined: Tue Jul 15, 2014 7:12 am
Posts: 1
Iam getting this exception.
Code:
org.hibernate.exception.ConstraintViolationException: could not insert: [com.xxx.model.Order

My tables:
orders

CREATE TABLE `orders` (
`id` int(11) NOT NULL auto_increment,
`firstname` varchar(50) NOT NULL,
`lastname` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
);
orderlines
CREATE 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.java
Code:
@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.java
Code:
@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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.