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更新得比较慢呢?
哎。。。