我将一些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中定义时起作用,所以我想它应该与注释一起起作用。
我终于找出问题所在。
在hbm文件中,MapKey名称是指列名称。但是注释指的是字段名称。
所以代替
@MapKey(name = "Id")
我必须有
@MapKey(name = "id")