Hibernate 注解 获得实体类属性和类型
package com.dragance.hrcrm.base;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
public class HibernateTable {
@Autowired
protected HibernateTemplate template; //注入template
/**
* 获得classMetadata 类
* @param entityName
* @return
*/
public ClassMetadata getClassMataDate(Class entityName) {
return getTemplate().getSessionFactory().getClassMetadata(entityName);
}
/**
* 获得该类的属性和类型
* @param entityName 注解的实体类
*/
public void testProperty(Class entityName) {
ClassMetadata cm = this.getClassMataDate(entityName);
String[] str = cm.getPropertyNames(); //获得该类所有的属性名称
for(int i=0; i<str.length; i++) {
String property = str[i];
String type = cm.getPropertyType(property).getName(); //获得该名称的类型
System.out.println(property+"--->"+type);
}
}
public HibernateTemplate getTemplate() {
return template;
}
public void setTemplate(HibernateTemplate template) {
this.template = template;
}
}