【问题标题】:Relationship with a subclass of SINGLE_TABLE inheritance与 SINGLE_TABLE 继承的子类的关系
【发布时间】:2020-01-31 19:33:08
【问题描述】:

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

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

我在网上搜索了一些解释,但没有同时描述 SINGLE_TABLE 继承策略和本例中的 OneToMany 行为。

我的父类如下:

@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;

  ....
}

子类:

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

  ....
}

有关系的类:

@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 中定​​义时有效,所以我想它应该与注释一起工作。

【问题讨论】:

  • 你的例子完整吗?我看不到 OtherClass/OtherParent 和 ChildClass/ParentClass 之间的任何关系
  • 抱歉,我忘记在 OtherClass 中重命名地图了。现在你可以看到关系了
  • 我试图弄清楚发生了什么,但我很难,因为这不是一个完整的例子。有很多成员/标识符的定义缺失:“IdSys ”、“IdOther”、“OtherParent”等

标签: java mysql hibernate jpa jakarta-ee


【解决方案1】:

我终于找出问题所在了。

在 hbm 文件中,MapKey 名称是指列名。但注解指的是字段名。

所以不是

@MapKey(name = "Id")

我必须有

@MapKey(name = "id")

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多