Java Code Examples for org.hibernate.metadata.ClassMetadata#getIdentifierPropertyName()

The following examples show how to use org.hibernate.metadata.ClassMetadata#getIdentifierPropertyName() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may want to check out the right sidebar which shows the related API usage.
Example 1
/**
 * 将更新对象拷贝至实体对象,并处理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);
		}
	}
}
 
Example 2
Source Project: lemon   File: HibernateBasicDao.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 取得对象的主键名,辅助函数.
 * 
 * @param entityClass
 *            实体类型
 * @return 主键名称
 */
public String getIdName(Class entityClass) {
    Assert.notNull(entityClass);
    entityClass = ReflectUtils.getOriginalClass(entityClass);

    ClassMetadata meta = this.getSessionFactory().getClassMetadata(
            entityClass);
    Assert.notNull(meta, "Class " + entityClass
            + " not define in hibernate session factory.");

    String idName = meta.getIdentifierPropertyName();
    Assert.hasText(idName, entityClass.getSimpleName()
            + " has no identifier property define.");

    return idName;
}
 
Example 3
@Override
public String getIdName() {
	ClassMetadata meta = getSessionFactory().getClassMetadata(entityClass);
	return meta.getIdentifierPropertyName();
}
 
Example 4
/**
 * 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();
}
 
Example 5
Source Project: jdal   File: HibernateUtils.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Gets the identifier property name of persistent object
 * 
 * @param sessionFactory the hibernate SessionFactory
 * @param obj the persistent object
 * @return the identifier property name
 */
public static String getIdentifierPropertyName(
		SessionFactory sessionFactory, Object obj) {
	
	ClassMetadata cm = getClassMetadata(sessionFactory, obj);

	return cm == null ? null : cm.getIdentifierPropertyName();
	
}
 
Example 6
/**
 * 取得对象的主键名.
 * 
 * @return String
 */
public String getIdName() {
	ClassMetadata meta = sessionFactory.getClassMetadata(entityClass);
	return meta.getIdentifierPropertyName();
}