繰り返しますが、私はこの問題を抱えており、ここで立ち往生しています。すぐ下のコードはサーブレットです。
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;
}
元帳.クラス
@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