hibernate 为什么可以发出insert语句,却不能完全保存到数据库呢?
2009-12-31 00:16
309 查看
1、Customer类
public class Customer implements java.io.Serializable {
// Fields
private long customerId;
private String customerName;
private String customerNumber;
private Integer creditGrade;
private String customerAddress;
private String phoneNumber;
private String email;
private String certificate;
private String merchandisePlanning;
private String checkState;
private byte customerGender;
private String checkinMan;
private String remark;
private Set applications = new HashSet(0);
......
2、Customer.hbm.xml
<hibernate-mapping>
<class name="MPDMS.persistence.Customer" table="customer" schema="dbo" catalog="MPDMS">
<id name="customerId" type="long" unsaved-value="null">
<column name="customerId" precision="6" scale="0" />
<generator class="native" />
</id>
<property name="customerName" type="string">
<column name="customerName" length="20" />
</property>
<property name="customerNumber" type="string">
<column name="customerNumber" length="15" />
</property>
<property name="creditGrade" type="integer">
<column name="creditGrade" />
</property>
<property name="customerAddress" type="string">
<column name="customerAddress" length="100" />
</property>
<property name="phoneNumber" type="string">
<column name="phoneNumber" length="20" />
</property>
<property name="email" type="string">
<column name="email" length="50" />
</property>
<property name="certificate" type="string">
<column name="certificate" length="30" />
</property>
<property name="merchandisePlanning" type="string">
<column name="merchandisePlanning" length="250" />
</property>
<property name="checkState" type="string">
<column name="checkState" length="10" />
</property>
<property name="customerGender" type="byte">
<column name="customerGender" />
</property>
<property name="checkinMan" type="string">
<column name="checkinMan" length="20" />
</property>
<property name="remark" type="string">
<column name="remark" length="256" />
</property>
<set name="applications" inverse="true">
<key>
<column name="customerId" precision="6" scale="0" not-null="true" />
</key>
<one-to-many class="MPDMS.persistence.Application" />
</set>
</class>
</hibernate-mapping>
.......
3、CustomerDao
public void save(Customer transientInstance) {
log.debug("saving Customer instance");
try {
Transaction tran = getSession().beginTransaction(); //方法2
getSession().save(transientInstance);
tran.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
4 CustomerTest
public void testNewCustomer2() {
CustomerDAO dao = new CustomerDAO();
Customer customer = new Customer();
customer.setCustomerName("姓名4");
customer.setCustomerAddress("地址4");
dao.save(customer);
dao.getSession().flush();
System.out.println("/n换行/n");
}
执行改测试方法后,后台有发出插入语句
Hibernate: insert into MPDMS.dbo.customer (customerName, customerNumber, creditGrade, customerAddress, phoneNumber, email, certificate, merchandisePlanning, checkState, customerGender, checkinMan, remark) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
换行
可是,为什么sql server里面的数据没有刚刚插入的语句的?
非常奇怪!
现在问题解决了,可是是很奇怪的结果。
第二天我重新打开数据库,昨晚发出的sql语句,现在竟然能够更新到数据库了。
刚刚我再试一下发出新建记录,数据库还是没有变化,可是一旦sql server断开连接,然后重新连接打开后,数据就有更新了。
那是不是说明sql server更新得比较慢呢?
哎。。。
public class Customer implements java.io.Serializable {
// Fields
private long customerId;
private String customerName;
private String customerNumber;
private Integer creditGrade;
private String customerAddress;
private String phoneNumber;
private String email;
private String certificate;
private String merchandisePlanning;
private String checkState;
private byte customerGender;
private String checkinMan;
private String remark;
private Set applications = new HashSet(0);
......
2、Customer.hbm.xml
<hibernate-mapping>
<class name="MPDMS.persistence.Customer" table="customer" schema="dbo" catalog="MPDMS">
<id name="customerId" type="long" unsaved-value="null">
<column name="customerId" precision="6" scale="0" />
<generator class="native" />
</id>
<property name="customerName" type="string">
<column name="customerName" length="20" />
</property>
<property name="customerNumber" type="string">
<column name="customerNumber" length="15" />
</property>
<property name="creditGrade" type="integer">
<column name="creditGrade" />
</property>
<property name="customerAddress" type="string">
<column name="customerAddress" length="100" />
</property>
<property name="phoneNumber" type="string">
<column name="phoneNumber" length="20" />
</property>
<property name="email" type="string">
<column name="email" length="50" />
</property>
<property name="certificate" type="string">
<column name="certificate" length="30" />
</property>
<property name="merchandisePlanning" type="string">
<column name="merchandisePlanning" length="250" />
</property>
<property name="checkState" type="string">
<column name="checkState" length="10" />
</property>
<property name="customerGender" type="byte">
<column name="customerGender" />
</property>
<property name="checkinMan" type="string">
<column name="checkinMan" length="20" />
</property>
<property name="remark" type="string">
<column name="remark" length="256" />
</property>
<set name="applications" inverse="true">
<key>
<column name="customerId" precision="6" scale="0" not-null="true" />
</key>
<one-to-many class="MPDMS.persistence.Application" />
</set>
</class>
</hibernate-mapping>
.......
3、CustomerDao
public void save(Customer transientInstance) {
log.debug("saving Customer instance");
try {
Transaction tran = getSession().beginTransaction(); //方法2
getSession().save(transientInstance);
tran.commit();
log.debug("save successful");
} catch (RuntimeException re) {
log.error("save failed", re);
throw re;
}
}
4 CustomerTest
public void testNewCustomer2() {
CustomerDAO dao = new CustomerDAO();
Customer customer = new Customer();
customer.setCustomerName("姓名4");
customer.setCustomerAddress("地址4");
dao.save(customer);
dao.getSession().flush();
System.out.println("/n换行/n");
}
执行改测试方法后,后台有发出插入语句
Hibernate: insert into MPDMS.dbo.customer (customerName, customerNumber, creditGrade, customerAddress, phoneNumber, email, certificate, merchandisePlanning, checkState, customerGender, checkinMan, remark) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
换行
可是,为什么sql server里面的数据没有刚刚插入的语句的?
非常奇怪!
现在问题解决了,可是是很奇怪的结果。
第二天我重新打开数据库,昨晚发出的sql语句,现在竟然能够更新到数据库了。
刚刚我再试一下发出新建记录,数据库还是没有变化,可是一旦sql server断开连接,然后重新连接打开后,数据就有更新了。
那是不是说明sql server更新得比较慢呢?
哎。。。
相关文章推荐
- 为什么数据库有时候不能定位阻塞(Blocker)源头的SQL语句
- 关于hibernate HQL不能使用insert语句
- SQL语句中,为什么where子句不能使用列别名,而order by却可以?
- 故障:hibernate操作数据库时,可以正常添加数据库,但是使用下面的语句时,报错:Query为空。
- hibernate left join 多表连接查询不到结果,但生成的查询语句在数据库可以查询
- 异构数据库之间完全可以用SQL语句导数据[转自大富翁论坛]
- hibernate save() 不能保存实体(能打印sql语句)
- java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句
- 解决getHibernateTemplate().save ()不能将数据保存到数据库的问题
- 解决getHibernateTemplate().save ()不能将数据保存到数据库的问题
- 为什么数据库有时候不能定位阻塞(Blocker)源头的SQL语句
- Hibernate可以使用insert语句
- hibernate通过反射,动态生成insert语句原理 并向数据库添加数据
- Command不是也可以对数据库进行insert,update,delete操作吗,为什么还需要DataAdapter。我知道DataAdapter可以用来想AataSet中填充数据,Command不可以吗?
- hibernate HQL中不能使用insert语句
- hibernateTemplate.save() 不能保存实体(能打印sql语句)
- hibernate-不能保存数据到数据库(数据不能持久化)
- SQL语句中,为什么where子句不能使用列别名,而order by却可以?
- getHibernateTemplate().save ()不能保存到数据库的问题
- SSH里面使用getHibernateTemplate().save(s)不能保存到数据库的问题