与 SINGLE_TABLE 继承的子类的关系

Relationship with a subclass of SINGLE_TABLE inheritance

我将一些 hbm 配置翻译成带注释的 java class。在 hbm 中,一些 classes 是使用继承策略 "SINGLE_TABLE" 定义的,而其他一些实体通过多对一关系将其称为 Map。

当我尝试启动应用程序时出现以下错误:
Caused by: org.hibernate.AnnotationException: Map key property not found: com.package.MyClass.Id

我在网上搜索了一些解释,但没有同时描述 SINGLE_TABLE 继承策略和这种情况下的 OneToMany 行为。

我有 parent class 如下:

@Entity
@Table(name = "parentclass")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", length = 10, discriminatorType = DiscriminatorType.INTEGER)
@DiscriminatorValue("100")
public abstract class ParentClass {

  @Id
  @Column(name = "Id", length = 11)
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;

  ....
}

child class :

@Entity
@DiscriminatorValue("2")
public abstract class ChildClass {

  ....
}

class 与关系:

@Entity
@Table(name = "otherclass")
@PrimaryKeyJoinColumn(name = "IdSys")
public class OtherClass extends OtherParent {

  ....

    @OneToMany
    @JoinColumn(name = "IdOther")
    @MapKey(name = "Id")
    @Where(clause = "type = 2")
    private Map<String, ChildClass> childClassMap;

  ....
}

它在 hbm 中定​​义时有效,所以我想它应该与注释一起使用。

我终于找到问题所在了。

在hbm文件中,MapKey名称指的是列名。但是注释引用了字段名称。

所以不用

@MapKey(name = "Id")

我一定要

@MapKey(name = "id")