配置文件
根据config 加载配置文件
hibernate会使用监听模式 在进行save update 的时候回对缓存进行更新操作。所有的操作hibernate都是调用一个监听器的方法来进行操作。监听器类似于切面编程,在原子操作的时候做其他相关操作。
private Serializable fireSave(SaveOrUpdateEvent event)
{
errorIfClosed();
checkTransactionSynchStatus();
SaveOrUpdateEventListener saveEventListener[] = listeners.getSaveEventListeners();
for(int i = 0; i < saveEventListener.length; i++)
saveEventListener[i].onSaveOrUpdate(event);
return event.getResultId();
}
hibernate.cfg.xml
<hibernate-configuration>
<!-- 一个sessionFactory代表数据库的一个连接 -->
<session-factory>
<!-- 链接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 链接数据库的密码 -->
<property name="connection.password">123</property>
<!-- 链接数据库的驱动 -->
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<!-- 链接数据库的url -->
<property name="connection.url">
jdbc:mysql://localhost:3306/test
</property>
<!--
方言
告诉hibernate用什么样的数据库
-->
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<!--
validate 加载hibernate时,验证数据库的结构 默认值
update 加载hibernate时,检查数据库,如果表不存在,则创建,如果存在,则更新
create 每次加载hiberante,都会创建表
create-drop 每次加载hiberante,创建,卸载hiberante时,销毁
-->
<property name="hbm2ddl.auto">update</property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<mapping resource="com/hibernate/domain/Person.hbm.xml" />
</session-factory>
</hibernate-configuration>
java代码
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sessionFactory = configuration.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
Person person = new Person();
session.save(person);
transaction.commit();
session.close();
hibernate的流程