如何在hibernate中推广模型类?

时间:2016-10-16 11:55:28

标签: java hibernate spring-mvc

我有使用mysql数据库和hibernate框架的spring mvc应用程序.II有4个主要类。单个主类将包含9个其他类,包括3到4个一对多关系。现在我的4个主要类是相同但是用不同的类名并保存在数据库中。虽然所有四个主类具有相同的属性,但我无法重新使用代码。有没有办法可以概括模型类,以便我可以重用代码。

1 个答案:

答案 0 :(得分:1)

@MappedSuperclass
public abstract class BaseEntity {

    //common attributes, getters/setters, whatever
}

@Entity
@Table(name = "firstTable")
public class ConcreteEntity extends BaseEntity {

    //anything specific to this class, if at all. Can be completely empty
}

@Entity
@Table(name = "secondTable")
public class AnotherConcreteEntity extends BaseEntity {

    //anything specific to this class, if at all. Can be completely empty
}