List of usage examples for org.hibernate.persister.collection QueryableCollection getElementColumnNames
public abstract String[] getElementColumnNames();
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public String[] getColumnNames(EntityType<?> entityType, String attributeName) { QueryableCollection collectionPersister = getCollectionPersister(entityType, attributeName); if (collectionPersister == null) { try {/*ww w . j av a 2s. c o m*/ return getEntityPersister(entityType).getPropertyColumnNames(attributeName); } catch (MappingException e) { throw new RuntimeException( "Unknown property [" + attributeName + "] of entity [" + entityType.getJavaType() + "]", e); } } else { return collectionPersister.getElementColumnNames(); } }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public String[] getColumnNames(EntityType<?> ownerType, String elementCollectionPath, String attributeName) { QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath); String subAttributeName = attributeName.substring(elementCollectionPath.length() + 1); if (persister.getElementType() instanceof ComponentType) { ComponentType elementType = (ComponentType) persister.getElementType(); String[] propertyNames = elementType.getPropertyNames(); Type[] subtypes = elementType.getSubtypes(); String[] propertyParts = subAttributeName.split("\\."); int offset = 0; for (int j = 0; j < propertyParts.length - 1; j++) { String propertyName = propertyParts[j]; for (int i = 0; i < propertyNames.length; i++) { int span = subtypes[i].getColumnSpan(persister.getFactory()); if (propertyName.equals(propertyNames[i])) { if (subtypes[i] instanceof ComponentType) { elementType = (ComponentType) subtypes[i]; propertyNames = elementType.getPropertyNames(); subtypes = elementType.getSubtypes(); break; } else { String[] columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); return columnNames; }/* w ww.j a v a 2s. c om*/ } else { offset += span; } } } String propertyName = propertyParts[propertyParts.length - 1]; for (int i = 0; i < propertyNames.length; i++) { int span = subtypes[i].getColumnSpan(persister.getFactory()); if (propertyName.equals(propertyNames[i])) { String[] columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); return columnNames; } else { offset += span; } } } else if (persister.getElementType() instanceof org.hibernate.type.EntityType) { AbstractEntityPersister elementPersister = (AbstractEntityPersister) entityPersisters .get(((org.hibernate.type.EntityType) persister.getElementType()).getAssociatedEntityName()); Type identifierType = ((org.hibernate.type.EntityType) persister.getElementType()) .getIdentifierOrUniqueKeyType(persister.getFactory()); String identifierOrUniqueKeyPropertyName = ((org.hibernate.type.EntityType) persister.getElementType()) .getIdentifierOrUniqueKeyPropertyName(persister.getFactory()); String prefix; if (identifierType instanceof EmbeddedComponentType) { String[] propertyNames = ((EmbeddedComponentType) identifierType).getPropertyNames(); String[] columnNames = columnNamesByPropertyName(elementPersister, propertyNames, subAttributeName, "", persister.getElementColumnNames(), persister.getFactory()); if (columnNames != null) { return columnNames; } } else if (subAttributeName.equals(identifierOrUniqueKeyPropertyName)) { return persister.getElementColumnNames(); } else if (identifierType instanceof ComponentType && subAttributeName.startsWith(prefix = identifierOrUniqueKeyPropertyName + ".")) { String[] propertyNames = ((ComponentType) identifierType).getPropertyNames(); String[] columnNames = columnNamesByPropertyName(elementPersister, propertyNames, subAttributeName.substring(identifierOrUniqueKeyPropertyName.length() + 1), prefix, persister.getElementColumnNames(), persister.getFactory()); if (columnNames != null) { return columnNames; } } } throw new IllegalArgumentException( "Couldn't find column names for " + getTypeName(ownerType) + "#" + attributeName); }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public String[] getColumnTypes(EntityType<?> ownerType, String elementCollectionPath, String attributeName) { QueryableCollection persister = getCollectionPersister(ownerType, elementCollectionPath); SessionFactoryImplementor sfi = persister.getFactory(); String[] columnNames = null;/*w w w. j a va 2s.c o m*/ Type propertyType = null; String subAttributeName = attributeName.substring(elementCollectionPath.length() + 1); if (persister.getElementType() instanceof ComponentType) { ComponentType elementType = (ComponentType) persister.getElementType(); String[] propertyNames = elementType.getPropertyNames(); Type[] subtypes = elementType.getSubtypes(); String[] propertyParts = subAttributeName.split("\\."); int offset = 0; for (int j = 0; j < propertyParts.length - 1; j++) { String propertyName = propertyParts[j]; for (int i = 0; i < propertyNames.length; i++) { int span = subtypes[i].getColumnSpan(persister.getFactory()); if (propertyName.equals(propertyNames[i])) { if (subtypes[i] instanceof ComponentType) { elementType = (ComponentType) subtypes[i]; propertyNames = elementType.getPropertyNames(); subtypes = elementType.getSubtypes(); break; } else { columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); break; } } else { offset += span; } } } if (columnNames == null) { String propertyName = propertyParts[propertyParts.length - 1]; for (int i = 0; i < propertyNames.length; i++) { int span = subtypes[i].getColumnSpan(persister.getFactory()); if (propertyName.equals(propertyNames[i])) { columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); break; } else { offset += span; } } } } else if (persister.getElementType() instanceof org.hibernate.type.EntityType) { Type identifierType = ((org.hibernate.type.EntityType) persister.getElementType()) .getIdentifierOrUniqueKeyType(persister.getFactory()); String identifierOrUniqueKeyPropertyName = ((org.hibernate.type.EntityType) persister.getElementType()) .getIdentifierOrUniqueKeyPropertyName(persister.getFactory()); String prefix; if (identifierType instanceof EmbeddedComponentType) { String[] propertyNames = ((EmbeddedComponentType) identifierType).getPropertyNames(); Type[] subtypes = ((EmbeddedComponentType) identifierType).getSubtypes(); int offset = 0; for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; int span = subtypes[i].getColumnSpan(persister.getFactory()); if (subAttributeName.equals(propertyName)) { columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); propertyType = subtypes[i]; break; } else { offset += span; } } } else if (subAttributeName.equals(identifierOrUniqueKeyPropertyName)) { columnNames = persister.getElementColumnNames(); propertyType = identifierType; } else if (identifierType instanceof ComponentType && subAttributeName.startsWith(prefix = identifierOrUniqueKeyPropertyName + ".")) { String[] propertyNames = ((ComponentType) identifierType).getPropertyNames(); Type[] subtypes = ((ComponentType) identifierType).getSubtypes(); String subPropertyName = subAttributeName.substring(prefix.length()); int offset = 0; for (int i = 0; i < propertyNames.length; i++) { String propertyName = propertyNames[i]; int span = subtypes[i].getColumnSpan(persister.getFactory()); if (subPropertyName.equals(propertyName)) { columnNames = new String[span]; String[] elementColumnNames = persister.getElementColumnNames(); System.arraycopy(elementColumnNames, offset, columnNames, 0, span); propertyType = subtypes[i]; break; } else { offset += span; } } } } if (columnNames == null) { throw new IllegalArgumentException( "Couldn't find column names for " + getTypeName(ownerType) + "#" + attributeName); } boolean isFormula = columnNames.length == 1 && columnNames[0] == null; if (isFormula) { return getColumnTypeForPropertyType(ownerType, attributeName, sfi, propertyType); } Database database = sfi.getServiceRegistry().locateServiceBinding(Database.class).getService(); Table[] tables = new Table[] { database.getTable(unquote(persister.getTableName())) }; return getColumnTypesForColumnNames(ownerType, columnNames, tables); }
From source file:com.blazebit.persistence.integration.hibernate.base.HibernateJpaProvider.java
License:Apache License
@Override public JoinTable getJoinTable(EntityType<?> ownerType, String attributeName) { CollectionPersister persister = getCollectionPersister(ownerType, attributeName); if (persister instanceof QueryableCollection) { QueryableCollection queryableCollection = (QueryableCollection) persister; if (!queryableCollection.getElementType().isEntityType()) { String[] targetColumnMetaData = queryableCollection.getElementColumnNames(); Map<String, String> targetColumnMapping = new HashMap<>(); for (int i = 0; i < targetColumnMetaData.length; i++) { targetColumnMapping.put(targetColumnMetaData[i], targetColumnMetaData[i]); }//from w w w. ja v a2s .c om return createJoinTable(ownerType, queryableCollection, targetColumnMapping, null, attributeName); } else if (queryableCollection.getElementPersister() instanceof Joinable) { String elementTableName = ((Joinable) queryableCollection.getElementPersister()).getTableName(); if (!queryableCollection.getTableName().equals(elementTableName)) { String[] targetColumnMetaData = queryableCollection.getElementColumnNames(); AbstractEntityPersister elementPersister = (AbstractEntityPersister) queryableCollection .getElementPersister(); String[] targetPrimaryKeyColumnMetaData = elementPersister.getKeyColumnNames(); Map<String, String> targetIdColumnMapping = new HashMap<>(); for (int i = 0; i < targetColumnMetaData.length; i++) { targetIdColumnMapping.put(targetColumnMetaData[i], targetPrimaryKeyColumnMetaData[i]); } Set<String> idAttributeNames = getColumnMatchingAttributeNames(elementPersister, Arrays.asList(targetPrimaryKeyColumnMetaData)); return createJoinTable(ownerType, queryableCollection, targetIdColumnMapping, idAttributeNames, attributeName); } } } return null; }
From source file:org.babyfish.hibernate.collection.spi.persistence.MapBasePersistence.java
License:Open Source License
@SuppressWarnings("unchecked") public Ref<V> visionallyRead(K key) { Arguments.mustNotBeNull("key", key); String role = this.getNonNullRole(); SessionImplementor session = this.getSession(); if (session == null || !session.isOpen() || !session.isConnected()) { return null; }// ww w . j a v a2s . c om SessionFactoryImplementor sessionFactory = session.getFactory(); QueryableCollection collection = (QueryableCollection) sessionFactory.getCollectionPersister(role); EntityPersister elementPersister = collection.getElementPersister(); String[] indexNames = collection.getIndexColumnNames(); if (indexNames == null || indexNames[0] == null) { indexNames = collection.getIndexFormulas(); } CriteriaImpl criteria = new CriteriaImpl(elementPersister.getEntityName(), session); //ownerKey, not ownerId Object ownerKey = collection.getCollectionType().getKeyOfOwner(this.getOwner(), session); //In Hibernate, isOneToMany means that there is no middle table //The @OneToMany of JPA with middle table is consider as many-to-many in Hibernate if (sessionFactory.getCollectionPersister(role).isOneToMany()) { String[] joinOwnerColumns = collection.getKeyColumnNames(); StringBuilder sqlBuilder = new StringBuilder(); for (int i = 0; i < joinOwnerColumns.length; i++) { if (i != 0) { sqlBuilder.append(" and "); } sqlBuilder.append("{alias}.").append(joinOwnerColumns[i]).append(" = ?"); } criteria.add(Restrictions.sqlRestriction(sqlBuilder.toString(), ownerKey, collection.getKeyType())); sqlBuilder = new StringBuilder(); for (int i = 0; i < indexNames.length; i++) { if (i != 0) { sqlBuilder.append(" and "); } sqlBuilder.append("{alias}.").append(indexNames[i]).append(" = ?"); } criteria.add(Restrictions.sqlRestriction(sqlBuilder.toString(), key, collection.getIndexType())); } else { String lhsPropertyName = collection.getCollectionType().getLHSPropertyName(); int lhsPropertyIndex = -1; if (lhsPropertyName != null) { String[] propertyNames = collection.getOwnerEntityPersister().getPropertyNames(); for (int i = propertyNames.length - 1; i >= 0; i--) { if (propertyNames[i].equals(lhsPropertyName)) { lhsPropertyIndex = i; break; } } } String[] lhsColumnNames = JoinHelper.getLHSColumnNames(collection.getCollectionType(), lhsPropertyIndex, (OuterJoinLoadable) elementPersister, sessionFactory); String[] joinElementColumnNames = collection.getElementColumnNames(); String[] joinOwnerColumnNames = collection.getKeyColumnNames(); StringBuilder subQueryBuilder = new StringBuilder(); subQueryBuilder.append("exists(select * from ").append(collection.getTableName()).append(" as ") .append(MIDDLE_TABLE_ALIAS).append(" where "); for (int i = 0; i < joinElementColumnNames.length; i++) { if (i != 0) { subQueryBuilder.append(" and "); } subQueryBuilder.append("{alias}.").append(lhsColumnNames[i]).append(" = ") .append(MIDDLE_TABLE_ALIAS).append(".").append(joinElementColumnNames[i]); } for (int i = 0; i < joinOwnerColumnNames.length; i++) { subQueryBuilder.append(" and ").append(MIDDLE_TABLE_ALIAS).append('.') .append(joinOwnerColumnNames[i]).append(" = ?"); } for (int i = 0; i < indexNames.length; i++) { subQueryBuilder.append(" and ").append(MIDDLE_TABLE_ALIAS).append('.').append(indexNames[i]) .append(" = ?"); } subQueryBuilder.append(')'); criteria.add(Restrictions.sqlRestriction(subQueryBuilder.toString(), new Object[] { ownerKey, key }, new Type[] { collection.getKeyType(), collection.getIndexType() })); } FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); try { return new Ref<V>((V) criteria.uniqueResult()); } finally { session.setFlushMode(oldFlushMode); } }
From source file:org.babyfish.hibernate.collection.spi.persistence.SetBasePersistence.java
License:Open Source License
/** * This method is used to replace /*from w w w . ja va2s. c o m*/ * "org.hibernate.collection.AbstractPersistentCollection#readElementExistence(Object element)" * @param element The example element to be read * @return The ref or readed element * <ul> * <li>NonNull: Read successfully, check the value of ref to check the read value is null or not</li> * <li>Null: Read failed</li> * </ul> */ @SuppressWarnings("unchecked") public Ref<E> visionallyRead(E element) { Arguments.mustNotBeNull("element", element); String role = this.getNonNullRole(); SessionImplementor session = this.getSession(); if (session == null || !session.isOpen() || !session.isConnected()) { return null; } SessionFactoryImplementor sessionFactory = session.getFactory(); QueryableCollection collection = (QueryableCollection) sessionFactory.getCollectionPersister(role); EntityPersister elementPersister = collection.getElementPersister(); Object elementId = elementPersister.getIdentifier(element, this.getSession()); if (elementId == null) { return new Ref<>(); } if (elementPersister.getEntityMetamodel().getIdentifierProperty().getUnsavedValue() .isUnsaved((Serializable) elementId)) { return new Ref<>(); } CriteriaImpl criteria = new CriteriaImpl(elementPersister.getEntityName(), session); /* * Add the condition of element. */ criteria.add(Restrictions.idEq(elementId)); //ownerKey, not ownerId Object ownerKey = collection.getCollectionType().getKeyOfOwner(this.getOwner(), session); //In Hibernate, isOneToMany means that there is no middle table //The @OneToMany of JPA with middle table is consider as many-to-many in Hibernate if (sessionFactory.getCollectionPersister(role).isOneToMany()) { String[] joinOwnerColumns = collection.getKeyColumnNames(); StringBuilder sqlBuilder = new StringBuilder(); for (int i = 0; i < joinOwnerColumns.length; i++) { if (i != 0) { sqlBuilder.append(" and "); } sqlBuilder.append("{alias}.").append(joinOwnerColumns[i]).append(" = ?"); } criteria.add(Restrictions.sqlRestriction(sqlBuilder.toString(), ownerKey, collection.getKeyType())); } else { String lhsPropertyName = collection.getCollectionType().getLHSPropertyName(); int lhsPropertyIndex = -1; if (lhsPropertyName != null) { String[] propertyNames = collection.getOwnerEntityPersister().getPropertyNames(); for (int i = propertyNames.length - 1; i >= 0; i--) { if (propertyNames[i].equals(lhsPropertyName)) { lhsPropertyIndex = i; break; } } } String[] lhsColumnNames = JoinHelper.getLHSColumnNames(collection.getCollectionType(), lhsPropertyIndex, (OuterJoinLoadable) elementPersister, sessionFactory); String[] joinElementColumnNames = collection.getElementColumnNames(); String[] joinOwnerColumnNames = collection.getKeyColumnNames(); StringBuilder subQueryBuilder = new StringBuilder(); subQueryBuilder.append("exists(select * from ").append(collection.getTableName()).append(" as ") .append(MIDDLE_TABLE_ALIAS).append(" where "); for (int i = 0; i < joinElementColumnNames.length; i++) { if (i != 0) { subQueryBuilder.append(" and "); } subQueryBuilder.append("{alias}.").append(lhsColumnNames[i]).append(" = ") .append(MIDDLE_TABLE_ALIAS).append('.').append(joinElementColumnNames[i]); } for (int i = 0; i < joinOwnerColumnNames.length; i++) { subQueryBuilder.append(" and ").append(MIDDLE_TABLE_ALIAS).append(".") .append(joinOwnerColumnNames[i]).append(" = ?"); } subQueryBuilder.append(')'); criteria.add( Restrictions.sqlRestriction(subQueryBuilder.toString(), ownerKey, collection.getKeyType())); } FlushMode oldFlushMode = session.getFlushMode(); session.setFlushMode(FlushMode.MANUAL); try { return new Ref<>((E) criteria.uniqueResult()); } finally { session.setFlushMode(oldFlushMode); } }