我有以下结构:
@MappedSuperclass
public class AbstractEntitySequence {
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator="X?" )
@SequenceGenerator( allocationSize = 1, name = "X?" )
@Column( name = "id", nullable = false, unique = true )
private Long id;
和子实体:
@Entity
@Table( name = "BANK" )
public class Bank extends AbstractEntitySequence implements Serializable {
...
}
和其他实体。
我需要每个实体都有一个序列生成器名称,但我不想将其放置在每个实体中(如 ^C ^V) 是否有可能以某种方式从子类初始化抽象类中的注释(替换 X1)?
最佳答案
您可以定义@SequenceGenerator仅在一个实体类中,它将可用于所有实体,因为根据 JavaDoc:
The scope of the generator name is global to the persistence unit (across all generator types).
因此,您的映射应如下所示:
@Entity
@SequenceGenerator(name="X", allocationSize=1)
public class Bank {
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="X")
@Id long id;
}
@Entity
public class InsuranceCompany {
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="X")
@Id long id;
}
关于java 如何动态初始化Annotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28916371/