List of usage examples for org.hibernate.persister.entity AbstractEntityPersister getPropertyColumnNames
public String[] getPropertyColumnNames(int i)
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the inverse of a one-to-many role * @param entityName The entity owning the role * @param rName The role name//from ww w. java 2s.com * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role */ private String[] getInverseOfCollectionRole(String entityName, String rName) { String[] result = new String[2]; SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); AbstractCollectionPersister parentMeta = (AbstractCollectionPersister) sessFact .getCollectionMetadata(entityName + "." + rName); if (parentMeta == null) { // Could be inherited -- search through superclasses while (parentMeta == null) { Class<?> cls = null; if (getMetaEntity().getEntityType() == MetaEntity.EntityType.POJO) cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.POJO); else cls = sessFact.getClassMetadata(entityName).getMappedClass(EntityMode.MAP); Class<?> superCls = cls.getSuperclass(); if (superCls.getName().equals("java.lang.Object")) throw new RuntimeException( "Unable to retrieve Hibernate information for collection " + entityName + "." + rName); ClassMetadata clsMeta = sessFact.getClassMetadata(superCls); if (clsMeta == null) throw new RuntimeException("Unable to retrieve Hibernate information for collection " + entityName + "." + rName + ", even from superclass(es)"); entityName = clsMeta.getEntityName(); parentMeta = (AbstractCollectionPersister) sessFact.getCollectionMetadata(entityName + "." + rName); } } String[] colNames = parentMeta.getKeyColumnNames(); String childName = parentMeta.getElementType().getName(); AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(childName); String[] propNames = childMeta.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { Type type = childMeta.getPropertyType(propNames[i]); if (!type.isEntityType()) continue; EntityType entType = (EntityType) type; if (!entType.getAssociatedEntityName().equals(entityName)) continue; String[] cnames = childMeta.getPropertyColumnNames(i); if (cnames.length != colNames.length) continue; boolean columnMatch = true; for (int j = 0; j < cnames.length; j++) { if (!cnames[j].equals(colNames[j])) { columnMatch = false; break; } } if (columnMatch) { result[0] = childName; result[1] = propNames[i]; return result; } } return null; }
From source file:com.autobizlogic.abl.metadata.hibernate.HibMetaRole.java
License:Open Source License
/** * Get the inverse of a many-to-one role * @param entityName The entity owning the role * @param rName The role name/*from w w w. ja va 2s. c om*/ * @return Null if no inverse was found, otherwise [entity name, role name] of the inverse role */ private String[] getInverseOfSingleRole(String entityName, String rName) { String[] result = new String[2]; SessionFactory sessFact = ((HibMetaModel) getMetaEntity().getMetaModel()).getSessionFactory(); AbstractEntityPersister childMeta = (AbstractEntityPersister) sessFact.getClassMetadata(entityName); int propIdx = childMeta.getPropertyIndex(rName); String[] cnames = childMeta.getPropertyColumnNames(propIdx); Type parentType = childMeta.getPropertyType(rName); if (parentType instanceof OneToOneType) return getInverseOfOneToOneRole(entityName, rName); if (!(parentType instanceof ManyToOneType)) throw new RuntimeException("Inverse of single-valued role " + entityName + "." + rName + " is neither single-valued not multi-valued"); ManyToOneType manyType = (ManyToOneType) parentType; String parentEntityName = manyType.getAssociatedEntityName(); AbstractEntityPersister parentMeta = (AbstractEntityPersister) sessFact.getClassMetadata(parentEntityName); String[] propNames = parentMeta.getPropertyNames(); for (int i = 0; i < propNames.length; i++) { Type type = parentMeta.getPropertyType(propNames[i]); if (!type.isCollectionType()) continue; CollectionType collType = (CollectionType) type; if (!collType.getAssociatedEntityName((SessionFactoryImplementor) sessFact).equals(entityName)) continue; AbstractCollectionPersister persister = (AbstractCollectionPersister) sessFact .getCollectionMetadata(parentEntityName + "." + propNames[i]); String[] colNames = persister.getKeyColumnNames(); if (cnames.length != colNames.length) continue; boolean columnMatch = true; for (int j = 0; j < cnames.length; j++) { if (!cnames[j].equals(colNames[j])) { columnMatch = false; break; } } if (columnMatch) { result[0] = parentEntityName; result[1] = propNames[i]; return result; } } return null; }
From source file:com.aw.core.dao.meta.HbmUtil.java
License:Open Source License
public EntityPropertyMapper buildPropertyMapper(Class entityClass) { AbstractEntityPersister entityPersister = (AbstractEntityPersister) sessionFactory .getClassMetadata(entityClass); if (entityPersister == null) throw new IllegalArgumentException("Class " + entityClass + " is not an Hibernate entity class"); EntityPropertyMapper mapper = new EntityPropertyMapper(entityClass, entityPersister.getTableName()); String[] propertyNames = entityPersister.getPropertyNames(); for (String propertyName : propertyNames) { String[] columnNames = entityPersister.getPropertyColumnNames(propertyName); Type propertyType = entityPersister.getPropertyType(propertyName); mapper.put(propertyName, propertyType, columnNames); }/*from w w w. java 2 s . c o m*/ String identifierPropertyName = entityPersister.getIdentifierPropertyName(); String[] identifierColumnNames = entityPersister.getIdentifierColumnNames(); Type identifierPropertyType = entityPersister.getIdentifierType(); mapper.putId(identifierPropertyName, identifierPropertyType, identifierColumnNames); return mapper; }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateExtendedQuerySupport.java
License:Apache License
@Override public int getSqlSelectAttributePosition(EntityManager em, Query query, String expression) { if (expression.contains(".")) { // TODO: implement throw new UnsupportedOperationException("Embeddables are not yet supported!"); }/* w w w . jav a2 s . c o m*/ SessionImplementor session = em.unwrap(SessionImplementor.class); HQLQueryPlan plan = getOriginalQueryPlan(session, query); if (plan.getTranslators().length > 1) { throw new IllegalArgumentException("No support for multiple translators yet!"); } QueryTranslator translator = plan.getTranslators()[0]; try { QueryNode queryNode = getField(translator, "sqlAst"); SelectClause selectClause = queryNode.getSelectClause(); Type[] queryReturnTypes = selectClause.getQueryReturnTypes(); boolean found = false; // The ordinal is 1 based int position = 1; for (int i = 0; i < queryReturnTypes.length; i++) { Type t = queryReturnTypes[i]; if (t instanceof ManyToOneType) { ManyToOneType manyToOneType = (ManyToOneType) t; AbstractEntityPersister persister = (AbstractEntityPersister) session.getFactory() .getEntityPersister(manyToOneType.getAssociatedEntityName()); int propertyIndex = persister.getPropertyIndex(expression); found = true; for (int j = 0; j < propertyIndex; j++) { position += persister.getPropertyColumnNames(j).length; } // Increment to the actual property position position++; } else { position++; } } if (found) { return position; } AST selectItem = selectClause.getFirstChild(); while (selectItem != null && (selectItem.getType() == SqlTokenTypes.DISTINCT || selectItem.getType() == SqlTokenTypes.ALL)) { selectItem = selectItem.getNextSibling(); } position = 1; for (AST n = selectItem; n != null; n = n.getNextSibling()) { if (n instanceof DotNode) { DotNode dot = (DotNode) n; if (expression.equals(dot.getPropertyPath())) { // Check if the property is an embeddable if (dot.getText().contains(",")) { throw new IllegalStateException("Can't order by the embeddable: " + expression); } found = true; break; } } position++; } if (found) { return position; } return -1; } catch (Exception e1) { throw new RuntimeException(e1); } }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public Map<String, String> getWritableMappedByMappings(EntityType<?> inverseType, EntityType<?> ownerType, String attributeName, String inverseAttribute) { AbstractEntityPersister entityPersister = getEntityPersister(ownerType); int propertyIndex = entityPersister.getEntityMetamodel().getPropertyIndex(attributeName); Type propertyType = entityPersister.getPropertyTypes()[propertyIndex]; org.hibernate.type.EntityType ownerPropertyType; if (propertyType instanceof CollectionType) { QueryableCollection persister = getCollectionPersister(ownerType, attributeName); AbstractEntityPersister inversePersister = getEntityPersister(inverseType); if (!persister.isInverse() && persister.getTableName().equals(inversePersister.getTableName())) { // We have a one-to-many relationship that has just join columns // Find properties for element columns in entityPersister // Map to properties for key columns of inverseType Set<String> elementAttributes = getColumnMatchingAttributeNames(entityPersister, Arrays.asList((entityPersister.toColumns(attributeName)))); Set<String> keyAttributes = removeIdentifierAccess(ownerType, elementAttributes, inverseType, getColumnMatchingAttributeNames(inversePersister, Arrays.asList(persister.getKeyColumnNames()))); Map<String, String> mapping = new HashMap<>(); Iterator<String> elemAttrIter = elementAttributes.iterator(); Iterator<String> keyAttrIter = keyAttributes.iterator(); while (elemAttrIter.hasNext()) { mapping.put(elemAttrIter.next(), keyAttrIter.next()); }/* w w w .j a v a 2 s . c o m*/ if (mapping.isEmpty()) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } return mapping; } else { // We only support detection when the inverse collection is writable if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) { return null; } throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable!"); } } else { // Either the mapped by property is writable if (entityPersister.getEntityMetamodel().getPropertyInsertability()[propertyIndex]) { return null; } // Or the columns of the mapped by property are part of the target id ownerPropertyType = (org.hibernate.type.EntityType) propertyType; } AbstractEntityPersister sourceType = (AbstractEntityPersister) entityPersisters .get(ownerPropertyType.getAssociatedEntityName()); Type identifierType = ownerPropertyType.getIdentifierOrUniqueKeyType(entityPersister.getFactory()); String sourcePropertyPrefix; String[] sourcePropertyNames; if (identifierType.isComponentType()) { ComponentType componentType = (ComponentType) identifierType; sourcePropertyPrefix = sourceType.getIdentifierPropertyName() + "."; sourcePropertyNames = componentType.getPropertyNames(); } else { sourcePropertyPrefix = ""; sourcePropertyNames = new String[] { sourceType.getIdentifierPropertyName() }; } String[] targetColumnNames = entityPersister.getPropertyColumnNames(propertyIndex); Type targetIdType = entityPersister.getIdentifierType(); if (targetIdType.isComponentType()) { ComponentType targetIdentifierType = (ComponentType) entityPersister.getIdentifierType(); String targetPropertyPrefix = entityPersister.getIdentifierPropertyName() + "."; String[] identifierColumnNames = entityPersister.getIdentifierColumnNames(); String[] targetIdentifierTypePropertyNames = targetIdentifierType.getPropertyNames(); Map<String, String> mapping = new HashMap<>(); for (int i = 0; i < targetColumnNames.length; i++) { for (int j = 0; j < identifierColumnNames.length; j++) { if (targetColumnNames[i].equals(identifierColumnNames[j])) { mapping.put(sourcePropertyPrefix + sourcePropertyNames[i], targetPropertyPrefix + targetIdentifierTypePropertyNames[j]); break; } } } if (mapping.isEmpty()) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } return mapping; } else { String targetIdColumnName = entityPersister.getIdentifierColumnNames()[0]; if (!targetIdColumnName.equals(targetColumnNames[0])) { throw new IllegalArgumentException("Mapped by property '" + inverseType.getName() + "#" + attributeName + "' must be writable or the column must be part of the id!"); } Map<String, String> mapping = new HashMap<>(); mapping.put(sourcePropertyPrefix + sourcePropertyNames[0], entityPersister.getIdentifierPropertyName()); return mapping; } }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public String[] getColumnTypes(EntityType<?> entityType, String attributeName) { QueryableCollection collectionPersister = getCollectionPersister(entityType, attributeName); if (collectionPersister == null) { AbstractEntityPersister entityPersister = getEntityPersister(entityType); SessionFactoryImplementor sfi = entityPersister.getFactory(); String[] columnNames = entityPersister.getPropertyColumnNames(attributeName); Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService(); Table[] tables;//from w w w. j a v a2 s . c o m if (entityPersister instanceof JoinedSubclassEntityPersister) { tables = new Table[((JoinedSubclassEntityPersister) entityPersister).getSubclassTableSpan()]; for (int i = 0; i < tables.length; i++) { tables[i] = database.getTable(entityPersister.getSubclassTableName(i)); } } else if (entityPersister instanceof UnionSubclassEntityPersister) { tables = new Table[((UnionSubclassEntityPersister) entityPersister).getSubclassTableSpan()]; for (int i = 0; i < tables.length; i++) { tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i))); } } else if (entityPersister instanceof SingleTableEntityPersister) { tables = new Table[((SingleTableEntityPersister) entityPersister).getSubclassTableSpan()]; for (int i = 0; i < tables.length; i++) { tables[i] = database.getTable(unquote(entityPersister.getSubclassTableName(i))); } } else { tables = new Table[] { database.getTable(unquote(entityPersister.getTableName())) }; } // In this case, the property might represent a formula boolean isFormula = columnNames.length == 1 && columnNames[0] == null; boolean isSubselect = tables.length == 1 && tables[0] == null; if (isFormula || isSubselect) { Type propertyType = entityPersister.getPropertyType(attributeName); return getColumnTypeForPropertyType(entityType, attributeName, sfi, propertyType); } return getColumnTypesForColumnNames(entityType, columnNames, tables); } else { SessionFactoryImplementor sfi = collectionPersister.getFactory(); return getColumnTypeForPropertyType(entityType, attributeName, sfi, collectionPersister.getElementType()); } }
From source file:gov.nih.nci.security.upt.util.HibernateHelper.java
License:BSD License
private static String modifySQLForUser(FilterClause filterClause, String generatedSQL, Session session, String peiTableOrViewName) { String targetClassName = null; if (StringUtils.isBlank(filterClause.getTargetClassAlias())) targetClassName = filterClause.getTargetClassName().substring(0, filterClause.getTargetClassName().indexOf(" - ")); else// w w w . ja v a2s .c om targetClassName = filterClause.getTargetClassAlias(); String targetClassAttributeName = null; if (StringUtils.isBlank(filterClause.getTargetClassAttributeAlias())) targetClassAttributeName = filterClause.getTargetClassAttributeName(); else targetClassAttributeName = filterClause.getTargetClassAttributeAlias(); String CSM_QUERY = " select pe.attribute_value from " + "csm_protection_group pg, " + "csm_protection_element pe, " + "csm_pg_pe pgpe, " + "csm_user_group_role_pg ugrpg, " + "csm_user u, " + "csm_role_privilege rp, " + "csm_role r, " + "csm_privilege p " + "where ugrpg.role_id = r.role_id " + "and ugrpg.user_id = u.user_id and " + "ugrpg.protection_group_id = ANY " + "(select pg1.protection_group_id " + "from csm_protection_group pg1 " + "where pg1.protection_group_id = pg.protection_group_id " + "or pg1.protection_group_id = " + "(select pg2.parent_protection_group_id " + "from csm_protection_group pg2 " + "where pg2.protection_group_id = pg.protection_group_id)) " + "and pg.protection_group_id = pgpe.protection_group_id " + "and pgpe.protection_element_id = pe.protection_element_id " + "and r.role_id = rp.role_id " + "and rp.privilege_id = p.privilege_id " + "and pe.object_id= '" + targetClassName + "' " + "and pe.attribute='" + targetClassAttributeName + "' " + "and p.privilege_name='READ' " + "and u.login_name=:USER_NAME " + "and pe.application_id=:APPLICATION_ID"; String CSM_QUERY_2 = "select upei.attribute_value from " + peiTableOrViewName + " upei where " + "upei.login_name=:USER_NAME and upei.application_id =:APPLICATION_ID and upei.privilege_name='READ'"; StringBuffer result = new StringBuffer(); String query = generatedSQL.substring(generatedSQL.indexOf('-') + 1, generatedSQL.length()); query = query.trim(); query = query.substring(0, query.indexOf('?')); String delimiters = "+-*/(),. "; StringTokenizer st = new StringTokenizer(query, delimiters, true); while (st.hasMoreTokens()) { String w = st.nextToken(); if (w.equals("this_")) { result = result.append("table_name_csm_"); } else if (w.equals("y0_")) { result = result.append(""); } else if (w.equals("as")) { result = result.append(""); } else { result = result.append(w); } } SessionFactory sessionFactory = session.getSessionFactory(); ClassMetadata classMetadata = sessionFactory.getClassMetadata(filterClause.getClassName()); String columnName = null; if (classMetadata instanceof AbstractEntityPersister) { AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) classMetadata; String Id = abstractEntityPersister.getIdentifierPropertyName(); String[] columns = abstractEntityPersister.getPropertyColumnNames(Id); columnName = columns[0]; } if (!StringUtils.isBlank(peiTableOrViewName)) { query = columnName + " in (" + result.toString() + CSM_QUERY_2 + "))"; } else { query = columnName + " in (" + result.toString() + CSM_QUERY + "))"; } return query.toString(); }
From source file:gov.nih.nci.security.upt.util.HibernateHelper.java
License:BSD License
private static String modifySQLForGroup(FilterClause filterClause, String generatedSQL, Session session, String peiTableOrViewName) { String targetClassName = null; if (StringUtils.isBlank(filterClause.getTargetClassAlias())) targetClassName = filterClause.getTargetClassName().substring(0, filterClause.getTargetClassName().indexOf(" - ")); else/* w w w. j av a 2 s . c o m*/ targetClassName = filterClause.getTargetClassAlias(); String targetClassAttributeName = null; if (StringUtils.isBlank(filterClause.getTargetClassAttributeAlias())) targetClassAttributeName = filterClause.getTargetClassAttributeName(); else targetClassAttributeName = filterClause.getTargetClassAttributeAlias(); String CSM_QUERY = "SELECT Distinct pe.attribute_value " + "FROM CSM_PROTECTION_GROUP pg, " + " CSM_PROTECTION_ELEMENT pe, " + " CSM_PG_PE pgpe," + " CSM_USER_GROUP_ROLE_PG ugrpg, " + " CSM_GROUP g, " + " CSM_ROLE_PRIVILEGE rp, " + " CSM_ROLE r, " + " CSM_PRIVILEGE p " + "WHERE ugrpg.role_id = r.role_id " + "AND ugrpg.group_id = g.group_id " + "AND ugrpg.protection_group_id = ANY " + "( select pg1.protection_group_id from csm_protection_group pg1 " + " where pg1.protection_group_id = pg.protection_group_id OR pg1.protection_group_id = " + " (select pg2.parent_protection_group_id from csm_protection_group pg2 where pg2.protection_group_id = pg.protection_group_id)" + " ) " + "AND pg.protection_group_id = pgpe.protection_group_id " + "AND pgpe.protection_element_id = pe.protection_element_id " + "AND r.role_id = rp.role_id " + "AND rp.privilege_id = p.privilege_id " + "AND pe.object_id= '" + targetClassName + "' " + "AND p.privilege_name='READ' " + "AND g.group_name IN (:GROUP_NAMES ) " + "AND pe.application_id=:APPLICATION_ID"; String CSM_QUERY_2 = "select upei.attribute_value from " + peiTableOrViewName + " upei where " + "upei.group_name IN (:GROUP_NAMES) and upei.application_id =:APPLICATION_ID and upei.privilege_name='READ'"; /*String CSM_QUERY = " select pe.attribute_value from " + "csm_protection_group pg, " + "csm_protection_element pe, " + "csm_pg_pe pgpe, " + "csm_user_group_role_pg ugrpg, " + "csm_user u, " + "csm_role_privilege rp, " + "csm_role r, " + "csm_privilege p " + "where ugrpg.role_id = r.role_id " + "and ugrpg.user_id = u.user_id and " + "ugrpg.protection_group_id = ANY " + "(select pg1.protection_group_id " + "from csm_protection_group pg1 " + "where pg1.protection_group_id = pg.protection_group_id " + "or pg1.protection_group_id = " + "(select pg2.parent_protection_group_id " + "from csm_protection_group pg2 " + "where pg2.protection_group_id = pg.protection_group_id)) " + "and pg.protection_group_id = pgpe.protection_group_id " + "and pgpe.protection_element_id = pe.protection_element_id " + "and r.role_id = rp.role_id " + "and rp.privilege_id = p.privilege_id " + "and pe.object_id= '" + targetClassName + "' " + "and pe.attribute='" + targetClassAttributeName + "' " + "and p.privilege_name='READ' " + "and u.login_name=:USER_NAME " + "and pe.application_id=:APPLICATION_ID" ; */ StringBuffer result = new StringBuffer(); String query = generatedSQL.substring(generatedSQL.indexOf('-') + 1, generatedSQL.length()); query = query.trim(); query = query.substring(0, query.indexOf('?')); String delimiters = "+-*/(),. "; StringTokenizer st = new StringTokenizer(query, delimiters, true); while (st.hasMoreTokens()) { String w = st.nextToken(); if (w.equals("this_")) { result = result.append("table_name_csm_"); } else if (w.equals("y0_")) { result = result.append(""); } else if (w.equals("as")) { result = result.append(""); } else { result = result.append(w); } } SessionFactory sessionFactory = session.getSessionFactory(); ClassMetadata classMetadata = sessionFactory.getClassMetadata(filterClause.getClassName()); String columnName = null; if (classMetadata instanceof AbstractEntityPersister) { AbstractEntityPersister abstractEntityPersister = (AbstractEntityPersister) classMetadata; String Id = abstractEntityPersister.getIdentifierPropertyName(); String[] columns = abstractEntityPersister.getPropertyColumnNames(Id); columnName = columns[0]; } if (!StringUtils.isBlank(peiTableOrViewName)) { query = columnName + " in (" + result.toString() + CSM_QUERY_2 + "))"; } else { query = columnName + " in (" + result.toString() + CSM_QUERY + "))"; } return query.toString(); }
From source file:org.apache.ctakes.ytex.uima.mapper.DocumentMapperServiceImpl.java
License:Apache License
public void initDocKeyMapping() { AbstractEntityPersister cm = (AbstractEntityPersister) this.sessionFactory.getClassMetadata(Document.class); // figure out which columns are already mapped String[] propNames = cm.getPropertyNames(); Set<String> mappedCols = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER); for (String prop : propNames) { String cols[] = cm.getPropertyColumnNames(prop); mappedCols.addAll(Arrays.asList(cols)); }/*from w w w . j ava2 s . c om*/ // this.formattedTableName = DBUtil.formatTableName(cm.getTableName()); this.formattedTableName = cm.getTableName(); log.info("document table name = " + formattedTableName); final String query = "select * from " + formattedTableName + " where 1=2"; Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = dataSource.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery(query); ResultSetMetaData rsmd = rs.getMetaData(); int nCols = rsmd.getColumnCount(); for (int i = 1; i <= nCols; i++) { String colName = rsmd.getColumnName(i); if (!mappedCols.contains(colName)) { log.info("document candidate foreign key column: " + colName); docTableCols.put(colName, rsmd.getColumnType(i)); } } if (log.isDebugEnabled()) { log.debug("docTableCols: " + docTableCols); } } catch (SQLException e) { log.error("problem determining document table fields", e); throw new RuntimeException(e); } finally { try { if (rs != null) rs.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (stmt != null) stmt.close(); } catch (SQLException e) { e.printStackTrace(); } try { if (conn != null) conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
From source file:org.nextframework.persistence.PersistenceUtils.java
License:Apache License
public static InverseCollectionProperties getInverseCollectionProperty(SessionFactory sessionFactory, Class<? extends Object> clazz, String collectionProperty) { SessionFactoryImplementor sessionFactoryImplementor; String[] keyColumnNames;//from ww w . j a va 2 s . c o m Class<?> returnedClass; try { sessionFactoryImplementor = (SessionFactoryImplementor) sessionFactory; ClassMetadata classMetadata = getClassMetadata(clazz, sessionFactory); if (classMetadata == null) { throw new PersistenceException("Class " + clazz.getName() + " is not mapped. "); } CollectionType ct = (CollectionType) classMetadata.getPropertyType(collectionProperty); AbstractCollectionPersister collectionMetadata = (AbstractCollectionPersister) sessionFactoryImplementor .getCollectionMetadata(ct.getRole()); keyColumnNames = ((AbstractCollectionPersister) collectionMetadata).getKeyColumnNames(); returnedClass = ct.getElementType(sessionFactoryImplementor).getReturnedClass(); } catch (ClassCastException e) { throw new PersistenceException( "Property \"" + collectionProperty + "\" of " + clazz + " is not a mapped as a collection."); } AbstractEntityPersister collectionItemMetadata = (AbstractEntityPersister) sessionFactoryImplementor .getClassMetadata(returnedClass); Type[] propertyTypes = collectionItemMetadata.getPropertyTypes(); String[] propertyNames = collectionItemMetadata.getPropertyNames(); for (int i = 0; i < propertyTypes.length; i++) { Type type = propertyTypes[i]; String propertyName = propertyNames[i]; String[] propertyColumnNames = collectionItemMetadata.getPropertyColumnNames(propertyName); InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties( sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, propertyColumnNames, type, propertyName); if (inverseCollectionProperties != null) { return inverseCollectionProperties; } } //check id Type identifierType = collectionItemMetadata.getIdentifierType(); String identifierName = collectionItemMetadata.getIdentifierPropertyName(); String[] identifierColumnNames = collectionItemMetadata.getIdentifierColumnNames(); InverseCollectionProperties inverseCollectionProperties = getInverseCollectionProperties( sessionFactoryImplementor, clazz, returnedClass, keyColumnNames, identifierColumnNames, identifierType, identifierName); if (inverseCollectionProperties != null) { return inverseCollectionProperties; } throw new PersistenceException( "Collection " + collectionProperty + " of " + clazz + " does not have an inverse path!"); }