<class name="Person" table="T_JDBC_PERSON">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="firstName" />
<property name="lastName" />
</class>
<class name="Boat" table="T_JDBC_BOAT">
<id name="id" column="ID">
<generator class="increment" />
</id>
<property name="tag" />
<many-to-one name="driver"/>
<many-to-one name="boarder"/>
</class>
这里many to one 表示Boat 是多,而 Person 是1, 同时数据库中 <class name="Boat" table="T_JDBC_BOAT">
建立 外键 <class name="Person" table="T_JDBC_PERSON">
many to one 字面意思 是 many对应表 而 one 对应属性值。左边对应表many 右边对应属性one
public class Boat {
private Long id;
private String tag;
private Person driver;
private Person boarder;
public Boat() {
}
public Boat(String tag, Person driver, Person boarder) {
this.tag = tag;
this.driver = driver;
this.boarder = boarder;
}
Session session = openSession();
session.beginTransaction(); //事物开启
session.doWork(
new Work() { //org.hibernate.jdbc.Work接口
public void execute(Connection connection) throws SQLException {
// in this current form, users must handle try/catches themselves for proper resource release
Statement statement = null;
try {
statement = connection.createStatement();
ResultSet resultSet = null;
try {
resultSet = statement.executeQuery( "select * from T_JDBC_PERSON" );
}
finally {
releaseQuietly( resultSet ); //关闭数据集
}
try {
resultSet = statement.executeQuery( "select * from T_JDBC_BOAT" );
}
finally {
releaseQuietly( resultSet );
}
}
finally {
releaseQuietly( statement ); //关闭sql语句
}
}
}
);
session.getTransaction().commit(); //事物提交
session.close();
这个test测试了使用Hibernate 调用JDBC接口,批量更新时,这可以对数据性能方面进行优化。因为JDBC执行效率比HIbernate要高。
同时可以做一些复杂的SQL操作。