hibernate

核心代码

Session session = util.HibernateSessionFactory.getSession();
School school = (School)session.get(School.class, 02);

System.out.println(school.getSname());
System.out.println(school.getHeader().getHname());


school.getHeader().setHname("王强");
Transaction transaction = session.beginTransaction();
session.update(school);
session.delete(school);
transaction.commit();
util.HibernateSessionFactory.closeSession();

配置文件

<class name="po.School" table="school">
<id column="SID" name="sid">
<generator class="assigned"/>
</id>
<property column="SNAME" generated="never" lazy="false" name="sname"/>
<one-to-one cascade="all" class="po.Header" name="header"/>
</class>

<class name="po.Header" table="Header">

<id name="hid" column="HID">

<generator class="increment" />
</id>
<property name="sid" column="SID"/>
<property name="hname" column="HNAME"/>
</class>

hibernate 与数据库的耦合性较低。

在一对一关系中要注意找出谁是主动方,在上面的例子中School是主动方,在主动方里面添加被动方的属性,上述例子添加了Header属性,然后到配置文件中注册。使用的时<one-to-one、>

原文地址:https://www.cnblogs.com/wyhong/p/2411854.html