[英]Hibernate Error When Commit session
這是我在DAOImpl(Hibernate)中的代碼:
@Transactional
public void insert(Cage cage) {
Session session = null;
Transaction tx = null;
try{
session = getHibernateTemplate().getSessionFactory().openSession();
tx = session.beginTransaction();
session.saveOrUpdate(cage);
session.flush();
session.clear();
tx.commit();
}catch(RuntimeException e){
try{
tx.rollback();
}catch(RuntimeException rbe){
rbe.printStackTrace();
System.out.println("Couldn’t roll back transaction");
}
throw e;
}finally{
if(session!=null){
session.close();
}
}
}
當第二次發生此問題的操作數據輸入(相同PK)時:
org.hibernate.exception.ConstraintViolationException:無法執行JDBC批處理更新
根據您的問題
When for the second time operations data entry (Same PK) takes place with this problem : org.hibernate.exception.ConstraintViolationException:
您試圖兩次插入相同的主鍵。 數據庫中的兩個條目不能具有相同的主鍵。
主鍵必須包含UNIQUE值。 檢查此鏈接http://www.w3schools.com/sql/sql_primarykey.asp
保持主鍵唯一,您將不會遇到此異常。 而且,如果您需要該列的重復條目,請不要將其設為主鍵
自動生成ID
@Id @GeneratedValue(strategy = GenerationType.AUTO)@Column(name =“ \\” ID \\“”)私有int ID;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.