我再次遇到这个问题而且我被困在这里。下面的代码在servlet中。
AnnotationConfiguration ac=new AnnotationConfiguration();
ac.addAnnotatedClass(Client.class);
ac.addAnnotatedClass(Inventory.class);
ac.addAnnotatedClass(Ledger.class);
ac.addAnnotatedClass(QuantityBought.class);
ac.configure();
SessionFactory factory=ac.buildSessionFactory();
Session session=factory.openSession();
Client client=(Client)session.get(Client.class, Integer.parseInt(request.getParameter("cid")));
client.setBalance(Float.parseFloat(request.getParameter("balance")));
Ledger ledger=new Ledger();
Date d=new Date();
ledger.setDate_of_purchase(d);
int total=0;
for(int l=1;l<6;l++)
{
if(request.getParameter("id"+l)!=null)
{
Inventory inventory=(Inventory)session.get(Inventory.class, Integer.parseInt(request.getParameter("id"+l)));
total=total+(int)(Integer.parseInt(request.getParameter("crate"+l))*inventory.getTrade()+Integer.parseInt(request.getParameter("botle"+l))*(inventory.getMrp()/inventory.getSize_of_crate()));
QuantityBought bought=new QuantityBought();
bought.setProduct(inventory.getName());
bought.setQuantity_bought(Integer.parseInt(request.getParameter("crate"+l))*inventory.getSize_of_crate()+Integer.parseInt(request.getParameter("botle"+l)));
bought.setSize_of_quantity(inventory.getSize_bottle());
bought.setTotal(Integer.parseInt(request.getParameter("crate"+l))*inventory.getTrade()+Integer.parseInt(request.getParameter("botle"+l))*(inventory.getMrp()/inventory.getSize_of_crate()));
bought.setBelongs_to_ledger(ledger);
inventory.setQuantity_available(inventory.getQuantity_available()-(Integer.parseInt(request.getParameter("crate"+l))*inventory.getSize_of_crate()+Integer.parseInt(request.getParameter("botle"+l))));
session.beginTransaction().begin();
session.save(bought);
session.saveOrUpdate(inventory);
session.beginTransaction().commit();
}
}
ledger.setSum_total(total);
client.getLedger().add(ledger);
session.beginTransaction().begin();
session.saveOrUpdate(client);
session.save(ledger);
session.beginTransaction().commit();
}
客户端。类
@Entity
public class Client implements Serializable {
private String Name;
private String Address;
private float balance;
@Id
@GeneratedValue
private int client_id;
@OneToMany(targetEntity=Ledger.class,mappedBy="client",cascade= CascadeType.ALL)
private List<Ledger> ledger;
}
Ledger.class
@Entity
public class Ledger implements Serializable {
@Id
private int id;
@ManyToOne
private Client client;
private Date date_of_purchase;
@OneToMany(targetEntity=QuantityBought.class,mappedBy="belongs_to_ledger",cascade= CascadeType.ALL)
private List<QuantityBought> quantitybought;
private float sum_total;
}
QuantityBought.class
@Entity
public class QuantityBought implements Serializable {
@Id
@GeneratedValue
private int id;
private String product;
private int size_of_quantity;
private int quantity_bought;
@ManyToOne
private Ledger belongs_to_ledger;
private float total;}
我在运行时遇到此错误。在保存QuantityBought的对象期间。我无法理解它有什么问题。
org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update