List of usage examples for org.hibernate.persister.collection QueryableCollection getKeyColumnNames
public String[] getKeyColumnNames();
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 .ja v a 2s . co 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
private JoinTable createJoinTable(EntityType<?> ownerType, QueryableCollection queryableCollection, Map<String, String> targetColumnMapping, Set<String> targetIdAttributeNames, String attributeName) { String[] indexColumnNames = queryableCollection.getIndexColumnNames(); Map<String, String> keyColumnMapping = null; Map<String, String> keyColumnTypes = null; if (indexColumnNames != null) { keyColumnMapping = new HashMap<>(indexColumnNames.length); keyColumnTypes = new HashMap<>(indexColumnNames.length); if (queryableCollection.getKeyType().isEntityType()) { throw new IllegalArgumentException( "Determining the join table key foreign key mappings is not yet supported!"); } else {/*from w ww. j a v a 2 s . c om*/ keyColumnMapping.put(indexColumnNames[0], indexColumnNames[0]); keyColumnTypes.put(indexColumnNames[0], getColumnTypeForPropertyType(ownerType, attributeName, queryableCollection.getFactory(), queryableCollection.getIndexType())[0]); } } AbstractEntityPersister ownerEntityPersister = (AbstractEntityPersister) queryableCollection .getOwnerEntityPersister(); String[] primaryKeyColumnMetaData = ownerEntityPersister.getKeyColumnNames(); String[] foreignKeyColumnMetaData = queryableCollection.getKeyColumnNames(); Map<String, String> idColumnMapping = new HashMap<>(primaryKeyColumnMetaData.length); for (int i = 0; i < foreignKeyColumnMetaData.length; i++) { idColumnMapping.put(foreignKeyColumnMetaData[i], primaryKeyColumnMetaData[i]); } Set<String> idAttributeNames = getColumnMatchingAttributeNames(ownerEntityPersister, Arrays.asList(primaryKeyColumnMetaData)); if (targetIdAttributeNames == null) { Type elementType = queryableCollection.getElementType(); if (elementType instanceof ComponentType) { targetIdAttributeNames = new HashSet<>(); collectPropertyNames(targetIdAttributeNames, null, elementType, queryableCollection.getFactory()); } } return new JoinTable(queryableCollection.getTableName(), idAttributeNames, idColumnMapping, keyColumnMapping, keyColumnTypes, targetIdAttributeNames, targetColumnMapping); }
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; }//www. j a va 2 s .com 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 /* www .j av a 2 s. c om*/ * "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); } }