java - 在 hibernate : is it possible to mix Annotations and XML configuration for an Entity?

标签 java hibernate spring annotations xml-configuration

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

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

有可能吗?

非常感谢。

编辑: 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="MyData" table="MyTable" >
    </class>
</hibernate-mapping>

并且不要编译 dtd。

最佳答案

hibernate 文档(最新)

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.

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.

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.

关于java - 在 hibernate : is it possible to mix Annotations and XML configuration for an Entity?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8643166/

相关文章:

java - NullpointerException 与连接对象创建 SQL 语句

java - 如何使用 spring data 从 firestore 的子集合中获取文档

java - Shiro Authenticating Realm 应该是事务性的吗?

hibernate - 对旧数据库使用Grails/Hibernate。如何创建缺少的功能?

Spring Boot : Testing the Service layer

spring - 无法为数据库添加 SSL 支持

java - 如何使用grails中的脚本覆盖所有打印方法的功能

java - 保存实体时出现 ConcurrentModificationException

json - 使用 Jackson JSON View 而不注释原始 bean 类

java - 以不同的间隔多次重试代码段