本文整理汇总了Java中org.hibernate.metadata.ClassMetadata.getIdentifier方法的典型用法代码示例。如果您正苦于以下问题:Java ClassMetadata.getIdentifier方法的具体用法?Java ClassMetadata.getIdentifier怎么用?Java ClassMetadata.getIdentifier使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.hibernate.metadata.ClassMetadata
的用法示例。
在下文中一共展示了ClassMetadata.getIdentifier方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: _countRows
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
private TreeMap<String, Object> _countRows(Object value) {
ClassMetadata meta = getSessionFactory().getClassMetadata(value.getClass());
String idName = meta.getIdentifierPropertyName();
Serializable idValue = meta.getIdentifier(value, (SessionImplementor)getTmpSession());
ArrayList<String[]> fieldSets;
if(this._fields.length > 0){
fieldSets = _prepareFields();
}else{
fieldSets = _getFieldsFromUniqueConstraint(value);
fieldSets.addAll(_extractFieldsFromObject(value));
}
for(String[] fieldSet : fieldSets){
TreeMap<String, Object> fieldMap = new TreeMap<>();
for(String fieldName: fieldSet){
fieldMap.put(fieldName, meta.getPropertyValue(value, fieldName));
}
if(_hasRecord(value, fieldMap, idName, idValue, meta)){
return fieldMap;
}
}
return null;
}
示例2: getId
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* Return pojo identifier
*/
protected Object getId(Object template) {
Object id = null;
ClassMetadata classMetaData = getMetadata(template);
if (classMetaData == null) //Unexpected class entity
return null;
if (template instanceof IGSEntry) {
id = ((IGSEntry) template).getFieldValue(classMetaData.getIdentifierPropertyName());
} else {
id = classMetaData.getIdentifier(template);
}
return id;
}
示例3: getForeignKeyValue
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* Get the value of the foreign key property. This comes from the entity, but if that value is
* null, and the entity is deleted, we try to get it from the originalValuesMap.
* @param entityInfo Breeze EntityInfo
* @param meta Metadata for the entity class
* @param foreignKeyName Name of the foreign key property of the entity, e.g. "CustomerID"
* @return
*/
private Object getForeignKeyValue(EntityInfo entityInfo, ClassMetadata meta, String foreignKeyName) {
Object entity = entityInfo.entity;
Object id = null;
if (foreignKeyName.equalsIgnoreCase(meta.getIdentifierPropertyName())) {
id = meta.getIdentifier(entity, null);
} else if (Arrays.asList(meta.getPropertyNames()).contains(foreignKeyName)) {
id = meta.getPropertyValue(entity, foreignKeyName);
} else if (meta.getIdentifierType().isComponentType()) {
// compound key
ComponentType compType = (ComponentType) meta.getIdentifierType();
int index = Arrays.asList(compType.getPropertyNames()).indexOf(foreignKeyName);
if (index >= 0) {
Object idComp = meta.getIdentifier(entity, null);
id = compType.getPropertyValue(idComp, index, EntityMode.POJO);
}
}
if (id == null && entityInfo.entityState == EntityState.Deleted) {
id = entityInfo.originalValuesMap.get(foreignKeyName);
}
return id;
}
示例4: reload
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* Util to reload an object using Hibernate
* @param obj to be reloaded
* @return Object found if not, null
* @throws HibernateException if something bad happens.
*/
public static Object reload(Object obj) throws HibernateException {
// assertNotNull(obj);
ClassMetadata cmd = connectionManager.getMetadata(obj);
Serializable id = cmd.getIdentifier(obj, EntityMode.POJO);
Session session = getSession();
session.flush();
session.evict(obj);
/*
* In hibernate 3, the following doesn't work:
* session.load(obj.getClass(), id);
* load returns the proxy class instead of the persisted class, ie,
* Filter$$EnhancerByCGLIB$$9bcc734d_2 instead of Filter.
* session.get is set to not return the proxy class, so that is what we'll use.
*/
// assertNotSame(obj, result);
return session.get(obj.getClass(), id);
}
示例5: getIdentifier
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
public Object getIdentifier(Object entity) {
final Class entityClass = Hibernate.getClass( entity );
final ClassMetadata classMetadata = emf.getSessionFactory().getClassMetadata( entityClass );
if (classMetadata == null) {
throw new IllegalArgumentException( entityClass + " is not an entity" );
}
//TODO does that work for @IdClass?
return classMetadata.getIdentifier( entity, EntityMode.POJO );
}
示例6: updaterCopyToPersistentObject
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* 将更新对象拷贝至实体对象,并处理many-to-one的更新。
*
* @param updater
* @param po
*/
@SuppressWarnings({ "unchecked", "deprecation", "rawtypes" })
private void updaterCopyToPersistentObject(Updater updater, Object po) {
Map map = BeanUtility.describe(updater.getBean());
Set<Map.Entry<String, Object>> set = map.entrySet();
for (Map.Entry<String, Object> entry : set) {
String name = entry.getKey();
Object value = entry.getValue();
if (!updater.isUpdate(name, value)) {
continue;
}
if (value != null) {
Class<?> valueClass = value.getClass();
ClassMetadata cm = getCmd(valueClass);
if (cm != null) {
Serializable vid = cm.getIdentifier(value);
// 如果更新的many to one的对象的id为空,则将many to one设置为null。
if (vid != null) {
value = getSession().load(valueClass, vid);
} else {
value = null;
}
}
}
try {
PropertyUtils.setProperty(po, name, value);
} catch (Exception e) {
// never
logger.warn("更新对象时,拷贝属性异常", e);
}
}
}
示例7: extractId
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* Determines the ID for the entity
*
* @param entity The entity to retrieve the ID for
* @return The ID
*/
@SuppressWarnings("deprecation") // No good alternative in the Hibernate API yet
protected ID extractId(T entity) {
final Class<?> entityClass = TypeHelper.getTypeArguments(JpaBaseRepository.class, this.getClass()).get(0);
final SessionFactory sf = (SessionFactory)(getEntityManager().getEntityManagerFactory());
final ClassMetadata cmd = sf.getClassMetadata(entityClass);
final SessionImplementor si = (SessionImplementor)(getEntityManager().getDelegate());
@SuppressWarnings("unchecked")
final ID result = (ID) cmd.getIdentifier(entity, si);
return result;
}
示例8: getValue
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* This method gets the value that is stored by the property. The returned object is compatible with the
* class returned by getType().
*/
@SuppressWarnings("unchecked")
@Override
public Object getValue()
{
logger.executionTrace();
final Session session = sessionFactory.getCurrentSession();
final SessionImplementor sessionImplementor = (SessionImplementor) session;
if (!sessionFactory.getCurrentSession().contains(pojo))
pojo = (T) session.get(entityType, (Serializable) getIdForPojo(pojo));
if (propertyInEmbeddedKey(propertyName))
{
final ComponentType identifierType = (ComponentType) classMetadata.getIdentifierType();
final String[] propertyNames = identifierType.getPropertyNames();
for (int i = 0; i < propertyNames.length; i++)
{
String name = propertyNames[i];
if (name.equals(propertyName))
{
final Object id = classMetadata.getIdentifier(pojo, sessionImplementor);
return identifierType.getPropertyValue(id, i, EntityMode.POJO);
}
}
}
final Type propertyType = getPropertyType();
final Object propertyValue = classMetadata.getPropertyValue(pojo, propertyName);
if (!propertyType.isAssociationType())
return propertyValue;
if (propertyType.isCollectionType())
{
if (propertyValue == null)
return null;
final HashSet<Serializable> identifiers = new HashSet<Serializable>();
final Collection<?> pojos = (Collection<?>) propertyValue;
for (Object object : pojos)
{
if (!session.contains(object))
object = session.merge(object);
identifiers.add(session.getIdentifier(object));
}
return identifiers;
}
if (propertyValue == null)
return null;
final Class<?> propertyTypeClass = propertyType.getReturnedClass();
final ClassMetadata metadata = sessionFactory.getClassMetadata(propertyTypeClass);
final Serializable identifier = metadata.getIdentifier(propertyValue, sessionImplementor);
return identifier;
}
示例9: isValid
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* {@inheritDoc}
*/
@SuppressWarnings({"PMD.CyclomaticComplexity", "PMD.ExcessiveMethodLength" })
public boolean isValid(final Object o) {
UnfilteredCallback unfilteredCallback = new UnfilteredCallback() {
public Object doUnfiltered(Session s) {
FlushMode fm = s.getFlushMode();
try {
s.setFlushMode(FlushMode.MANUAL);
Class<?> classWithConstraint
= findClassDeclaringConstraint(hibernateHelper.unwrapProxy(o).getClass());
Criteria crit = s.createCriteria(classWithConstraint);
ClassMetadata metadata = hibernateHelper.getSessionFactory()
.getClassMetadata(classWithConstraint);
for (UniqueConstraintField field : uniqueConstraint.fields()) {
Object fieldVal = metadata.getPropertyValue(o, field.name(), EntityMode.POJO);
if (fieldVal == null) {
if (field.nullsEqual()) {
// nulls are equal, so add it to to criteria
crit.add(Restrictions.isNull(field.name()));
} else {
// nulls are never equal, so uniqueness is automatically satisfied
return true;
}
} else {
// special casing for entity-type properties - only include them in the criteria if they are
// already
// persistent
// otherwise, short-circuit the process and return true immediately since if the
// entity-type property
// is not persistent then it will be a new value and thus different from any currently in
// the db, thus satisfying uniqueness
ClassMetadata fieldMetadata = hibernateHelper
.getSessionFactory().getClassMetadata(
hibernateHelper.unwrapProxy(fieldVal).getClass());
if (fieldMetadata == null
|| fieldMetadata.getIdentifier(fieldVal, EntityMode.POJO) != null) {
crit.add(
Restrictions.eq(field.name(),
ReflectHelper.getGetter(o.getClass(),
field.name())
.get(o)));
} else {
return true;
}
}
}
// if object is already persistent, then add condition to exclude it matching itself
Object id = metadata.getIdentifier(o, EntityMode.POJO);
if (id != null) {
crit.add(Restrictions.ne(metadata.getIdentifierPropertyName(), id));
}
int numMatches = crit.list().size();
return numMatches == 0;
} finally {
s.setFlushMode(fm);
}
}
};
return (Boolean) hibernateHelper.doUnfiltered(unfilteredCallback);
}
示例10: getPropertyValue
import org.hibernate.metadata.ClassMetadata; //导入方法依赖的package包/类
/**
* Return the property value for the given entity.
* @param meta
* @param entity
* @param propName If null, the identifier property will be returned.
* @return
*/
private Object getPropertyValue(ClassMetadata meta, Object entity, String propName) {
if (propName == null || propName == meta.getIdentifierPropertyName()) return meta.getIdentifier(entity, null);
else return meta.getPropertyValue(entity, propName);
}