Hibernate Annotation / XML解决方法

时间:2014-10-17 07:58:50

标签: java xml hibernate jpa orm

我正在处理一个hibernate项目,其中有注释和xml映射实体。

这是设置,实体/ A类是基于注释的。

Class A

@Id
long id;

我需要A类中的一个集合,它只能通过XML配置进行映射。对于我所知道的给定实体,我们不能混合注释和xml。

但是,我可以创建一个包装类,将其称为X,并使用我在A中放入的集合xml配置为X创建相应的XML映射吗?所以我们有:

Class A

@Id
long id;

Object X;

然后还有一个X.hbm.xml文件。请注意,此文件将包含映射以表示来自表B的列的集合,该列由A的主键映射,该主键是B中的外键。所以我猜它实际上必须引用表A和B.

这种解决方法是否可行?看起来像一个长镜头......

1 个答案:

答案 0 :(得分:0)

您可以尝试映射文件(我用它们来重新定义“外部”实体的某些属性)

的persistence.xml

<persistence-unit name="yourPersistenceUnit" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
  <mapping-file>META-INF/orm-custom.xml</mapping-file>

ORM-custom.xml

<?xml version="1.0" encoding="UTF-8"?>
<entity-mappings
        xmlns="http://java.sun.com/xml/ns/persistence/orm"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm orm_2_0.xsd"
        version="2.0">

    <entity class="your.Entity">
        <attributes>
            <one-to-many name="attributes" fetch="LAZY"/>
            <one-to-many name="answers" fetch="LAZY"/>
        </attributes>
    </entity>
</entity-mappings>