下面列出了org.hibernate.metadata.ClassMetadata#getPropertyNames ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 将更新对象拷贝至实体对象,并处理many-to-one的更新。
*
* @param updater
* @param po
*/
private void updaterCopyToPersistentObject(Updater<T> updater, T po,
ClassMetadata cm) {
String[] propNames = cm.getPropertyNames();
String identifierName = cm.getIdentifierPropertyName();
T bean = updater.getBean();
Object value;
for (String propName : propNames) {
if (propName.equals(identifierName)) {
continue;
}
try {
value = MyBeanUtils.getSimpleProperty(bean, propName);
if (!updater.isUpdate(propName, value)) {
continue;
}
cm.setPropertyValue(po, propName, value, POJO);
} catch (Exception e) {
throw new RuntimeException(
"copy property to persistent object failed: '"
+ propName + "'", e);
}
}
}
/**
* Construct a entity collection.
*
* @param parentMetadata parent meta data
* @param childMetadata child meta data
* @param parent parent object
* @param objects child objects
*/
public HibernateEntityCollection(ClassMetadata parentMetadata, ClassMetadata childMetadata, Object parent,
Collection<?> objects) {
this.objects = objects;
int i = 0;
for (Type type : childMetadata.getPropertyTypes()) {
if (type instanceof ManyToOneType) {
ManyToOneType mto = (ManyToOneType) type;
if (mto.getAssociatedEntityName().equals(parentMetadata.getEntityName())) {
parentName = childMetadata.getPropertyNames()[i];
}
}
i++;
}
this.metadata = childMetadata;
this.parent = parent;
}
/**
* @param entity an actual entity object, not a proxy!
*/
public String toString(Object entity, EntityMode entityMode) throws HibernateException {
// todo : this call will not work for anything other than pojos!
ClassMetadata cm = factory.getClassMetadata( entity.getClass() );
if ( cm==null ) return entity.getClass().getName();
Map result = new HashMap();
if ( cm.hasIdentifierProperty() ) {
result.put(
cm.getIdentifierPropertyName(),
cm.getIdentifierType().toLoggableString( cm.getIdentifier( entity, entityMode ), factory )
);
}
Type[] types = cm.getPropertyTypes();
String[] names = cm.getPropertyNames();
Object[] values = cm.getPropertyValues( entity, entityMode );
for ( int i=0; i<types.length; i++ ) {
if ( !names[i].startsWith("_") ) {
String strValue = values[i]==LazyPropertyInitializer.UNFETCHED_PROPERTY ?
values[i].toString() :
types[i].toLoggableString( values[i], factory );
result.put( names[i], strValue );
}
}
return cm.getEntityName() + result.toString();
}
/**
* 获得该类的属性和类型
*
* @param entityName
* 注解的实体类
*/
private <T> void getProperty(Class entityName) {
ClassMetadata cm = sessionFactory.getClassMetadata(entityName);
String[] str = cm.getPropertyNames(); // 获得该类所有的属性名称
for (int i = 0; i < str.length; i++) {
String property = str[i];
String type = cm.getPropertyType(property).getName(); // 获得该名称的类型
org.jeecgframework.core.util.LogUtil.info(property + "--->" + type);
}
}
/**
* 获得该类的属性和类型
*
* @param entityName
* 注解的实体类
*/
private <T> void getProperty(Class entityName) {
ClassMetadata cm = sessionFactory.getClassMetadata(entityName);
String[] str = cm.getPropertyNames(); // 获得该类所有的属性名称
for (int i = 0; i < str.length; i++) {
String property = str[i];
String type = cm.getPropertyType(property).getName(); // 获得该名称的类型
org.jeecgframework.core.util.LogUtil.info(property + "--->" + type);
}
}
/**
* Contructor to be used by all child classes to create a new instance
* and get the name right.
*
* @param clazz the Grails class
* @param sessionFactory The Hibernate SessionFactory instance
* @param metaData The ClassMetaData for this class retrieved from the SF
* @param defaultConstraints The default global constraints definition
*/
public GrailsHibernateDomainClass(Class<?> clazz, SessionFactory sessionFactory, GrailsApplication application, ClassMetadata metaData, Map<String, Object> defaultConstraints) {
super(clazz, "");
this.application = application;
new StandardAnnotationMetadata(clazz);
String ident = metaData.getIdentifierPropertyName();
this.defaultConstraints = defaultConstraints;
if (ident != null) {
Class<?> identType = getPropertyType(ident);
this.identifier = new GrailsHibernateDomainClassProperty(this, ident);
this.identifier.setIdentity(true);
this.identifier.setType(identType);
this.propertyMap.put(ident, this.identifier);
}
// configure the version property
final int versionIndex = metaData.getVersionProperty();
String versionPropertyName = null;
if (versionIndex > -1) {
versionPropertyName = metaData.getPropertyNames()[versionIndex];
this.version = new GrailsHibernateDomainClassProperty(this, versionPropertyName);
this.version.setType(getPropertyType(versionPropertyName));
}
// configure remaining properties
String[] propertyNames = metaData.getPropertyNames();
boolean[] propertyNullablility = metaData.getPropertyNullability();
for (int i = 0; i < propertyNames.length; i++) {
String propertyName = propertyNames[i];
if (!propertyName.equals(ident) && !(versionPropertyName != null && propertyName.equals(versionPropertyName))) {
GrailsHibernateDomainClassProperty prop = new GrailsHibernateDomainClassProperty(this, propertyName);
prop.setType(getPropertyType(propertyName));
Type hibernateType = metaData.getPropertyType(propertyName);
// if its an association type
if (hibernateType.isAssociationType()) {
prop.setAssociation(true);
// get the associated type from the session factory and set it on the property
AssociationType assType = (AssociationType) hibernateType;
if (assType instanceof AnyType) {
continue;
}
try {
String associatedEntity = assType.getAssociatedEntityName((SessionFactoryImplementor) sessionFactory);
ClassMetadata associatedMetaData = sessionFactory.getClassMetadata(associatedEntity);
prop.setRelatedClassType(associatedMetaData.getMappedClass(EntityMode.POJO));
} catch (MappingException me) {
// other side must be a value object
if (hibernateType.isCollectionType()) {
prop.setRelatedClassType(Collection.class);
}
}
// configure type of relationship
if (hibernateType.isCollectionType()) {
prop.setOneToMany(true);
} else if (hibernateType.isEntityType()) {
prop.setManyToOne(true);
// might not really be true, but for our purposes this is ok
prop.setOneToOne(true);
}
prop.setOptional(propertyNullablility[i]);
}
this.propertyMap.put(propertyName, prop);
}
}
this.properties = this.propertyMap.values().toArray(new GrailsDomainClassProperty[this.propertyMap.size()]);
// process the constraints
evaluateConstraints();
}