【问题标题】:Hibernate annotation mapping to joined table with composite key休眠注释映射到具有复合键的连接表
【发布时间】:2023-03-24 00:50:02
【问题描述】:

我有一个实体,我想将 OneToOne 与具有复合键的表连接起来(省略 getter/setter):

@Entity
@Table(name = "parent")
public class Parent {
  @Id
  private String parentId;
  @Column(name = "data")
  private String data;
  @OneToOne
  private Child child;
}

还有:

@Entity
@IdClass(ChildKey.class)
@Table(name = "child")
public class Child{
  @Id
  private String parentId;
  @Id
  private String username;
  @Column(name = "data")
  private String childData;
}

public class ChildKey implements Serializable {
  private String parentId;
  private String username;
}

父级在子实体中没有“用户名”字段的概念。我需要将其作为标准传递。在数据库中,child 的主键在 parentId 和 username 上。

如果我没有指定 JoinColumn,hibernate 会尝试使用字段 child_username 和 child_parentId 进行映射。如果我只指定一个 Joincolumn,我会得到一个损坏的映射。如果我同时指定了两个 JoinColumns,我就没有要指定的父列。

如何映射此类并将用户名作为条件传递? (它来自身份验证数据)或者如果我偏离了轨道,我该如何以不同的方式做到这一点。

【问题讨论】:

    标签: java hibernate jpa


    【解决方案1】:

    我最终做的是在 Child 类上定义一个 @Filter,如下所示:

    @Entity
    @IdClass(ChildKey.class)
    @Table(name = "child")
    @FilterDef(name = "usernameFilter", parameters = {
            @ParamDef( name = "username", type="string")
    })
    public class Child { ... }
    

    在 Parent 类上,我用对过滤器的引用对集合进行了注释:

    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumn(name = "parentId")
    @Filter(name="usernameFilter", condition = "username = :username")
    private List<Child> children;
    

    最后,在我的 DAO 中,我按名称对过滤器进行了参数化,如下所示:

    Filter filter = currentSession().enableFilter("usernameFilter");
    filter.setParameter("username", user.getUsername());
    

    这样做会产生我想到的确切 SQL,这是 JOIN 条件中带有变量的附加子句:

    SELECT 
        ...
    FROM
        parent this_
            LEFT OUTER JOIN
        child child_ ON this_.parentId = child_.parentId
            AND child_.username = ?
    

    我可能不清楚我在最初的问题中寻找的最终结果。发布此答案以防对其他人有所帮助。

    【讨论】:

      【解决方案2】:

      您也许可以使用派生身份。

      Parent 类将保持不变;但是您可以指定一个 @OneToOne 映射回孩子的父级,Child 和 ChildKey 类看起来像这样:

      @Entity
      @IdClass(ChildKey.class)
      @Table(name = "child")
      public class Child{
        @Id
        @OneToOne(mappedBy="child")
        private Parent parent;
        @Id
        private String username;
        @Column(name = "data")
        private String childData;
      }
      
      public class ChildKey implements Serializable {
        private String parent; // name matches name of the @Id field and type matches type of Parent @Id field
        private String username; // name and type match those of the @Id field
      }
      

      派生身份在 JPA 2.1 规范第 2.4.1 节中讨论。

      【讨论】:

      • 感谢您的回答,但我看不出在运行时可以通过用户名限制哪些地方,请您解释一下吗?
      • 啊。我的错。有点晚了。 :) 所以:你所拥有的是父母和孩子之间的 OneToMany;但是您想将其限制为特定用户的单个子项。听起来给定的父级实际上也适用于特定的用户,因此应该使用用户名创建。你为什么说“父母在子实体中没有'用户名'字段的概念”,实际上它确实有?
      猜你喜欢
      • 1970-01-01
      • 2011-07-22
      • 1970-01-01
      • 2014-10-20
      • 1970-01-01
      • 2017-08-11
      • 2010-10-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多