Java 谷歌反射:什么时候应该使用反射库?以及如何从表元数据创建动态POJO?
我是一个冬眠学习者。当我研究如何在只提供表和模式名的情况下创建动态POJO类时,我开始了解google reflections库Java 谷歌反射:什么时候应该使用反射库?以及如何从表元数据创建动态POJO?,java,hibernate,annotations,reflections,Java,Hibernate,Annotations,Reflections,我是一个冬眠学习者。当我研究如何在只提供表和模式名的情况下创建动态POJO类时,我开始了解google reflections库 try { sessionFactory = new StringUtil().getSessionFactory(className); session = sessionFactory.openSession(); sessionImp = (SessionImpl) session; Syste
try {
sessionFactory = new StringUtil().getSessionFactory(className);
session = sessionFactory.openSession();
sessionImp = (SessionImpl) session;
System.out.println("Product name ["
+ sessionImp.connection().getMetaData()
.getDatabaseProductName() + "] table name "+tableName);
metadata = sessionImp.connection().getMetaData();
tables = metadata.getTables(null, "SDP", null,
new String[] { "TABLE" });
// tables = metadata.getTables( null, "public", null, new
// String[]{"TABLE"} );//Postgres
ResultSet rs = metadata.getExportedKeys("SDP", "SDP", tableName);
while (rs.next()) {
PojoGenerator.pkey = rs.getString("PKCOLUMN_NAME");
}
System.out.println("Primary key of Table is ["+PojoGenerator.pkey+"]");
while (tables.next()) {
tName = tables.getString(3);
if (tName.equalsIgnoreCase(tableName)) {
columns = metadata.getColumns("SDP", "SDP", tName, null);
// columns = metadata.getColumns("sdp", "public", tName,
// null);//Postgres
while (columns.next()) {
tableMetadata = new TableMetadata();
tableMetadata.setColumnName(columns.getString("COLUMN_NAME"));
tableMetadata.setPropertyName(new StringUtil().getWordCaseNames(columns.getString("COLUMN_NAME")));
//tableMetadata.setRealColumnName( columns.getString("COLUMN_NAME") );
tableMetadata.setColumnType(new StringUtil().dbToJavaDataType(
columns.getString("TYPE_NAME"),
columns.getInt("COLUMN_SIZE"),
columns.getString("DECIMAL_DIGITS")));
tableMetadata.setColumnLength(columns.getInt("COLUMN_SIZE"));
tableMetadata.setSession(session);
list.add(tableMetadata);
}
columns = null;
tName = null;
}
}
} catch (Exception e) {
e.printStackTrace();
}
此元数据帮助我生成POJO:
public static Class generate(String className, Map<String, Class<?>> properties, List tableDetails) throws NotFoundException, CannotCompileException {
boolean needToCreateClass = false;
Object obj = null;
Class<?> clazz = null;
CtMethod getterMethod = null;
TableMetadata tableMetadata = null;
int numberOfColumnsInTable = 0;
try {
clazz = Class.forName(className);
obj = clazz.newInstance();
if ( obj != null ){
needToCreateClass = false;
}else{
needToCreateClass = true;
}
}catch (Exception e){
needToCreateClass = true;
}
System.out.println("Need to create a class ["+needToCreateClass+"]");
if ( needToCreateClass ) {
ClassPool pool = ClassPool.getDefault();
CtClass cc = pool.makeClass(className);
// add this to define a super class to extend
// cc.setSuperclass(resolveCtClass(MySuperClass.class));
// add this to define an interface to implement
cc.addInterface(resolveCtClass(Serializable.class));
ClassFile cf = cc.getClassFile();
ConstPool constpool = cf.getConstPool();
AnnotationsAttribute attrTable = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
AnnotationsAttribute attrColumn = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
Annotation [] tableAnnotations = new Annotation[]{new Annotation("javax.persistence.Entity", constpool), new Annotation("javax.persistence.Table", constpool) };
for (Entry<String, Class<?>> entry : properties.entrySet()) {
cc.addField(new CtField(resolveCtClass(entry.getValue()), entry.getKey(), cc));
// System.out.println("Key is ["+entry.getKey()+"]");
getterMethod = generateGetter(cc, entry.getKey(), entry.getValue());
// add getter
cc.addMethod(getterMethod);
if ( numberOfColumnsInTable == 0 ) {
tableAnnotations[1].addMemberValue("name", new StringMemberValue("CRBT_RBT", constpool));
tableAnnotations[1].addMemberValue("schema", new StringMemberValue("SDP", constpool));
attrTable.setAnnotations(tableAnnotations);
cc.getClassFile().addAttribute(attrTable);
attrTable = null;
}
attrColumn = new AnnotationsAttribute(constpool, AnnotationsAttribute.visibleTag);
Annotation annot1[] = {new Annotation("javax.persistence.Id", constpool),new Annotation("javax.persistence.Column", constpool)};
Annotation annot2[] = {new Annotation("javax.persistence.Column", constpool)};
tableMetadata = (TableMetadata)tableDetails.get( numberOfColumnsInTable );
if ( numberOfColumnsInTable < tableDetails.size() ) {
String realColumnName=new StringUtil().getRealName( entry.getKey());
if(realColumnName.equalsIgnoreCase(pkey)){
annot1[1].addMemberValue("name", new StringMemberValue( realColumnName , constpool ) );
attrColumn.setAnnotations(annot1);
}else{
annot2[0].addMemberValue("name", new StringMemberValue( realColumnName , constpool ) );
attrColumn.setAnnotations(annot2);
}
numberOfColumnsInTable++;
}
getterMethod.getMethodInfo().addAttribute(attrColumn);
// add setter
cc.addMethod(generateSetter(cc, entry.getKey(), entry.getValue()));
}
try {
cc.writeFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*
Validation for applied annotations
*/
try {
Object[] all = cc.getAnnotations();
int k = 0;
for (Object object : all) {
System.out.println(all[k]);
k++;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
/* Validation End */
return cc.toClass();
}else{
return clazz;
}
}
publicstaticclass-generate(字符串className,Map>entry:properties.entrySet()){
cc.addField(新的CtField(resolveCtClass(entry.getValue()),entry.getKey(),cc));
//System.out.println(“键为[“+entry.getKey()+”]);
getterMethod=generateGetter(cc,entry.getKey(),entry.getValue());
//添加吸气剂
cc.addMethod(getterMethod);
if(numberOfColumnsInTable==0){
tableAnnotations[1]。addMemberValue(“名称”,新StringMemberValue(“CRBT_RBT”,constpool));
tableAnnotations[1].addMemberValue(“模式”,新StringMemberValue(“SDP”,constpool));
attratable.setAnnotations(tableAnnotations);
cc.getClassFile().addAttribute(属性表);
attrTable=null;
}
attrColumn=新的AnnotationsAttribute(constpool,AnnotationsAttribute.visibleTag);
注释annot1[]={新注释(“javax.persistence.Id”,constpool),新注释(“javax.persistence.Column”,constpool)};
注解annot2[]={newannotation(“javax.persistence.Column”,constpool)};
tableMetadata=(tableMetadata)tableDetails.get(numberOfColumnsInTable);
if(numberOfColumnsTable
但是现在我需要用hibernate配置映射那些动态创建的POJO。所以我的问题来了:
1.我可以用图书馆吗?
2.怎么用?
3.如果没有,除了为每个类创建.hbm.xml文件之外,还有其他解决方案吗
请帮忙。提前谢谢
更新:
我有一个想法,但我不知道它将如何运作。
想法:
我将为每个类动态创建.hbm.xml文件,并将这些类的包映射到hibernate.cfg.xml中。
但是
1.我不知道如何通过包映射扫描映射文件。
2.我甚至不知道这是否可能,如果可能的话,怎么办?@v.ladynev请看一看。@v.ladynev请看一看。