在 Hibernate 中:是否可以为实体混合注释和 XML 配置? [英] In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?

查看:29
本文介绍了在 Hibernate 中:是否可以为实体混合注释和 XML 配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

那么,是否可以混合使用这两种配置,而不是只使用其中一种?

So, is possible to mix both configurations instead of use only one of those?

我想要的只是通过注释保留所有配置并通过 XML 读取表格.

All I want is to keep all the configuration by Annotations and read the table by a XML.

有可能吗?

非常感谢.

hbm.xml 文件将如何?我有这个:

How will be the hbm.xml file? I have this one:

<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
    <class name="MyData" table="MyTable" >
    </class>
</hibernate-mapping>

并且不要编译 dtd.

And don´t compile the dtd.

推荐答案

Hibernate 文档(最新)

Hibernate docs (latest)

注意你可以混合使用遗留的 hbm.xml 和注解方法.资源元素可以是 hbm 文件或 EJB3XML 部署描述符.区别对您来说是透明的配置过程.

Note that you can mix the legacy hbm.xml use and the annotation approach. The resource element can be either an hbm file or an EJB3 XML deployment descriptor. The distinction is transparent for your configuration process.

您可以混合使用带注释的持久类和经典的 hbm.cfg.xml具有相同 SessionFactory 的声明.但是你不能声明一个类多次(无论是注释还是通过 hbm.xml).你不能在实体中混合配置策略(hbm 与注释)层次结构.

You can mix annotated persistent classes and classic hbm.cfg.xml declarations with the same SessionFactory. You can however not declare a class several times (whether annotated or through hbm.xml). You cannot mix configuration strategies (hbm vs annotations) in an entity hierarchy either.

为了简化从 hbm 文件到注释的迁移过程,配置机制检测之间的映射重复注释和 hbm 文件.HBM 文件的优先级高于在类到类的基础上注释元数据.你可以改变使用 hibernate.mapping.precedence 属性的优先级.默认是hbm, class 并将其更改为 class, hbm 将优先考虑注释发生冲突时 hbm 文件上的类.

To ease the migration process from hbm files to annotations, the configuration mechanism detects the mapping duplication between annotations and hbm files. HBM files are then prioritized over annotated metadata on a class to class basis. You can change the priority using hibernate.mapping.precedence property. The default is hbm, class and changing it to class, hbm will prioritize the annotated classes over hbm files when a conflict occurs.

这篇关于在 Hibernate 中:是否可以为实体混合注释和 XML 配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆