【问题标题】:Associations on alternate keys using annotations使用注释的备用键关联
【发布时间】:2016-01-30 20:41:23
【问题描述】:

我正在尝试使用 JPA Annotations for Hibernate 实现以下关联:

 <class name="Person">
    <id name="id">
        <generator class="hilo"/>
    </id>

    <set name="accounts" inverse="true">
        <key column="userId" property-ref="userId"/>
        <one-to-many class="Account"/>
    </set>

    <property name="userId"/>
</class>

<class name="Account">
    <id name="accountId" length="32">
        <generator class="uuid"/>
    </id>

    <property name="userId"/>
</class>

或者,我需要不基于主键进行带注释的关联。

有什么想法可以实现吗?

示例取自Hibernate Associations on alternate keys

【问题讨论】:

    标签: java hibernate jpa annotations


    【解决方案1】:

    经过不断的搜索,我找到了以下两个给我答案的链接:

    https://hibernate.atlassian.net/browse/HHH-4479

    并且 Hibernate @JoinFormula

    对我来说是这样的:

    @OneToMany
    @JoinColumnsOrFormulas({
    @JoinColumnOrFormula(column = @JoinColumn(name = "userId", referencedColumnName="userId"))
    })
    private Set<Account> accounts; 
    

    【讨论】:

      猜你喜欢
      • 2021-02-09
      • 1970-01-01
      • 2021-05-26
      • 2011-10-18
      • 1970-01-01
      • 2012-03-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      相关资源
      最近更新 更多