Hibernate2-->Hibernate3的注意事项

本文档提供了将应用程序从旧版Hibernate迁移到HB3并在WebLogic上运行所需的更改列表。涉及更新包名、配置文件调整、方法调用转换及缓存设置等内容。
I make use of Hibernate metadata as well as Spring.  Here's my list of
all the changes I had to make to run my application with HB3 on
WebLogic. This may repeat some of this information above and is not
meant to be exhaustive.

1. global replace net.sf.hibernate to org.hibernate (be sure to include
the XML configuration files as well)
2. global replace org.springframework.orm.hibernate to
org.springframework.orm.hibernate3 (be sure to include the XML
configuration files as well)
3. global replace net.sf.hibernate.expression.Expression to
org.hibernate.criterion.Expression
4. global replace net.sf.hibernate.expression.Order to
org.hibernate.criterion.Order
5. convert method call getHibernateTemplate.getNamedQuery(Session sess,
String query) to session.getNamedQuery(String query)
6. convert method call getHibernateTemplate().createCriteria(session,
getReferenceClass()) to session.createCriteria(getReferenceClass())
7. Add new EntityMode.POJO parameter as follows to:
ClassMetadata.getPropertyValue(dest,
SkipFields.TCN.toString().substring(1), EntityMode.POJO)
8. Add new EntityMode.POJO parameter as follows to:
classMetadata.getMappedClass(EntityMode.POJO)
9. global replace HibernateProxyHelper.getClass(object) to
HibernateProxyHelper.getClassWithoutInitializingProxy(object);
10. Convert method call: propertyTypes.isPersistentCollectionType()) to
propertyTypes.isCollectionType())
11. Update the DTD as follows:
<!DOCTYPE hibernate-configuration
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
12. Replace in the HBM files any instance of “<generator class=”vm” />
to of “<generator class=”native” />
13. Add lazy=”false” to any class or association if you don’t use to use
lazy initialization. However, this is strongly discouraged and thus why
the default is now set to true.
14. Caching is now enabled by default using the EHCache. If you want to
disable this, you must do this from the hibernate.cfg.xml. Again, this
is strongly discouraged.
15. Remove all the previous versions of the .jars associated with
Hibernate and replace them with the new versions.
16. On BEA, you have to move antlr-2.7.5H3.jar to the PRE_CLASSPATH of
the server to run inside BEA because BEA includes this tool in their
system classpath that is inconsistent.
<think>我们正在讨论Hibernate中的两种关联映射:<many-to-one>和<one-to-many>。它们分别用于表示多对一和一对多的关系。根据引用[1]和引用[2]的内容,并结合Hibernate的知识,我们可以总结如下: 1. **映射方向**: - `<many-to-one>`:用于多对一关系,即多个实体实例关联到同一个实体实例。例如,多个员工(Employee)属于同一个部门(Department)。在“多”的一方(Employee)的映射文件中配置。 - `<one-to-many>`:用于一对多关系,即一个实体实例关联到多个实体实例。例如,一个部门(Department)有多个员工(Employee)。在“一”的一方(Department)的映射文件中配置。 2. **配置位置**: - `<many-to-one>`:配置在“多”的一方的映射文件中(如Employee.hbm.xml)。 - `<one-to-many>`:配置在“一”的一方的映射文件中(如Department.hbm.xml),通常作为`<set>`、`<list>`等集合元素的子元素。 3. **数据库表示**: - `<many-to-one>`:在“多”的一方的数据库表中生成一个外键列(或多列,如果是复合外键),指向“一”的一方的主键。 - `<one-to-many>`:在“一”的一方没有对应的数据库列,因为关系是通过“多”的一方的外键来维护的。它只是定义了从“一”的一方如何访问关联的多个对象。 4. **配置属性**: - `<many-to-one>`:常用属性包括`name`(属性名)、`class`(关联的类)、`column`(外键列名)等。另外,如果设置`unique="true"`,则变成一对一关系(引用[1])。 - `<one-to-many>`:常用属性是`class`(关联的类),它作为集合元素的子元素出现。 5. **级联和反转控制**: - 两者都可以配置`cascade`属性来设置级联操作。 - 在双向关联中,通常需要在“一”的一方设置`inverse="true"`,表示关系由“多”的一方维护(即外键的更新由`<many-to-one>`端控制),以避免重复更新。 6. **使用示例**: **Employee.hbm.xml (多的一方)**: ```xml <class name="Employee" table="employee"> <id name="id">...</id> <many-to-one name="department" class="Department" column="dept_id" /> </class> ``` **Department.hbm.xml (一的一方)**: ```xml <class name="Department" table="department"> <id name="id">...</id> <set name="employees" inverse="true"> <key column="dept_id" /> <one-to-many class="Employee" /> </set> </class> ``` 7. **引用[2]的说明**:`<many-to-one>`映射的是实体类型(即另一个实体类),而原子类型(如String、Date等)则直接映射为表的字段。`<one-to-many>`映射的也是实体类型,但它是集合形式。 8. **注意事项**: - 在双向关联中,必须确保双方映射的列名(外键)一致。 - 使用`<one-to-many>`时,通常配合集合(如Set、List)使用。 综上所述,两者的主要区别在于映射的方向、配置位置和数据库表示方式。`<many-to-one>`在“多”的一方配置,产生外键;`<one-to-many>`在“一”的一方配置,不产生外键,只是定义关联的集合。 §§相关问题§§ 1. Hibernate中如何配置双向一对多关联? 2. 在双向关联中,为什么建议设置`inverse="true"`? 3. 如何通过Hibernate实现多对多关系?</think>### Hibernate 中 `<many-to-one>` 和 `<one-to-many>` 配置区别详解 #### 1. **本质区别** | **特性** | **`<many-to-one>`** | **`<one-to-many>`** | |------------------|------------------------------------------------|------------------------------------------------| | **映射方向** | 多对一(多方指向一方) | 一对多(一方指向多方) | | **配置位置** | 多方实体映射文件(如 `Employee.hbm.xml`) | 一方实体映射文件(如 `Department.hbm.xml`) | | **数据库实现** | 在多方表中创建外键列 | 不创建新列,依赖多方外键反向关联 | | **Java 类型** | 实体对象(如 `Department`) | 集合类型(如 `Set<Employee>`) | #### 2. **配置语法对比 **`<many-to-one>` 示例**(多方配置): ```xml <!-- Employee.hbm.xml --> <many-to-one name="department" <!-- 多方实体中的属性名 --> class="Department" <!-- 关联的一方实体类 --> column="dept_id" <!-- 外键列名 --> not-null="true" <!-- 非空约束 --> /> ``` - 在 `employee` 表中生成外键列 `dept_id` - Java 实体:`Employee` 类包含 `Department department` 字段 **`<one-to-many>` 示例**(一方配置): ```xml <!-- Department.hbm.xml --> <set name="employees"> <!-- 一方实体中的集合属性名 --> <key column="dept_id"/> <!-- 关联的多方外键列名 --> <one-to-many class="Employee"/> <!-- 关联的多方实体类 --> </set> ``` - 不修改 `department` 表结构 - Java 实体:`Department` 类包含 `Set<Employee> employees` 字段 #### 3. **核心差异点 1. **外键控制权** - `<many-to-one>` 直接控制外键值 - `<one-to-many>` 需通过 `inverse="true"` 声明由多方维护外键[^1] ```xml <set name="employees" inverse="true">...</set> ``` 2. **级联操作** - `<many-to-one>` 级联删除需谨慎(可能违反外键约束) - `<one-to-many>` 可配置级联删除一方时自动删除多方 ```xml <set name="employees" cascade="delete-orphan">...</set> ``` 3. **查询行为** - `<many-to-one>` 默认立即加载(`fetch="join"`) - `<one-to-many>` 默认延迟加载(`lazy="true"`) #### 4. **双向关联最佳实践 ```xml <!-- Department.hbm.xml (一方) --> <set name="employees" inverse="true" cascade="save-update"> <key column="dept_id"/> <one-to-many class="Employee"/> </set> <!-- Employee.hbm.xml (多方) --> <many-to-one name="department" class="Department" column="dept_id" not-null="true" /> ``` - **关键点**: - `inverse="true"` 确保外键由多方维护 - 双方配置的 `column` 名称必须一致 - 避免级联删除循环(使用 `delete-orphan` 替代 `all-delete-orphan`) #### 5. **特殊场景 - **一对一实现**:在 `<many-to-one>` 中添加 `unique="true"`[^1] ```xml <many-to-one name="address" unique="true"/> <!-- 强制一对一 --> ``` - **复合外键**:双方均需配置多列 ```xml <!-- 多方 --> <many-to-one name="project"> <column name="dept_id"/> <column name="project_code"/> </many-to-one> <!-- 一方 --> <set name="tasks"> <key> <column name="dept_id"/> <column name="project_code"/> </key> <one-to-many class="Task"/> </set> ``` #### 6. **常见错误 1. **重复映射**: - 错误:同时在多方配 `<property name="dept_id">` 和 `<many-to-one column="dept_id">` - 解决:删除冗余属性映射 2. **约束冲突**: - 现象:删除一方时触发 `ConstraintViolationException` - 解决:配置级联删除或手动清理多方数据 3. **列名不一致**: - 错误:`<many-to-one column="dept_id">` 但 `<key column="department_id">` - 结果:关联失效 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值