List of usage examples for org.hibernate.persister.entity PropertyMapping toColumns
public String[] toColumns(String propertyName) throws QueryException, UnsupportedOperationException;
From source file:edu.utah.further.core.data.util.HibernateUtil.java
License:Apache License
/** * Generates an SQL in (...) statement to be used with in statements involving * composite keys./*from ww w.j a va2s. co m*/ * * This method is strictly a workaround utility method for an issue with using * {@link Restrictions#in(String, java.util.Collection)} with a composite key. It * generates an sql statement correctly but binds the parameters wrong. This issue has * been reported SEVERAL times, as early as 05 and it still hasn't been fixed (it's * 09). There is a 2 second correction that needs to be added to the code to fix the * issue but for some reason Hibernate developers refuse to do it. In light of not * knowing what else it might break in custom compiling Hibernate with the fix it was * preferred to do a work around. It is to be used directly with * {@link Restrictions#sqlRestriction(String)}. * * @param entityClass * the entity which contains the composite key for which an SQL in * statement needs to be generated * @param sessionFactory * the {@link SessionFactory} assosciated with the entities * @param elementsSize * the size of the elements that will be in the in clause * @return * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-708 * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1575 * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1743 * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1832 * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-1972 * @see http://opensource.atlassian.com/projects/hibernate/browse/HHH-3164 */ public static String sqlRestrictionCompositeIn(final Class<? extends PersistentEntity<?>> entityClass, final SessionFactory sessionFactory, final int elementsSize) { final ClassMetadata classMetadata = sessionFactory.getClassMetadata(entityClass); final String identifierName = classMetadata.getIdentifierPropertyName(); final Type identifierType = classMetadata.getIdentifierType(); if (!(identifierType.isComponentType())) { return Strings.EMPTY_STRING; } final ComponentType componentType = (ComponentType) identifierType; final String[] idPropertyNames = componentType.getPropertyNames(); final PropertyMapping propertyMapping = getPropertyMapping(entityClass, sessionFactory); final StringBuilder inString = StringUtil.newStringBuilder(); // field names -- ({alias}.field1, {alias}.field2) inString.append('('); for (int i = 0; i < idPropertyNames.length; i++) { inString.append("{alias}").append('.') // Look up the database field from the property path .append(propertyMapping.toColumns(identifierName + "." + idPropertyNames[i])[0]); if (i + 1 < idPropertyNames.length) { inString.append(", "); } else { inString.append(')'); } } // values -- in ( (?, ?), (?, ?) ) inString.append(" in ").append('('); for (int i = 0; i < elementsSize; i++) { if (elementsSize % MAX_IN == 0) { inString.append(')'); inString.append(Strings.EMPTY_STRING); inString.append("or"); inString.append(Strings.EMPTY_STRING); inString.append('('); } inString.append('('); for (int j = 0; j < idPropertyNames.length; j++) { inString.append('?'); if (j + 1 < idPropertyNames.length) { inString.append(","); } } inString.append(')'); if ((i + 1 < elementsSize) && (i + 1 % MAX_IN != 0)) { inString.append(", "); } } inString.append(')'); return inString.toString(); }