Hibernate的元数据(不需要java类,动态建表,存储,读取)

http://blog.csdn.net/fengxinyixiao/article/details/11684521
Hibernate提供了ClassMetadata接口、CollectionMetadata接口和Type层次体系来访问元数据。可以通过SessionFactory获取元数据接口的实例。
ClassMetadata catMeta = sessionfactory.getClassMetadata(Cat.class);
Object[] propertyValues = catMeta.getPropertyValues(fritz);
String[] propertyNames = catMeta.getPropertyNames();
Type[] propertyTypes = catMeta.getPropertyTypes();
Map namedValues = new HashMap();
for (int i = 0; i < propertyNames.length; i++)
{
if (!propertyTypes[i].isEntityType()
&& !propertyTypes[i].isCollectionType())
{
namedValues.put(propertyNames[i], propertyValues[i]);
}
}

通过将持久化对象的类作为参数调用SessionFactory的getClassMetadata方法就可以得到关于此对象的所有元数据信息的接口ClassMetadata。下面是ClassMetadata接口的主要方法说明。
l public String getEntityName():获取实体名称。
l public String getIdentifierPropertyName():得到主键的名称。
l public String[] getPropertyNames():得到所有属性名称(不包括主键)。
l public Type getIdentifierType():得到主键的类型。
l public Type[] getPropertyTypes():得到所有属性的类型(不包括主键)。
l public Type getPropertyType(String propertyName):得到指定属性的类型。
l public boolean isVersioned():实体是否是版本化的。
l public int getVersionProperty():得到版本属性。
l public boolean[] getPropertyNullability():得到所有属性的“是否允许为空”属性。
l public boolean[] getPropertyLaziness():得到所有属性的“是否LazyLoad”属性。
l public boolean hasIdentifierProperty():实体是否有主键字段。
l public boolean hasSubclasses():是否有子类。
l public boolean isInherited():是否是子类。
ClassMetadata 接口有getPropertyTypes()、getPropertyNullability()这样平面化的访问所有字段属性的方法,这些方法是供 Hibernate内部实现用的,在外部使用的时候我们常常需要深入每个属性的内部,这样借助于getPropertyNames()、 getPropertyType(String propertyName)两个方法就可以满足要求了。
ClassMetadata entityMetaInfo = sessionFactory
.getClassMetadata(destClass);
String[] propertyNames = entityMetaInfo.getPropertyNames();
for (int i = 0, n = propertyNames.length; i < n; i++)
{
String propertyName = propertyNames[i];
Type propType = entityMetaInfo.getPropertyType(propertyName);
…
}

getPropertyType(String propertyName)方法返回的类型为Type,这个类型包含了字段的元数据信息。Type接口只是一个父接口,它有很多子接口和实现类.


Hibernate中的集合类型的基类是 CollectionType,其子类分别对应着数组类型(ArrayType)、Bag类型(BagType)、List类型(ListType)、 Map类型(MapType)、Set类型(SetType)。而“多对一”和“一对一”类型分别为ManyToOneType和 OneToOneType,它们的基类为EntityType。BigDecimal、Boolean、String、Date等类型则属于 NullableType的直接或者间接子类。
Type接口的主要方法列举如下。
l public boolean isAssociationType():此类型是否可以转型为AssociationType,并不表示此属性是关联属性。
l public boolean isCollectionType():是否是集合类型。
l public boolean isComponentType():是否是Component类型,如果是的话必须能转型为AbstractComponentType类型。
l public boolean isEntityType():是否是实体类型。
l public boolean isAnyType():是否是Any类型。
l public int[] sqlTypes(Mapping mapping):取得实体各个字段的SQL类型,返回值的类型遵守java.sql.Types中的定义。
l public Class getReturnedClass():返回值类型。
l public String getName():返回类型名称。
【例10.4】Hibernate元数据接口调用。
package com.cownew.Char15;
import org.hibernate.SessionFactory;
import org.hibernate.metadata.ClassMetadata;
import org.hibernate.type.Type;
import com.cownew.PIS.base.permission.common.UserInfo;
import com.cownew.PIS.framework.bizLayer.hibernate.HibernateConfig;
public class HibernateMetaTest
{
public static void main(String[] args)
{
SessionFactory sessionFactory =
HibernateConfig.getSessionFactory();
ClassMetadata entityMetaInfo = sessionFactory
.getClassMetadata(UserInfo.class);
String[] propertyNames = entityMetaInfo.getPropertyNames();
for (int i = 0, n = propertyNames.length; i < n; i++)
{
String propertyName = propertyNames[i];
Type propType = entityMetaInfo.getPropertyType(propertyName);
System.out.println(propertyName + "字段类型为"
+ propType.getReturnedClass().getName());
}
if (entityMetaInfo.hasIdentifierProperty())
{
String idPropName = entityMetaInfo.getIdentifierPropertyName();
Type idPropType = entityMetaInfo.getIdentifierType();
System.out.println("主键字段为:" + idPropName + "类型为"
+ idPropType.getReturnedClass().getName());
} else
{
System.out.println("此实体无主键");
}
}
}

运行结果:
number字段类型为java.lang.String
password字段类型为java.lang.String
person字段类型为com.cownew.PIS.basedata.common.PersonInfo
permissions字段类型为java.util.Set
isSuperAdmin字段类型为java.lang.Boolean
isFreezed字段类型为java.lang.Boolean
主键字段为:id


在位置类的情况下遍历所有元数据

//获得所有元数据
Map<String, ClassMetadata> map= sessionFactory.getAllClassMetadata();
//遍历
for (String key : map.keySet()) {       
                       //获得某个元数据
        AbstractEntityPersister cmd=(AbstractEntityPersister)map.get(key);
                        //获得元数据的信息
    String tbName= cmd.getTableName();
    String[] cols= cmd.getIdentifierColumnNames();
    String pkName="";
    if(cols.length>0){
    pkName=cols[0];
    }
    //遍历类的信息
    Class c=Class.forName(key);
        Field[] fields= c.getDeclaredFields();
        for (Field field : fields) {
        if(field.getName().equalsIgnoreCase(pkName.replace("_", ""))){
        String fType= field.getType().getSimpleName();
        if(!fType.equals("String")){
        tableInfo.put(tbName, pkName);
        }
        break;
        }
}   
   
    }

猜你喜欢

转载自panyongzheng.iteye.com/blog/2038191
今日推荐