那么,可以混合使用两种配置而不是只使用其中一种吗?
我想要的只是通过注释保留所有配置并通过 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 toclass, 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/