测试建表脚本:
create table Person1 ( personId number(10) not null primary key, addressId number(10) not null );
create table Address1 ( addressId number(10) not null primary key );
Address.hbm.xml:
<class name="com.chenjun.eshop.staffManage.domain.Address" table="Address1">
<id name="id" column="addressId">
<generator class="assigned"><!-- 由程序分配主键 -->
</generator>
</id>
</class>
Person.hbm.xml:
<class name="com.chenjun.eshop.staffManage.domain.Person"
table="Person1">
<id name="id" column="personId">
<generator class="assigned"><!-- 由程序分配主键 -->
</generator>
</id>
<many-to-one name="address" column="addressId" not-null="true" />
</class>
测试代码:
public void test()
{
Session session = this.getHibernateTemplate().getSessionFactory().openSession();
session.beginTransaction();
Address address = new Address();
address.setId(102);
Person person = new Person();
person.setId(202);
person.setAddress(address);
session.save(address);
session.save(person);
//hibernate中的增删改都要提交事件,因为它默认是设为不提交的,而jdbc的connect是默认提交的。
session.beginTransaction().commit();
//释放资源
session.close();
}