英文:
How to dynamically create sequence generators in JPA
问题
I want to reuse Counter by passing the sequence name order_sequence
dynamically to this class, how can this be achieved
我想通过动态地将序列名称 order_sequence
传递给这个类来重用 Counter,应该如何实现?
英文:
I want to reuse Counter by passing the sequence name order_sequence
dynamically to this class, how can this be achieved
public class Counter {
@Id
@GeneratedValue(generator = "sequence-generator")
@GenericGenerator(name = "sequence-generator", strategy = "org.hibernate.id.enhanced.SequenceStyleGenerator", parameters = {
@Parameter(name = "sequence_name", value = "order_sequence"),
@Parameter(name = "initial_value", value = "1"), @Parameter(name = "increment_size", value = "1") })
private Long counter;
}
答案1
得分: 1
The main idea could be override org.hibernate.id.enhanced.SequenceStyleGenerator
in the following way:
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
public @interface EntityAwareGeneratorParams {
String sequence();
}
public class EntityAwareGenerator extends SequenceStyleGenerator {
public static final String NAME = EntityAwareGenerator.class.getName();
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
String entityName = params.getProperty(ENTITY_NAME);
if (entityName == null) {
throw new IllegalStateException("Entity name must not be null");
}
Class<?> entityClass = serviceRegistry.requireService(ClassLoaderService.class)
.classForName(entityName);
EntityAwareGeneratorParams generatorParams = entityClass.getAnnotation(EntityAwareGeneratorParams.class);
if (generatorParams == null) {
throw new IllegalStateException(String.format(
"Annotation @%s is not present for class %s",
EntityAwareGeneratorParams.class.getName(),
entityClass.getName()
));
}
params.setProperty(SEQUENCE_PARAM, generatorParams.sequence());
super.configure(type, params, serviceRegistry);
}
}
MappedSuperClass:
@Id
@GeneratedValue(generator = "entity-aware-generator", strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "entity-aware-generator", strategy = EntityAwareGenerator.NAME, ...)
private Long id;
Child classes:
@Entity
@EntityAwareGeneratorParams(sequence="another sequence")
英文:
The main idea could be override org.hibernate.id.enhanced.SequenceStyleGenerator
in the following way:
@Target(TYPE)
@Retention(RUNTIME)
@Inherited
public @interface EntityAwareGeneratorParams {
String sequence();
}
public class EntityAwareGenerator extends SequenceStyleGenerator {
public static final String NAME = EntityAwareGenerator.class.getName();
@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
String entityName = params.getProperty(ENTITY_NAME);
if (entityName == null) {
throw new IllegalStateException("Entity name must not be null");
}
Class<?> entityClass = serviceRegistry.requireService(ClassLoaderService.class)
.classForName(entityName);
EntityAwareGeneratorParams generatorParams = entityClass.getAnnotation(EntityAwareGeneratorParams.class);
if (generatorParams == null) {
throw new IllegalStateException(String.format(
"Annotation @%s is not present for class %s",
EntityAwareGeneratorParams.class.getName(),
entityClass.getName()
));
}
params.setProperty(SEQUENCE_PARAM, generatorParams.sequence());
super.configure(type, params, serviceRegistry);
}
}
MappedSuperClass:
@Id
@GeneratedValue(generator = "entity-aware-generator", strategy = GenerationType.SEQUENCE)
@GenericGenerator(name = "entity-aware-generator", strategy = EntityAwareGenerator.NAME, ...)
private Long id;
Child classes:
@Entity
@EntityAwareGeneratorParams(sequence="another sequence")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论