java Hibernate:从映射迁移到注释 - 是否可以混合使用 hbm 和注释?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11332698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:41:35  来源:igfitidea点击:

Hibernate: Migrating from mapping to annotations - is it possible to mix hbm and annotation?

javaspringhibernateannotationshibernate-mapping

提问by marcam

I'm currently migrating my project from Hibernate HBM Mappings to Annotations. Everything was easy as far as i dealt with small classes. But I have same huge classes and i try to mix both mapping and annotations for this class. I read that this was possible by using the hibernate property "hibernate.mapping.precedence" and setting it to "class, hbm" instead of "hbm, class". (see: In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?)

我目前正在将我的项目从 Hibernate HBM 映射迁移到注释。就我处理小班课程而言,一切都很简单。但是我有相同的大类,我尝试为这个类混合映射和注释。我读到这可以通过使用休眠属性“hibernate.mapping.precedence”并将其设置为“class, hbm”而不是“hbm, class”来实现。(请参阅:在 Hibernate 中:是否可以为实体混合注释和 XML 配置?

For example I have the following Document class:

例如,我有以下 Document 类:

@Entity
@Table(name="DOCUMENT")
public class Document  {
   @Column(name="DESCRIPTION")
   private String description;
}

and the following Document.hbm.xml file:

以及以下 Document.hbm.xml 文件:

<?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="Document" table="DOCUMENT" >
    <id name="id" column="DOCUMENT_ID" type="long" />
  </class>
</hibernate-mapping>

In my hibernate.cfg.xml file I put:

在我的 hibernate.cfg.xml 文件中,我放了:

<property name="hibernate.mapping.precedence">class, hbm</property>
<mapping class="Document"/>
<mapping resource="Document.hbm.xml"/>

My problem is that: - if I put "class, hbm" for the precedence then I have ONLY my annotations in class Document - if I put "hbm, class" then I have ONLY my mappings in the hbm ressource

我的问题是: - 如果我把“class, hbm”放在首位,那么我在类 Document 中只有我的注释 - 如果我把“hbm, class”放在 hbm 资源中,那么我只有我的映射

Does anyone knwo if there is a way to have both Annotations and HBM mappings ?

有没有人知道是否有办法同时拥有注释和 HBM 映射?

Thanks

谢谢

Kamran

卡姆兰

PS: I use : Hibernate 4.1.4 and Spring Framework 3.1.1

PS:我使用:Hibernate 4.1.4 和 Spring Framework 3.1.1

回答by Pablo

You can't mix them for the same class. At the end of section 1.2 of hibernate annotations:

您不能将它们混合用于同一类。在hibernate 注释第 1.2 节末尾:

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.cfg.xml 声明与相同的 SessionFactory 混合使用。但是,您不能多次声明一个类(无论是通过注释还是通过 hbm.xml)。您也不能在实体层次结构中混合配置策略(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, changing it to class, hbm will prioritize the annotated classes over hbm files when a conflict occurs.

为了简化从 hbm 文件到注释的迁移过程,配置机制检测注释和 hbm 文件之间的映射重复。HBM 文件然后按类别优先于带注释的元数据。您可以使用 hibernate.mapping.precedence 属性更改优先级。默认为 hbm, class,将其更改为 class,当发生冲突时,hbm 会将带注释的类优先于 hbm 文件。

Using annotations and hbm files is declaring a class two times. Therefore, one will be prioritized over the other in a class to class basis (class to class basis means that for each class, only the hbm file or the annotations are used).

使用注释和 hbm 文件是两次声明一个类。因此,在类到类的基础上,一个优先于另一个(类到类的基础意味着对于每个类,只使用 hbm 文件或注释)。