简体   繁体   English

休眠注释/ XML解决方法

[英]Hibernate Annotation/XML workaround

I am working on a project in hibernate where there is both annotations and xml mapped entities. 我正在休眠的项目中同时存在注释和xml映射的实体。

Here is the setup, Entity/Class A is annotation based. 这是设置,实体/类A基于注释。

Class A

@Id
long id;

I need a collection in class A, that can only be mapped through XML configurations. 我需要类A中的一个集合,该集合只能通过XML配置进行映射。 We cannot mix annotations and xml for a given entity I know. 我们无法为我知道的给定实体混合注释和xml。

But can I just create a wrapper class, call it X, and create a corresponding XML mapping for X with the collection xml configuration that I would have put in A? 但是我是否可以只创建一个包装器类,将其命名为X,并使用我将在A中放置的集合xml配置为X创建对应的XML映射? So we'd have: 所以我们有:

Class A

@Id
long id;

Object X;

And then also a X.hbm.xml file. 然后还有一个X.hbm.xml文件。 Note this file will contain mapping to formulate a collection of a column from say Table B, that is mapped by A's primary key which is a foreign key in B. So I guess it will actually have to reference Table A and B. 请注意,此文件将包含映射以制定表B中列的集合,该映射由A的主键映射,该主键是B中的外键。因此,我想它实际上必须引用表A和B。

Is this workaround going to work or not? 这种解决方法是否可行? Seems like a long shot... 好像是远射...

You may try mapping files (I used them to redefine some properties of an "external" entity) 您可以尝试映射文件(我使用它们来重新定义“外部”实体的某些属性)

persistence.xml 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 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM