简体   繁体   English

javax.persistence注释和继承

[英]javax.persistence annotations and inheritance

I have 4 persistent classes which all have the same fields (exactly) the only 3 difference between them is 1) the class name, 2) the table name and 3) the data. 我有4个持久化类,它们都具有相同的字段(确切地说),它们之间唯一的区别是1)类名,2)表名和3)数据。 i am aware that this might seem strange to some but trust me there is a good reason which i won't go into here. 我知道这对某些人来说可能有点奇怪,但相信我有一个很好的理由我不会进入这里。

now, i'm using hibernate annotations to configure my class which should work like so: 现在,我正在使用hibernate注释来配置我的类,它应该像这样工作:

@Entity
@Table(name = "store")
public class Store
{
 @Id
 @Column(name = "unique_id")
 protected String id;
 @Column
 protected String category;
 ...
}

.. and this does work, for a single stand-alone class, however there are many fields to map and i'd like to do it all in one hit for all four similar classes, ie: ..这确实有效,对于一个单独的类,但是有很多字段要映射,我想在所有四个相似的类中一次性完成所有这些,即:

public class StoreBase
{
 @Id
 @Column(name = "unique_id")
 protected String id;
 @Column
 protected String category;
 ...
}

@Entity
@Table(name = "store1")
public class Store1 extends StoreBase
{}

@Entity
@Table(name = "store2")
public class Store2 extends StoreBase
{}

@Entity
@Table(name = "store3")
public class Store3 extends StoreBase
{}

@Entity
@Table(name = "store4")
public class Store4 extends StoreBase
{}

however when attempting this i get the following exception: 但是在尝试这个时我得到以下异常:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: package.entities.Store1
 at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:672)
 at org.hibernate.cfg.AnnotationConfiguration.processArtifactsOfType(AnnotationConfiguration.java:546)
 at org.hibernate.cfg.AnnotationConfiguration.secondPassCompile(AnnotationConfiguration.java:291)
 at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1292)
 at org.hibernate.cfg.AnnotationConfiguration.buildSessionFactory(AnnotationConfiguration.java:867)

i'm guessing this is because the super class is not being searched for the identifier? 我猜这是因为没有搜索超类的标识符?

is there a way to utilise inheritance in this context? 有没有办法在这种情况下利用继承?

thanks, paul. 谢谢,保罗。

@MappedSuperclass
public class StoreBase

See docs for more info. 有关详细信息,请参阅文档

看看@MappedSuperclass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM