一、复习
实体状态
transient 临时状态 only in memory , not associted with persistent manager , new Entity()
persistent 持久状态 both in memory(associted with persistent manager) and in database
detached 游离状态 both in memory(not associted with persistent manager) and in database
关联关系
J2EE分层
表现层,业务层,数据层
二、Hibernate控制的事务
ACID
atomicity consistency isolation durability
事务保证原子操作的不可分,也就是操作的同时成功或同时失败。
Transaction tran=session.beginTranaction();
tran.commit();
tran.rollback();
以上是事务对象的方法,来实现对事务的支持。
三、hibernate的事务隔离级别
hibernate的事务隔离级别和JDBC中大致相同。
设置时要在hibernate.cfg.xml配置
<property name="hibernate.connection.isolation">4</property>
1,读未提交的数据(Read uncommitted isolation)
2,读已提交的数据(Read committed isolation)
4,可重复读级别(Repeatable read isolation)
8,可串行化级别(Serializable isolation)
四、hibernate的锁(悲观锁,乐观锁)
session.get(User.class , userid , LockMode.UPGRADE);
悲观锁是由数据库本身所实现的,会对数据库中的数据进行锁定,也就是锁行。
LockMode.UPGRADE,修改锁,在get()方法中加上这个设置作为第三个参数。
LockMode.NONE 无锁机制
LockMode.READ 读取锁
LockMode.WRITE 写入锁,不能在程序中直接使用
还可以使用Session.lock() Query.setLockMode() Criteria.setLockMode()方法来设置锁
乐观锁,也就是通过对记录加上某些信息来解决并发访问的问题。
版本检查
要在其表中多加上一列表示版本信息,会在读取时读到这个版本号,并在修改之后更新这个版本号,并且只有版本号相同才会予以更新,如果版本号低,就会抛出例外。
<version name="version" column="version" type="integer" />
时间戳
使用时间戳,是通过最后修改时间来判断是否来做更新操作,也就是只有在最后更新时间之后才会做更新。
<timestamp name="updateTime" column="updatetime"/>
五、类继承关系映射
建表策略
(1)所有类建一个表 (2)只为具体类建表 (3)每个类建一个表。
只为具体类建表,使用于不使用多态的情况下,具体类之间没有继承关系时适用
需要针对每个类写映射配置文件,就和普通的单表映射的xml文件相同。
也可以使用一个xml文件来进行映射,可以通过写union-subclass标签来表现其关系
这里不能使用id生成策略中的native,而是要指定特定的生成策略。
例:
<union-subclass name="notpad" table="tpcc_notpad">
<property name="wight" column="wight" type="integer"/>
</union-subclass>
<union-subclass name="desktop" table="tpcc_desktop">
<property name="LCD" column="isLCD" type="yes_no"/>
</union-subclass>
每个类建一个表,可以有效减少数据的冗余,减少字段,查询效率不很高。
正对每个类建一个表,只要写一个配置文件来进行类的映射即可
映射文件中的子类可以使用join-subclass标签来表示,并且引用父类的主键作为共享主键,就是不需要指定id生成策略
例:
<hibernate-mapping package="alan.hbn.rel.inherit" auto-import="false">
<class name="Computer" table="tph_computer">
<id name="comid" column="comid" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="price" column="price" type="integer"/>
<joined-subclass name="notpad" table="tpc_notpad">
<key column="comid" />
<property name="wight" column="wight" type="integer"/>
</joined-subclass>
<joined-subclass name="Guest" table="tpc_guest">
<key column="comid" />
<property name="LCD" column="isLCD" type="yes_no"/>
</joined-subclass>
</class>
</hibernate-mapping>
所有类只建一个表,查寻效率比较高,但是会产生很多空间浪费,当子类中的非空约束,就不大适用了,这是对于子类可以使用subclass标签表示。
<hibernate-mapping package="alan.hbn.rel.inherit" auto-import="false">
<class name="Computer" table="tph_computer">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<discriminator column="computer_type" type="integer"/>
<property name="price" column="price" type="integer"/>
<subclass name="Administrator" discriminator-value="ad">
<property name="wight" column="wight" type="integer"/>
</subclass>
<subclass name="Guest" discriminator-value="gu">
<property name="LCD" column="isLCD" type="yes_no"/>
</subclass>
</class>
</hibernate-mapping>
不考虑多态时,最好是用只针对具体类建表(2个表),而考虑多态时尽量使用所有类建一个表,只有当子类中的属性过多是才考虑每个类建一个表的策略。
实体状态
transient 临时状态 only in memory , not associted with persistent manager , new Entity()
persistent 持久状态 both in memory(associted with persistent manager) and in database
detached 游离状态 both in memory(not associted with persistent manager) and in database
关联关系
J2EE分层
表现层,业务层,数据层
二、Hibernate控制的事务
ACID
atomicity consistency isolation durability
事务保证原子操作的不可分,也就是操作的同时成功或同时失败。
Transaction tran=session.beginTranaction();
tran.commit();
tran.rollback();
以上是事务对象的方法,来实现对事务的支持。
三、hibernate的事务隔离级别
hibernate的事务隔离级别和JDBC中大致相同。
设置时要在hibernate.cfg.xml配置
<property name="hibernate.connection.isolation">4</property>
1,读未提交的数据(Read uncommitted isolation)
2,读已提交的数据(Read committed isolation)
4,可重复读级别(Repeatable read isolation)
8,可串行化级别(Serializable isolation)
四、hibernate的锁(悲观锁,乐观锁)
session.get(User.class , userid , LockMode.UPGRADE);
悲观锁是由数据库本身所实现的,会对数据库中的数据进行锁定,也就是锁行。
LockMode.UPGRADE,修改锁,在get()方法中加上这个设置作为第三个参数。
LockMode.NONE 无锁机制
LockMode.READ 读取锁
LockMode.WRITE 写入锁,不能在程序中直接使用
还可以使用Session.lock() Query.setLockMode() Criteria.setLockMode()方法来设置锁
乐观锁,也就是通过对记录加上某些信息来解决并发访问的问题。
版本检查
要在其表中多加上一列表示版本信息,会在读取时读到这个版本号,并在修改之后更新这个版本号,并且只有版本号相同才会予以更新,如果版本号低,就会抛出例外。
<version name="version" column="version" type="integer" />
时间戳
使用时间戳,是通过最后修改时间来判断是否来做更新操作,也就是只有在最后更新时间之后才会做更新。
<timestamp name="updateTime" column="updatetime"/>
五、类继承关系映射
建表策略
(1)所有类建一个表 (2)只为具体类建表 (3)每个类建一个表。
只为具体类建表,使用于不使用多态的情况下,具体类之间没有继承关系时适用
需要针对每个类写映射配置文件,就和普通的单表映射的xml文件相同。
也可以使用一个xml文件来进行映射,可以通过写union-subclass标签来表现其关系
这里不能使用id生成策略中的native,而是要指定特定的生成策略。
例:
<union-subclass name="notpad" table="tpcc_notpad">
<property name="wight" column="wight" type="integer"/>
</union-subclass>
<union-subclass name="desktop" table="tpcc_desktop">
<property name="LCD" column="isLCD" type="yes_no"/>
</union-subclass>
每个类建一个表,可以有效减少数据的冗余,减少字段,查询效率不很高。
正对每个类建一个表,只要写一个配置文件来进行类的映射即可
映射文件中的子类可以使用join-subclass标签来表示,并且引用父类的主键作为共享主键,就是不需要指定id生成策略
例:
<hibernate-mapping package="alan.hbn.rel.inherit" auto-import="false">
<class name="Computer" table="tph_computer">
<id name="comid" column="comid" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<property name="price" column="price" type="integer"/>
<joined-subclass name="notpad" table="tpc_notpad">
<key column="comid" />
<property name="wight" column="wight" type="integer"/>
</joined-subclass>
<joined-subclass name="Guest" table="tpc_guest">
<key column="comid" />
<property name="LCD" column="isLCD" type="yes_no"/>
</joined-subclass>
</class>
</hibernate-mapping>
所有类只建一个表,查寻效率比较高,但是会产生很多空间浪费,当子类中的非空约束,就不大适用了,这是对于子类可以使用subclass标签表示。
<hibernate-mapping package="alan.hbn.rel.inherit" auto-import="false">
<class name="Computer" table="tph_computer">
<id name="id" column="id" type="long" unsaved-value="0">
<generator class="identity"/>
</id>
<discriminator column="computer_type" type="integer"/>
<property name="price" column="price" type="integer"/>
<subclass name="Administrator" discriminator-value="ad">
<property name="wight" column="wight" type="integer"/>
</subclass>
<subclass name="Guest" discriminator-value="gu">
<property name="LCD" column="isLCD" type="yes_no"/>
</subclass>
</class>
</hibernate-mapping>
不考虑多态时,最好是用只针对具体类建表(2个表),而考虑多态时尽量使用所有类建一个表,只有当子类中的属性过多是才考虑每个类建一个表的策略。