我是春季新人,冬眠。我有一个无法正常工作的示例项目。当我尝试保存来自用户的新订单时,没有任何错误或异常,但是记录未插入数据库。 这是我的代码 strong text StoreController.java
@Autowired
OrderService orderService;
@SuppressWarnings("unchecked")
@RequestMapping(value = "/store/addorder", method = RequestMethod.GET)
public ModelAndView addOrder(HttpSession session) {
ModelAndView model = new ModelAndView();
// create list of products that we have to add in orders
List<CartItem> items = (List<CartItem>) session.getAttribute("cart");
Set<CartItem> itemsSet = new HashSet<CartItem>();
// new order generated and setter methods invoke
Orders order = new Orders(itemsSet);
Date d = new Date();
Date delivery = StoreUtils.deliveryDate(d, 3);
order.setOrderDate(d);
order.setDeliveryDate(delivery);
order.setItems(itemsSet);
for (CartItem cartItem : items) {
itemsSet.add(cartItem);
}
String addOrders = orderService.addOrders(order);
System.err.println("new order add status " + addOrders + "-------------");
// change product quantity after adding new order
if (!addOrders.toLowerCase().contains("error")) {
for (int i = 0; i < items.size(); i++) {
Integer qSale = items.get(i).getQuantity() * (-1);
productService.rechargeProduct(items.get(i).getProduct(), qSale);
}
model.setViewName("successorder");
model.addObject("order", order);
model.addObject("message", addOrders);
session.setAttribute("cart", null);
} else {
session.setAttribute("error", addOrders);
model.setViewName("redirect:/addtocartlist");
}
return model;
}
Orders.java
@Entity
@Table(name = "orders")
public class Orders implements Serializable {
private static final long serialVersionUID = -3672662224925418969L;
@Id
@Column(name = "orderid", nullable = false)
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@DateTimeFormat(pattern = "yyyy-mm-dd")
@Column(name = "orderDate", nullable = false)
private Date orderDate;
@DateTimeFormat(pattern = "yyyy-mm-dd")
@Column(name = "delivery", nullable = false)
private Date deliveryDate;
@Column(name = "success", nullable = true, columnDefinition = "tinyint(1) default 0")
private boolean success;
@Column(name = "cancel", nullable = true, columnDefinition = "tinyint(1) default 0")
private boolean canceled;
@Column(name = "cause", nullable = true)
private String cancelCause;
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinTable(name = "ORDERS_ITEMS", joinColumns = { @JoinColumn(name = "orderid") }, inverseJoinColumns = {
@JoinColumn(name = "item_id") })
private Set<CartItem> items = new HashSet<CartItem>(0);
//setters and getters
}
CartItem.java
@Entity
@Table(name = "items")
public class CartItem implements Serializable {
private static final long serialVersionUID = 7968604053015663078L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "item_id", nullable = false)
private Long id;
@Column(name = "quantity", nullable = false, columnDefinition = "int(11) default 1")
private Integer quantity;
@Column(name = "totalprice", nullable = false)
private BigDecimal totalprice;
@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "prd_id", nullable = false)
private Product product;
//setters and getters
}
Product.java
@Entity
@Table(name = "products")
public class Product implements Serializable {
private static final long serialVersionUID = -7738539408628995177L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "prd_id")
private Long id;
@Column(name = "full_name", nullable = false)
private String fullName;
@Column(name = "seller_name")
private String seller;
@Column(name = "company_name", nullable = false)
private String companyName;
@Column(name = "created_date")
@DateTimeFormat(pattern = "yyyy-mm-dd")
private Date createdDate;
@Column(name = "expiry_date")
@DateTimeFormat(pattern = "yyyy-mm-dd")
private Date expiryDate;
@Column(name = "insert_date")
@DateTimeFormat(pattern = "yyyy-mm-dd")
private Date insertDate;
@Column(name = "likes", nullable = true)
private Integer likeCount;
@Column(name = "quantity", nullable = true)
private Integer quantity;
@Column(name = "price", nullable = false)
private BigDecimal price;
@Column(name = "category", nullable = false)
private String category;
@Column(name = "description", nullable = true)
private String description;
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy = "product")
private Set<CartItem> items;
//setters and getters
}
最后是我的doa实现代码
OrdersDaoImpl.java
@Repository("ordersDao")
public class OrdersDaoImpl implements OrdersDao {
@Autowired
SessionFactory sessionFactory;
protected Session session() {
try {
return sessionFactory.getCurrentSession();
} catch (HibernateException e) {
return sessionFactory.openSession();
}
}
public String addOrders(Orders orders) {
String result = "";
try {
session().save(orders);
result = "success";
} catch (Exception e) {
if (e.getMessage().toLowerCase().contains("duplicate"))
result = "error this order already was exist";
else
result = "error " + e.getMessage();
System.err.println(result);
} finally {
session().clear();
}
return result;
}
}
当我尝试添加新订单时,我没有例外。为什么我的服务不起作用? 我的项目中还有另一个控制器来管理用户。在该控制器和dao实现中,添加和删除用户工作正常。 我认为我的代码中存在一对多和多对多的逻辑错误。请帮助我克服这种失败。