我有@ManyToOne关系。客户端有许多do_orders。我已经将客户端正确保存在数据库中。
这是我初始化程序时得到的结果:
Hibernate:从hibernate_sequences中选择sequence_next_hi_value,其中sequence_name =' DO_ORDER' Hibernate:更新hibernate_sequences设置sequence_next_hi_value =? sequence_next_hi_value =?和sequence_name =' DO_ORDER' Hibernate:从hibernate_sequences中选择sequence_next_hi_value,其中sequence_name =' ITEM' Hibernate:更新hibernate_sequences设置sequence_next_hi_value =? sequence_next_hi_value =?和sequence_name =' ITEM'
但是,当我尝试保存订单时,我得到以下异常:
control.database.DatabaseAccessFailException: Transaction Fail
at control.database.DAO.commitTransaction(DAO.java:35)
at control.servlets.ConfirmOrder.doPost(ConfirmOrder.java:74)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.securityfilter.filter.SecurityFilter.doFilter(SecurityFilter.java:188)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:724)
以下是可能涉及的三个项目类别:
客户端
@Entity
@Table(name = "CLIENT")
public class Client extends User implements Serializable {
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "wishes", joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "genericItem_id")})
private List<GenericItem> wishlist;
@ElementCollection(targetClass=Item.class)
@JoinTable(name = "inbag", joinColumns = {@JoinColumn(name = "user_id")},
inverseJoinColumns = {@JoinColumn(name = "item_id")})
private List<Item> shoppingbag;
@OneToMany(fetch = FetchType.LAZY, mappedBy = "client")
private List<Do_Order> doOrders;
public Client() {
super();
this.wishlist = new ArrayList<GenericItem>() ;
this.shoppingbag = new ArrayList<Item>();
this.doOrders = new ArrayList<Do_Order>();
}
public Client(String name, String email, String password, Integer document) {
super(name, email, password, document, UserType.CLIENT);
this.wishlist = new ArrayList<GenericItem>() ;
this.shoppingbag = new ArrayList<Item>();
this.doOrders = new ArrayList<Do_Order>();
}
public Client(String name, String email, String password, Integer document, List<GenericItem> wishlist,
List<Item> shoppingbag) {
super(name, email, password, document, UserType.CLIENT);
this.wishlist = wishlist;
this.shoppingbag = shoppingbag;
this.doOrders = new ArrayList<Do_Order>();
}
public List<GenericItem> getWishlist() {
return wishlist;
}
public void setWishlist(List<GenericItem> wishlist) {
this.wishlist = wishlist;
}
public void addToWishlist(GenericItem genericItem) {
wishlist.add(genericItem);
}
public void removeFromWishlist(GenericItem genericItem) {
wishlist.remove(genericItem);
}
public List<Item> getShoppingbag() {
return shoppingbag;
}
public void setShoppingbag(List<Item> shoppingbag) {
this.shoppingbag = shoppingbag;
}
public void addToShoppingbag(Item item) {
shoppingbag.add(item);
}
public void removeFromShoppingbag (Item item) {
shoppingbag.remove(item);
}
public List<Do_Order> getDoOrders() {
return doOrders;
}
public void setDoOrders(List<Do_Order> doOrders) {
this.doOrders = doOrders;
}
public void addOrder(Do_Order order) {
doOrders.add(order);
}
}
Do_Order
@Entity
@Table(name = "DO_ORDER")
public class Do_Order implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "order_id")
private long order_id;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "INORDER", joinColumns = {@JoinColumn(name = "order_id")}, inverseJoinColumns = {
@JoinColumn(name = "item_id")})
private List<Item> items;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "brand_id")
private Brand brand;
@Column(name = "delivered")
private boolean delivered;
@Column(name = "current_date")
private Date current_date;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "user_id")
private Client client;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "address_id")
private Address address;
@Column(name = "order_price")
private float order_price;
public Do_Order() {
}
public Do_Order(Client client, Address address, Brand brand) {
this.client = client;
this.address = address;
this.brand = brand;
this.items = new ArrayList<Item>();
current_date = new Date();
delivered = false;
}
public void addItem(Item item) {
items.add(item);
order_price += item.getGenericItem().getGenericItem_price();
}
public long getOrder_id() {
return order_id;
}
public List<Item> getItems() {
return items;
}
public boolean isDelivered() {
return delivered;
}
public void confirmDelivery() {
delivered = true;
}
public Date getCurrent_date() {
return current_date;
}
public Client getClient() {
return client;
}
public Address getAddress() {
return address;
}
public float getOrder_price() {
return order_price;
}
public Brand getBrand() {
return brand;
}
}
档案:
@Entity
@Table(name = "ITEM")
public class Item implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(name = "item_id")
private long item_id;
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "genericItem_id")
private GenericItem genericItem;
private Size size;
public Item() {
}
public Item(GenericItem genericItem, Size size) {
this.genericItem = genericItem;
this.size = size;
}
public long getItem_id() {
return item_id;
}
public GenericItem getGenericItem() {
return genericItem;
}
public Size getSize() {
return size;
}
}
现在是servlet的方法:
@Override
protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
throws ServletException, IOException {
DatabaseManager databaseManager = (DatabaseManager) httpServletRequest.getSession().getAttribute("databaseManager");
List<Item> shoppingbag= (List<Item>) httpServletRequest.getSession().getAttribute("shoppingbag");
Client client = (Client) httpServletRequest.getSession().getAttribute("client");
Address address = (Address) httpServletRequest.getSession().getAttribute("address");
List<Do_Order> associatedOrders = (List<Do_Order>) httpServletRequest.getSession().getAttribute(associatedOrders);
OrderDAO orderDAO = databaseManager.getOrderDAO();
orderDAO.beginTransaction();
for (int i = 0 ; i < associatedOrders.size(); i++) {
orderDAO.save(associatedOrders.get(i));
}
try {
orderDAO.commitTransaction();
} catch (DatabaseAccessFailException e) {
e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}