用ChinatCityDAO.java 继承 AbstractADO.java执行一个数据的插入操作。
1.第一次执行insert操作成功.
控制台输出如下:
ID : null City : shanghai
Hibernate: insert into china_city (CityName, CityId) values (?, ?)
mysql> select * from china_city;
+----------------------------------+----------+
| CityId | CityName |
+----------------------------------+----------+
| 402881e70f8a5bc9010f8a5bcf720001 | shanghai |
+----------------------------------+----------+
2. 继续执行insert后失败。发现Hibernate执行了一个Update操作,而数据包含了上一个动作的ID。不明白为什么。
打印的HQL语句为
ID : 402881e70f8a5bc9010f8a5bcf720001 City : shanghai2
Hibernate: update china_city set CityName=? where CityId=?
AbstractADO.java类
protected void saveOrUpdate(Object obj) {
try {
startOperation();
session.saveOrUpdate(obj);
// session.flush();
tx.commit();
} catch (HibernateException e) {
handleException(e);
} finally {
HibernateFactory.close(session);
}
}
ChinatCityDAO.java
public boolean insert(ChinaCity city){
System.out.println("ID : " +city.getCityId() + " City : "+city.getCityName());
saveOrUpdate(city);
return true;
}