首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Teneo忽略mappedBy中的OneToMany并创建ForeignKey

Teneo忽略mappedBy中的OneToMany并创建ForeignKey
EN

Stack Overflow用户
提问于 2014-07-01 23:30:41
回答 1查看 355关注 0票数 0

我试图遵循这个例子,并使用Teneo在一个单独的表中创建多到多的关系,其中包含复合键和其他列。我的模型在hibernate中,这就是ecore文件对于模型的一部分所喜欢的:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<ecore:EPackage xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ecore="http://www.eclipse.org/emf/2002/Ecore" name="my" nsURI="test.my" nsPrefix="test">
  <eClassifiers xsi:type="ecore:EClass" name="Resource">
    <eAnnotations source="teneo.jpa">
      <details key="appinfo" value="@Table(uniqueConstraints={@UniqueConstraint(columnNames={&quot;ip&quot;,&quot;name&quot;, &quot;revision&quot;,  &quot;type&quot;})})"/>
    </eAnnotations>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="type" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
        iD="true">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@Id&#xD;&#xA;    @GeneratedValue(strategy=GenerationType.AUTO)"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EReference" name="resourceTags" ordered="false"
        upperBound="-1" eType="#//ResourceTag" resolveProxies="false" eOpposite="#//ResourceTag/resource">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@OneToMany ( indexed = false, fetch=FetchType.EAGER, mappedBy = &quot;resourceId&quot;,  cascade=CascadeType.ALL)"/>
      </eAnnotations>
    </eStructuralFeatures>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="Tag">
    <eAnnotations source="teneo.jpa">
      <details key="appinfo" value="@Table(uniqueConstraints={@UniqueConstraint(columnNames={ &quot;name&quot;})})&#xD;&#xA;@NoAuditing"/>
    </eAnnotations>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="name" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="id" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EInt"
        iD="true">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@Id&#xD;&#xA;    @GeneratedValue(strategy=GenerationType.AUTO)"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EReference" name="resourceTags" ordered="false"
        upperBound="-1" eType="#//ResourceTag" resolveProxies="false" eOpposite="#//ResourceTag/tag">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@OneToMany (indexed=false, fetch=FetchType.EAGER, mappedBy = &quot;pk.tagid&quot;, cascade=CascadeType.ALL)"/>
      </eAnnotations>
    </eStructuralFeatures>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="ResourceTag">
    <eAnnotations source="teneo.jpa">
      <details key="appinfo" value="@Table(name = &quot;ResourceTag&quot;)&#xD;&#xA;"/>
    </eAnnotations>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="date" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EDate"/>
    <eStructuralFeatures xsi:type="ecore:EReference" name="pk" eType="#//ResourceTagId"
        resolveProxies="false">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@EmbeddedId"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EAttribute" name="comment" eType="ecore:EDataType http://www.eclipse.org/emf/2002/Ecore#//EString"/>
    <eStructuralFeatures xsi:type="ecore:EReference" name="tag" eType="#//Tag" eOpposite="#//Tag/resourceTags">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@Transient"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EReference" name="resource" eType="#//Resource"
        eOpposite="#//Resource/resourceTags">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@Transient"/>
      </eAnnotations>
    </eStructuralFeatures>
  </eClassifiers>
  <eClassifiers xsi:type="ecore:EClass" name="ResourceTagId">
    <eAnnotations source="teneo.jpa">
      <details key="appinfo" value="@Embeddable"/>
    </eAnnotations>
    <eStructuralFeatures xsi:type="ecore:EReference" name="tag" eType="#//Tag">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@ManyToOne&#xD;&#xA;@JoinColumns({&#xD;&#xA;&#x9;@JoinColumn(name=&quot;tagId&quot;, referencedColumnName = &quot;id&quot;)&#xD;&#xA;})"/>
      </eAnnotations>
    </eStructuralFeatures>
    <eStructuralFeatures xsi:type="ecore:EReference" name="resource" eType="#//Resource">
      <eAnnotations source="teneo.jpa">
        <details key="appinfo" value="@ManyToOne&#xD;&#xA;@JoinColumns({&#xD;&#xA;&#x9;@JoinColumn(name=&quot;resourceId&quot;, referencedColumnName = &quot;id&quot;)&#xD;&#xA;})"/>
      </eAnnotations>
    </eStructuralFeatures>
  </eClassifiers>
</ecore:EPackage>

此模型的生成映射为

代码语言:javascript
复制
<?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 auto-import="false">
    <typedef name="model.EIllegalStateException" class="org.eclipse.emf.teneo.hibernate.mapping.DefaultToStringUserType">
        <param name="epackage">http://www.qualcomm.com/qti/qsip/designbook/model</param>
        <param name="edatatype">EIllegalStateException</param>
    </typedef>
    <class name="test.my.ResourceImpl" entity-name="Resource" abstract="false" lazy="false" discriminator-value="Resource" table="`resource`">
        <meta attribute="eclassName" inherit="false">Resource</meta>
        <meta attribute="epackage" inherit="false">http://www.qualcomm.com/qti/qsip/designbook/model</meta>
        <id name="id" type="int" unsaved-value="0">
            <column not-null="true" unique="false" name="`id`"/>
            <generator class="native"/>
        </id>
        <discriminator type="string">
            <column name="`dtype`" index="resourcedtype" length="255" not-null="true"/>
        </discriminator>
        <version name="revision" type="int">
            <column not-null="true" unique="false" name="`revision`" unique-key="c0"/>
        </version>
        <property name="type" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
            <column not-null="false" unique="false" name="`type`" unique-key="c0"/>
        </property>
        <property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
            <column not-null="false" unique="false" name="`name`" unique-key="c0"/>
        </property>
        <bag name="resourceTags" inverse="true" lazy="false" cascade="all,delete-orphan">
            <key update="true">
                <column name="`resourcetag_resource_id`" unique="false"/>
            </key>
            <one-to-many entity-name="ResourceTag"/>
        </bag>
    </class>
    <class name="test.my.TagImpl" entity-name="Tag" abstract="false" lazy="false" discriminator-value="Tag" table="`tag`">
        <meta attribute="eclassName" inherit="false">Tag</meta>
        <meta attribute="epackage" inherit="false">http://www.qualcomm.com/qti/qsip/designbook/model</meta>
        <id name="id" type="int" unsaved-value="0">
            <column not-null="true" unique="false" name="`id`"/>
            <generator class="native"/>
        </id>
        <discriminator type="string">
            <column name="`dtype`" index="tagdtype" length="255" not-null="true"/>
        </discriminator>
        <property name="name" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
            <column not-null="false" unique="false" name="`name`" unique-key="c0"/>
        </property>
        <bag name="resourceTags" inverse="true" lazy="false" cascade="all,delete-orphan">
            <key update="true">
                <column name="`resourcetag_tag_id`" unique="false"/>
            </key>
            <one-to-many entity-name="ResourceTag"/>
        </bag>
    </class>
    <class name="test.my.ResourceTagImpl" entity-name="ResourceTag" abstract="false" lazy="false" discriminator-value="ResourceTag" table="`resourcetag`">
        <meta attribute="eclassName" inherit="false">ResourceTag</meta>
        <meta attribute="epackage" inherit="false">http://www.qualcomm.com/qti/qsip/designbook/model</meta>
        <composite-id name="pk" class="test.my.ResourceTagIdImpl" access="org.eclipse.emf.teneo.hibernate.mapping.property.EReferencePropertyHandler">
            <meta attribute="eclassName" inherit="false">ResourceTagId</meta>
            <meta attribute="epackage" inherit="false">http://www.qualcomm.com/qti/qsip/designbook/model</meta>
            <key-many-to-one name="tag" entity-name="Tag" lazy="false">
                <column not-null="false" unique="false" name="`tagid`"/>
            </key-many-to-one>
            <key-many-to-one name="resource" entity-name="Resource" lazy="false">
                <column not-null="false" unique="false" name="`resourceid`"/>
            </key-many-to-one>
        </composite-id>
        <discriminator type="string">
            <column name="`dtype`" index="ResourceTagdtype" length="255" not-null="true"/>
        </discriminator>
        <property name="date" lazy="false" insert="true" update="true" not-null="false" unique="false" type="timestamp">
            <column not-null="false" unique="false" name="`date`"/>
        </property>
        <property name="comment" lazy="false" insert="true" update="true" not-null="false" unique="false" type="java.lang.String">
            <column not-null="false" unique="false" name="`comment`"/>
        </property>
    </class>
</hibernate-mapping>

如您所见,从资源到oneToMany的映射是由一个新的外来密钥映射的,因此ResourceTag表有重复列resourcetag_tag_idresourcetag_resource_id,而不是复合主键'resourceId‘和'tagid’中的列。

我的hibernate perisitent选项如下:

代码语言:javascript
复制
props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_NON_CONTAINMENT,
                "PERSIST,REFRESH");
props.setProperty(PersistenceOptions.CASCADE_POLICY_ON_CONTAINMENT,
                "ALL");

props.setProperty(PersistenceOptions.MAP_ALL_LISTS_AS_IDBAG, "false");
props.setProperty(PersistenceOptions.ALWAYS_MAP_LIST_AS_BAG, "false");

props.setProperty(PersistenceOptions.ID_FEATURE_AS_PRIMARY_KEY, "false");
props.setProperty(PersistenceOptions.ALWAYS_VERSION, "false");
props.setProperty(PersistenceOptions.JOIN_TABLE_FOR_NON_CONTAINED_ASSOCIATIONS, "false");
props.setProperty(PersistenceOptions.SET_FOREIGN_KEY_NAME, "false");
props.setProperty(PersistenceOptions.FEATUREMAP_AS_COMPONENT, "true");

我在resource.resourceTags中的@OnetoMany属性中尝试了许多不同的组合,但是teneo/hibernate忽略了它们。正如示例中所解释的那样,我还尝试使用@AssociationOverride,但这对映射也没有任何影响。

我想要实现的是标签和资源之间的双向、多到多的关系,它有一些额外的属性。因此,当我加载资源时,我可以自动获得标记,当我加载标记时,我可以自动获得资源。但是,使用此映射,ResourceTag表中的额外外键列为null,因此不会获取该关系。

这是模型的一个子集,在我删除的这些类中包含了许多其他引用。许多配置都是基于其他需求定义的。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-16 02:56:44

实际上,使用joinColumn而不是mappedby解决了这个问题。你可以获得更多的信息这里

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24520714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档