在Hibernate:是否有可能混合实体的注解和XML配置? [英] In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?

查看:275
本文介绍了在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.

推荐答案

休眠文件(最新)

请注意,您可以混合传统使用hbm.xml和注解
  做法。资源元素可以是一个HBM文件或一个EJB3
  XML部署描述符。的区别是透明您
  配置过程。

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 VS注解)
  无论是层次

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,类键,将其更改为类,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天全站免登陆