我有 4 个类如下...
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Platform{
@Id
@Column(name = "PLATFORM_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer platformId;
...
@Entity
@Table(name = "ANDROID_PLATFORM")
public class AndroidPlatform extends Platform{
public AndroidPlatform(){
}
...
@MappedSuperclass
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Application{
@Id
@Column(name = "APPLICATION_ID")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer applicationId;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name="PLATFORM_ID")
private Platform platform;
...
@Entity
@Table(name = "ANDROID_APPLICATION")
public class AndroidApplication extends Application {
public AndroidApplication(){
}
...
当我开始我的项目时,它会抛出这样的错误
org.hibernate.AnnotationException: @OneToOne or @ManyToOne on com.***.model.AndroidApplication.platform references an unknown entity: com.***.model.Platform
...
我的类有什么问题,或者在 hibernate 中使用抽象类的最佳方法是什么?
请帮忙。
最佳答案
在您的抽象类 Application 中(以及通过 AndroidApplication 中的继承),您使用的是 private Platform platform;
。 Platform是一个抽象类,不能实例化,只有部分映射信息(具体表名缺失)。
Hibernate 需要一个无参数的构造函数来实例化加载的实体。它还需要知道要使用哪个类(不是抽象类,而是实现类),并且需要成员变量类型的所有映射信息。
解决方案:您必须在 Application 或 AndroidApplication 中将 private Platform platform
与 private AndroidPlatform platform
交换,并且此 AndroidPlatform 类型的变量必须具有 hibernate 注释。
第二个解决方案:如果您确实需要 Java 结构,那么您可以使用 xml 映射文件。您可以在每个子类中指定确切的类型,即使成员变量的 Java 类是该类型的父类(super class)也是如此。
https://stackoverflow.com/questions/10945014/
相关文章:
c# - "Unable to find an entry point named"在 c# 中使用
asp.net - 读取已在 ASP.NET httpmodule 中设置的经典 ASP 中的服务器
rest - 使用 Twitter 的 REST API 从收藏夹获取 faved_at 日期?
.net - String.GetHashCode() 的复杂性
jquery-ui - jQuery UI 日期选择器。显示一种格式,保存其他格式
svn - Hudson - SVN 错误 : org. tmatesoft.svn.core.SV