List of usage examples for org.hibernate.metadata ClassMetadata getNaturalIdentifierProperties
int[] getNaturalIdentifierProperties();
From source file:cz.jirutka.commons.hibernate.HibernateUtils.java
License:Open Source License
/** * Return names of properties holding NaturalId of the given entity's * metadata./*from w w w. ja v a2s . c o m*/ * * @param metadata entity class metadata * @return names of properties holding NaturalId */ public static String[] getNaturalIdentifierNames(ClassMetadata metadata) { if (!metadata.hasNaturalIdentifier()) { return new String[0]; } int[] naturalProps = metadata.getNaturalIdentifierProperties(); String[] propNames = metadata.getPropertyNames(); String[] naturalNames = new String[naturalProps.length]; for (int i = 0; i < naturalProps.length; i++) { naturalNames[i] = propNames[naturalProps[i]]; } return naturalNames; }
From source file:cz.jirutka.rsql.hibernate.builder.NaturalIdCriterionBuilder.java
License:Open Source License
@Override public Criterion createCriterion(String property, Comparison operator, String argument, Class<?> entityClass, String alias, CriteriaBuilder builder) throws ArgumentFormatException, UnknownSelectorException { Class<?> type = findPropertyType(property, builder.getClassMetadata(entityClass)); ClassMetadata classMetadata = builder.getClassMetadata(type); int[] idProps = classMetadata.getNaturalIdentifierProperties(); Class<?> idType = classMetadata.getPropertyTypes()[idProps[0]].getReturnedClass(); String idName = classMetadata.getPropertyNames()[idProps[0]]; if (idProps.length != 1) { LOG.warn("Entity {} has more than one Natural ID, only first will be used"); }/* w ww . j av a 2 s . c om*/ LOG.debug("Entity {} has Natural ID {} of type {}", new Object[] { type.getSimpleName(), idName, idType.getSimpleName() }); Object castedArgument = builder.getArgumentParser().parse(argument, idType); String newAlias = builder.createAssociationAlias(alias + property); return createCriterion(newAlias + '.' + idName, operator, castedArgument); }
From source file:es.logongas.ix3.dao.metadata.impl.MetaDataImplHibernate.java
License:Apache License
@Override public List<String> getNaturalKeyPropertiesName() { List<String> naturalKeyPropertiesName = new ArrayList<String>(); ClassMetadata classMetadata = getClassMetadata(); if (classMetadata == null) { return naturalKeyPropertiesName; }//ww w. j av a 2s. c o m if (classMetadata.hasNaturalIdentifier()) { int[] positions = classMetadata.getNaturalIdentifierProperties(); String[] propertyNames = classMetadata.getPropertyNames(); for (int i = 0; i < positions.length; i++) { int position = positions[i]; String naturalKeyPropertyName = propertyNames[position]; naturalKeyPropertiesName.add(naturalKeyPropertyName); } } else { //Si no hay clave natural, la lista no tendr ningn elemento } return naturalKeyPropertiesName; }