- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我目前正在将我的项目从 Hibernate HBM Mappings 迁移到 Annotations。就我处理小类而言,一切都很容易。但是我有同样巨大的类,我尝试为这个类混合映射和注释。我读到这可以通过使用 hibernate 属性“hibernate.mapping.precedence”并将其设置为“class,hbm”而不是“hbm,class”。 (见:In Hibernate: is it possible to mix Annotations and XML configuration for an Entity?)
例如,我有以下 Document 类:
@Entity
@Table(name="DOCUMENT")
public class Document {
@Column(name="DESCRIPTION")
private String description;
}
以及以下 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>
在我的 hibernate.cfg.xml 文件中我放了:
<property name="hibernate.mapping.precedence">class, hbm</property>
<mapping class="Document"/>
<mapping resource="Document.hbm.xml"/>
我的问题是:- 如果我把“类,hbm”作为优先级,那么我在类文档中只有我的注释- 如果我输入“hbm,class”,那么我在 hbm 资源中只有我的映射
有没有人知道是否有办法同时拥有注解和 HBM 映射?
谢谢
卡姆兰
PS:我使用的是:Hibernate 4.1.4 和 Spring Framework 3.1.1
最佳答案
您不能将它们混合用于同一类。在 section 1.2 of hibernate annotations 结尾:
您可以将带注释的持久类和经典的 hbm.cfg.xml 声明与相同的 SessionFactory 混合使用。 您不能多次声明一个类(无论是注释还是通过 hbm.xml)。您也不能在实体层次结构中混合配置策略(hbm 与注释)。
为了简化从 hbm 文件到注解的迁移过程,配置机制会检测注解和 hbm 文件之间的映射重复。然后,HBM 文件优先于带注释的元数据在类与类的基础上。您可以使用 hibernate.mapping.precedence 属性更改优先级。默认是hbm,class,改成class,hbm会在发生冲突时优先标注的类优先于hbm文件。
使用注解和 hbm 文件是两次声明一个类。因此,在类到类的基础上,一个将优先于另一个(类到类的基础意味着对于每个类,仅使用 hbm 文件或注释)。
关于java - Hibernate:从映射迁移到注释 - 是否可以混合使用 hbm 和注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11332698/
我使用 hibernate 作为 ORM 工具,我面临一个问题, 我需要将全局临时表映射到 HBM。现在,问题在于键,我知道在 hbm 中必须有一些关键字段,但是我应该输入什么,因为任何列在任何时间点
Hibernate 的常用配置文件主要分为 2 种:核心配置文件(hibernate.cfg.xml)和映射文件(Xxx.hbm.xml),它们主要用于配置数据库连接、事务管理、Hibernate 本
在我的 MSSQL 服务器中,我有一个名为 AllFavourite 的 SQL View 。为了将数据加载到我的 DTO 类中,我的 hbm.xml 文件中有以下内容... 在我的代码中
我有以下三个类: public class Student { private Integer studentId; private StudentSchool studentScho
我正在从 NHibernate 映射创建我的表和索引。 例如,我按如下方式创建姓氏索引: 我想创建一个复杂的索引,以便例如 LastName 和 FirstName 作为一个索引生成。这在 NH
我在映射我们的 byte[] 时遇到问题 field 。我一直在寻找几种解决方案,但到目前为止都没有奏效。我得到的只是这个异常(exception): The length of the byte[]
我正在通过代码使用 NHibernate 映射,并以这种方式创建 session 工厂: var mapper = new ModelMapper(); map
我的 hibernate hbm xml 有一个属性映射为 我正在使
我在屏幕上显示位图时遇到问题。当我编译并运行不显示任何内容时,尝试逐步调试,发现下面的代码是问题所在。 HBITMAP hbm = (HBITMAP)LoadImage(hInstance,
我是 hibernte 新手。我正在尝试使用 hibernate 从 oracle 表中简单检索记录。 我的实体类名为pojo1,pojo1.hbm.xml如下: 我在运行包含所有
例如, 如果我想要f
我正在通过代码使用 NHibernate 映射,并以这种方式创建 session 工厂: var mapper = new ModelMapper(); map
我有一个有两个主键的表,我如何在 hbm 映射文件中映射相同的表。如何定义两个主键。 最佳答案 两个主键?您可能指的是复合主键。检查this here . 关于java - 如何在 hbm 中定义
我是 hibernate 新手,遇到了一个问题。我已经阅读了 hibernate 网站上的入门指南等所有内容,但仍然无法找到解决方案。 我有一堂这样的课: public class ResultTre
我在打开 hbm 文件时遇到这个奇怪的问题。我正在使用 hibernate3.jar。我还验证了类路径中只有一个 hibernate3.jar 并且它包含 hibernate 映射 dtd 文件。 我
你能否让 1 个表被映射到 2 个不同类的 2 个不同的 HBM 引用(它们是不相关的,基本类,没有继承)。 我知道由于重复,这可能是不好的做法,但它与代码维护有关。我试图避免删除其中一个类。 谢谢,
例如, 如果我想要f
我正在开发一个 Spring 框架和 hibernate 应用程序,其中包含一个企业 Web 应用程序的中央数据库,该应用程序具有每天约有 1000 位用户在线。 您可以假设有一个计费应用程序,任何人
我是 Hibernate 新手,正在尝试插入 ArrayList在数据库中使用 .hbm 映射文件。我不想使用任何注释。我搜索了如何插入 ArrayList并从 here 找到工作代码。 我期待一个包
我在谷歌上搜索了很多,并且遵循了很多关于它的教程,但我没有让它工作。我在表 Player 和 Type 之间有多对多关系。两个表都通过 typeperplayer 连接。在表中 typeperplay
我是一名优秀的程序员,十分优秀!