我必须使用一个奇怪的数据库结构,在生产中可能有更多表但具有相同的列。我们希望通过实体/JPQL 查询使用 JPA (Hibernate) 来处理它们。这个想法是在运行时在 JVM 中为每个表动态创建实体类。这种方法效果很好。我使用 Javassist 创建实体 - 使用已经存在的编译实体,并动态添加注释(实体、表):
public Class generateGroupStateEntity(String className,String tableName) {
ClassPool cp = ClassPool.getDefault();
InputStream is = new FileInputStream(new File("MyTemplateEntity.class"));
CtClass c = cp.makeClass(is);
c.setName(className);
ClassFile cf = c.getClassFile();
ConstPool constPool = cf.getConstPool();
AnnotationsAttribute annotAtr = new AnnotationsAttribute(constPool,AnnotationsAttribute.visibleTag);
Annotation annot = new Annotation("javax.persistence.Entity", constPool);
annotAtr.addAnnotation(annot);
Annotation annotTable = new Annotation("javax.persistence.Table", constPool);
annotTable.addMemberValue("name", new StringMemberValue(tableName,cf.getConstPool()));
annotAtr.addAnnotation(annotTable);
cf.addAttribute(annotAtr);
return c.toClass();
}
当我从一个配置对象手动构建一个 Hibernate SessionFactory 时,使用 addAnnotatedClass 方法将这些类添加到配置中,它工作正常。问题在于自动发现,当我在 spring 上下文中使用包扫描时,JPA 或 AnnotationSessionFactoryBean 都没有发现/找到实体。(我得到:“QuerySyntaxException:[MyEntity] 未映射”异常)
Class.forName(myNewEntityName) 有效,这意味着它在类加载器中被初始化。
(当然我知道这不是处理这些表格的理想设计模式。)
问题是为什么以及如何鞋底?
(Javassist 3.15.0-GA,hibernate core 3.6.6.Final,hibernate-entitymanager 3.5.6-Final)