org.hibernate.mapping.PersistentClass Java Examples

The following examples show how to use org.hibernate.mapping.PersistentClass. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PersisterFactory.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static EntityPersister createClassPersister(
		PersistentClass model, 
		CacheConcurrencyStrategy cache, 
		SessionFactoryImplementor factory,
		Mapping cfg)
throws HibernateException {
	Class persisterClass = model.getEntityPersisterClass();
	if (persisterClass==null || persisterClass==SingleTableEntityPersister.class) {
		return new SingleTableEntityPersister(model, cache, factory, cfg);
	}
	else if (persisterClass==JoinedSubclassEntityPersister.class) {
		return new JoinedSubclassEntityPersister(model, cache, factory, cfg);
	}
	else if (persisterClass==UnionSubclassEntityPersister.class) {
		return new UnionSubclassEntityPersister(model, cache, factory, cfg);
	}
	else {
		return create(persisterClass, model, cache, factory, cfg);
	}
}
 
Example #2
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void addEntityBinding(PersistentClass persistentClass) throws DuplicateMappingException {
	final String entityName = persistentClass.getEntityName();
	if ( entityBindingMap.containsKey( entityName ) ) {
		throw new DuplicateMappingException( DuplicateMappingException.Type.ENTITY, entityName );
	}
	entityBindingMap.put( entityName, persistentClass );

	final AccessType accessType = AccessType.fromExternalName( persistentClass.getCacheConcurrencyStrategy() );
	if ( accessType != null ) {
		if ( persistentClass.isCached() ) {
			locateCacheRegionConfigBuilder( persistentClass.getRootClass().getCacheRegionName() ).addEntityConfig(
					persistentClass,
					accessType
			);
		}

		if ( persistentClass.hasNaturalId() && persistentClass instanceof RootClass && persistentClass.getNaturalIdCacheRegionName() != null ) {
			locateCacheRegionConfigBuilder( persistentClass.getNaturalIdCacheRegionName() ).addNaturalIdConfig(
					(RootClass) persistentClass,
					accessType
			);
		}
	}
}
 
Example #3
Source File: Ejb3JoinColumn.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
public void linkValueUsingDefaultColumnNaming(
		Column referencedColumn,
		PersistentClass referencedEntity,
		SimpleValue value) {
	String logicalReferencedColumn = getBuildingContext().getMetadataCollector().getLogicalColumnName(
			referencedEntity.getTable(),
			referencedColumn.getQuotedName()
	);
	String columnName = buildDefaultColumnName( referencedEntity, logicalReferencedColumn );

	//yuk side effect on an implicit column
	setLogicalColumnName( columnName );
	setReferencedColumn( logicalReferencedColumn );
	initMappingColumn(
			columnName,
			null, referencedColumn.getLength(),
			referencedColumn.getPrecision(),
			referencedColumn.getScale(),
			getMappingColumn() != null ? getMappingColumn().isNullable() : false,
			referencedColumn.getSqlType(),
			getMappingColumn() != null ? getMappingColumn().isUnique() : false,
			false
	);
	linkWithValue( value );
}
 
Example #4
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void createIndexBackRef(
		MappingDocument mappingDocument,
		IndexedPluralAttributeSource pluralAttributeSource,
		IndexedCollection collectionBinding) {
	if ( collectionBinding.isOneToMany()
			&& !collectionBinding.getKey().isNullable()
			&& !collectionBinding.isInverse() ) {
		final String entityName = ( (OneToMany) collectionBinding.getElement() ).getReferencedEntityName();
		final PersistentClass referenced = mappingDocument.getMetadataCollector().getEntityBinding( entityName );
		final IndexBackref ib = new IndexBackref();
		ib.setName( '_' + collectionBinding.getOwnerEntityName() + "." + pluralAttributeSource.getName() + "IndexBackref" );
		ib.setUpdateable( false );
		ib.setSelectable( false );
		ib.setCollectionRole( collectionBinding.getRole() );
		ib.setEntityName( collectionBinding.getOwner().getEntityName() );
		ib.setValue( collectionBinding.getIndex() );
		referenced.addProperty( ib );
	}
}
 
Example #5
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
private static void bindPojoRepresentation(Element node, PersistentClass entity,
		Mappings mappings, java.util.Map metaTags) {

	String className = getClassName( node.attribute( "name" ), mappings );
	String proxyName = getClassName( node.attribute( "proxy" ), mappings );

	entity.setClassName( className );

	if ( proxyName != null ) {
		entity.setProxyInterfaceName( proxyName );
		entity.setLazy( true );
	}
	else if ( entity.isLazy() ) {
		entity.setProxyInterfaceName( className );
	}

	Element tuplizer = locateTuplizerDefinition( node, EntityMode.POJO );
	if ( tuplizer != null ) {
		entity.addTuplizer( EntityMode.POJO, tuplizer.attributeValue( "class" ) );
	}
}
 
Example #6
Source File: PersistentTableBulkIdStrategy.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@Override
protected IdTableInfoImpl buildIdTableInfo(
		PersistentClass entityBinding,
		Table idTable,
		JdbcServices jdbcServices,
		MetadataImplementor metadata,
		PreparationContextImpl context) {
	final String renderedName = jdbcServices.getJdbcEnvironment().getQualifiedObjectNameFormatter().format(
			idTable.getQualifiedTableName(),
			jdbcServices.getJdbcEnvironment().getDialect()
	);

	context.creationStatements.add( buildIdTableCreateStatement( idTable, jdbcServices, metadata ) );
	if ( dropIdTables ) {
		context.dropStatements.add( buildIdTableDropStatement( idTable, jdbcServices ) );
	}

	return new IdTableInfoImpl( renderedName );
}
 
Example #7
Source File: AbstractEntityPersister.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
private boolean determineCanReadFromCache(PersistentClass persistentClass, EntityDataAccess cacheAccessStrategy) {
	if ( cacheAccessStrategy == null ) {
		return false;
	}

	if ( persistentClass.isCached() ) {
		return true;
	}

	final Iterator<Subclass> subclassIterator = persistentClass.getSubclassIterator();
	while ( subclassIterator.hasNext() ) {
		final Subclass subclass = subclassIterator.next();
		if ( subclass.isCached() ) {
			return true;
		}
	}
	return false;
}
 
Example #8
Source File: JoinedSubclassEntityPersister.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void associateSubclassNamesToSubclassTableIndexes(
		PersistentClass persistentClass,
		Set<String> classNames,
		String[][] mapping,
		SessionFactoryImplementor factory) {

	final String tableName = persistentClass.getTable().getQualifiedName(
			factory.getDialect(),
			factory.getSettings().getDefaultCatalogName(),
			factory.getSettings().getDefaultSchemaName()
	);

	associateSubclassNamesToSubclassTableIndex( tableName, classNames, mapping );

	Iterator itr = persistentClass.getJoinIterator();
	while ( itr.hasNext() ) {
		final Join join = (Join) itr.next();
		final String secondaryTableName = join.getTable().getQualifiedName(
				factory.getDialect(),
				factory.getSettings().getDefaultCatalogName(),
				factory.getSettings().getDefaultSchemaName()
		);
		associateSubclassNamesToSubclassTableIndex( secondaryTableName, classNames, mapping );
	}
}
 
Example #9
Source File: EntityCallbackHandlerInitializer.java    From DataHubSystem with GNU Affero General Public License v3.0 6 votes vote down vote up
@PostConstruct
public void init() throws ClassNotFoundException
{
  final Configuration configuration = annotationSessionFactory
        .getConfiguration();
  final ReflectionManager reflectionManager = configuration
        .getReflectionManager();
  final Iterator<PersistentClass> classMappings = configuration
        .getClassMappings();
  while (classMappings.hasNext()) 
  {
     entityCallbackHandler.add(reflectionManager.classForName(
        classMappings.next().getClassName(), this.getClass()),
        reflectionManager);
  }
}
 
Example #10
Source File: AttributeFactory.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private EntityMetamodel getDeclarerEntityMetamodel(AbstractIdentifiableType<?> ownerType) {
	final Type.PersistenceType persistenceType = ownerType.getPersistenceType();
	if ( persistenceType == Type.PersistenceType.ENTITY ) {
		return context.getSessionFactory()
				.getMetamodel()
				.entityPersister( ownerType.getTypeName() )
				.getEntityMetamodel();
	}
	else if ( persistenceType == Type.PersistenceType.MAPPED_SUPERCLASS ) {
		PersistentClass persistentClass =
				context.getPersistentClassHostingProperties( (MappedSuperclassTypeImpl<?>) ownerType );
		return context.getSessionFactory()
				.getMetamodel()
				.entityPersister( persistentClass.getClassName() )
				.getEntityMetamodel();
	}
	else {
		throw new AssertionFailure( "Cannot get the metamodel for PersistenceType: " + persistenceType );
	}
}
 
Example #11
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public boolean canProcessImmediately() {
	// We can process the FK immediately if it is a reference to the associated
	// entity's PK.
	//
	// There is an assumption here that the columns making up the FK have been bound.
	// We assume the caller checks that
	final PersistentClass referencedEntityBinding = mappingDocument.getMetadataCollector()
			.getEntityBinding( referencedEntityName );
	return referencedEntityBinding != null && referencedEntityAttributeName != null;

}
 
Example #12
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void bindDiscriminatorSubclassEntities(
		AbstractEntitySourceImpl entitySource,
		PersistentClass superEntityDescriptor) {
	for ( IdentifiableTypeSource subType : entitySource.getSubTypes() ) {
		final SingleTableSubclass subEntityDescriptor = new SingleTableSubclass( superEntityDescriptor, metadataBuildingContext );
		bindDiscriminatorSubclassEntity( (SubclassEntitySourceImpl) subType, subEntityDescriptor );
		superEntityDescriptor.addSubclass( subEntityDescriptor );
		entitySource.getLocalMetadataBuildingContext().getMetadataCollector().addEntityBinding( subEntityDescriptor );
	}
}
 
Example #13
Source File: PojoEntityTuplizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public PojoEntityTuplizer(EntityMetamodel entityMetamodel, PersistentClass mappedEntity) {
		super( entityMetamodel, mappedEntity );
		this.mappedClass = mappedEntity.getMappedClass();
		this.proxyInterface = mappedEntity.getProxyInterface();
		this.lifecycleImplementor = Lifecycle.class.isAssignableFrom( mappedClass );
		this.isBytecodeEnhanced = entityMetamodel.getBytecodeEnhancementMetadata().isEnhancedForLazyLoading();

		String[] getterNames = new String[propertySpan];
		String[] setterNames = new String[propertySpan];
		Class[] propTypes = new Class[propertySpan];
		for ( int i = 0; i < propertySpan; i++ ) {
			getterNames[i] = getters[i].getMethodName();
			setterNames[i] = setters[i].getMethodName();
			propTypes[i] = getters[i].getReturnType();
		}

		if ( hasCustomAccessors || !Environment.useReflectionOptimizer() ) {
			optimizer = null;
		}
		else {
			// todo : YUCK!!!
			optimizer = Environment.getBytecodeProvider().getReflectionOptimizer(
					mappedClass,
					getterNames,
					setterNames,
					propTypes
			);
//			optimizer = getFactory().getSettings().getBytecodeProvider().getReflectionOptimizer(
//					mappedClass, getterNames, setterNames, propTypes
//			);
		}
	}
 
Example #14
Source File: DynamicMapInstantiator.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public DynamicMapInstantiator(PersistentClass mappingInfo) {
	this.entityName = mappingInfo.getEntityName();
	isInstanceEntityNames.add( entityName );
	if ( mappingInfo.hasSubclasses() ) {
		Iterator itr = mappingInfo.getSubclassClosureIterator();
		while ( itr.hasNext() ) {
			final PersistentClass subclassInfo = ( PersistentClass ) itr.next();
			isInstanceEntityNames.add( subclassInfo.getEntityName() );
		}
	}
}
 
Example #15
Source File: AbstractPropertyMapping.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private PersistentClass getCommonPersistentClass(PersistentClass clazz1, PersistentClass clazz2) {
	while ( clazz2 != null && clazz2.getMappedClass() != null && clazz1.getMappedClass() != null && !clazz2.getMappedClass()
			.isAssignableFrom( clazz1.getMappedClass() ) ) {
		clazz2 = clazz2.getSuperclass();
	}
	return clazz2;
}
 
Example #16
Source File: MyEntityTuplizer.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
	// allows defining a custom proxy factory, which is responsible for
	// generating lazy proxies for a given entity.
	//
	// Here we simply use the default...
	return super.buildProxyFactory( persistentClass, idGetter, idSetter );
}
 
Example #17
Source File: JoinedSubclassEntityPersister.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private Set<String> processPersistentClassHierarchy(
		PersistentClass persistentClass,
		boolean isBase,
		SessionFactoryImplementor factory,
		String[][] mapping) {

	// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
	// collect all the class names that indicate that the "main table" of the given PersistentClass should be
	// included when one of the collected class names is used in TREAT
	final Set<String> classNames = new HashSet<String>();

	final Iterator itr = persistentClass.getDirectSubclasses();
	while ( itr.hasNext() ) {
		final Subclass subclass = (Subclass) itr.next();
		final Set<String> subclassSubclassNames = processPersistentClassHierarchy(
				subclass,
				false,
				factory,
				mapping
		);
		classNames.addAll( subclassSubclassNames );
	}

	classNames.add( persistentClass.getEntityName() );

	if ( ! isBase ) {
		MappedSuperclass msc = persistentClass.getSuperMappedSuperclass();
		while ( msc != null ) {
			classNames.add( msc.getMappedClass().getName() );
			msc = msc.getSuperMappedSuperclass();
		}

		associateSubclassNamesToSubclassTableIndexes( persistentClass, classNames, mapping, factory );
	}

	return classNames;
}
 
Example #18
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public org.hibernate.type.Type getReferencedPropertyType(String entityName, String propertyName) throws MappingException {
	final PersistentClass pc = entityBindingMap.get( entityName );
	if ( pc == null ) {
		throw new MappingException( "persistent class not known: " + entityName );
	}
	Property prop = pc.getReferencedProperty( propertyName );
	if ( prop == null ) {
		throw new MappingException(
				"property not known: " +
						entityName + '.' + propertyName
		);
	}
	return prop.getType();
}
 
Example #19
Source File: MasterDetailTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testMeta() throws Exception {
	PersistentClass clazz = getCfg().getClassMapping( Master.class.getName() );
	MetaAttribute meta = clazz.getMetaAttribute("foo");
	assertTrue( "foo".equals( meta.getValue() ) );
	meta = clazz.getProperty("name").getMetaAttribute("bar");
	assertTrue( meta.isMultiValued() );
}
 
Example #20
Source File: InFlightMetadataCollectorImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public org.hibernate.type.Type getIdentifierType(String entityName) throws MappingException {
	final PersistentClass pc = entityBindingMap.get( entityName );
	if ( pc == null ) {
		throw new MappingException( "persistent class not known: " + entityName );
	}
	return pc.getIdentifier().getType();
}
 
Example #21
Source File: Configuration.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private void validate() throws MappingException {
	Iterator iter = classes.values().iterator();
	while ( iter.hasNext() ) {
		( (PersistentClass) iter.next() ).validate( mapping );
	}
	iter = collections.values().iterator();
	while ( iter.hasNext() ) {
		( (Collection) iter.next() ).validate( mapping );
	}
}
 
Example #22
Source File: MetadataImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
public org.hibernate.type.Type getIdentifierType(String entityName) throws MappingException {
	final PersistentClass pc = entityBindingMap.get( entityName );
	if ( pc == null ) {
		throw new MappingException( "persistent class not known: " + entityName );
	}
	return pc.getIdentifier().getType();
}
 
Example #23
Source File: LegacyJpaImplNamingIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLegacyJpaImplNamingStrategy_whenCreateDatabase_thenGetStrategyNames() {
    Metadata metadata = MetadataExtractorIntegrator.INSTANCE.getMetadata();
    String entity = Account.class.getCanonicalName();
    PersistentClass persistentClass = metadata.getEntityBinding(entity);
    Table table = persistentClass.getTable();
    String physicalNameExpected = "Secondary_Email";
    String implicitNameExpected = "defaultEmail";
    String tableNameExpected = "Account";

    String tableNameCreated = table.getName();
    boolean columnNameIsQuoted = table
      .getColumn(3)
      .isQuoted();
    String physicalNameCreated = table
      .getColumn(3)
      .getName();
    String implicitNameCreated = table
      .getColumn(2)
      .getName();

    SoftAssertions.assertSoftly(softly -> {
        softly
          .assertThat(columnNameIsQuoted)
          .isTrue();
        softly
          .assertThat(tableNameCreated)
          .isEqualTo(tableNameExpected);
        softly
          .assertThat(physicalNameCreated)
          .isEqualTo(physicalNameExpected);
        softly
          .assertThat(implicitNameCreated)
          .isEqualTo(implicitNameExpected);
    });
}
 
Example #24
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
private void handleNaturalIdBinding(
		MappingDocument mappingDocument,
		PersistentClass entityBinding,
		Property attributeBinding,
		NaturalIdMutability naturalIdMutability) {
	if ( naturalIdMutability == NaturalIdMutability.NOT_NATURAL_ID ) {
		return;
	}

	attributeBinding.setNaturalIdentifier( true );

	if ( naturalIdMutability == NaturalIdMutability.IMMUTABLE ) {
		attributeBinding.setUpdateable( false );
	}

	NaturalIdUniqueKeyBinder ukBinder = mappingDocument.getMetadataCollector().locateNaturalIdUniqueKeyBinder(
			entityBinding.getEntityName()
	);

	if ( ukBinder == null ) {
		ukBinder = new NaturalIdUniqueKeyBinderImpl( mappingDocument, entityBinding );
		mappingDocument.getMetadataCollector().registerNaturalIdUniqueKeyBinder(
				entityBinding.getEntityName(),
				ukBinder
		);
	}

	ukBinder.addAttributeBinding( attributeBinding );
}
 
Example #25
Source File: PojoEntityTuplizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
@Override
protected Instantiator buildInstantiator(EntityMetamodel entityMetamodel, PersistentClass persistentClass) {
	if ( optimizer == null ) {
		return new PojoEntityInstantiator( entityMetamodel, persistentClass, null );
	}
	else {
		return new PojoEntityInstantiator( entityMetamodel, persistentClass, optimizer.getInstantiationOptimizer() );
	}
}
 
Example #26
Source File: PkDrivenByDefaultMapsIdSecondPass.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void doSecondPass(Map persistentClasses) throws MappingException {
	PersistentClass referencedEntity = (PersistentClass) persistentClasses.get( referencedEntityName );
	if ( referencedEntity == null ) {
		throw new AnnotationException(
				"Unknown entity name: " + referencedEntityName
		);
	}
	TableBinder.linkJoinColumnWithValueOverridingNameIfImplicit(
			referencedEntity,
			referencedEntity.getKey().getColumnIterator(),
			columns,
			value);
}
 
Example #27
Source File: BytecodeEnhancementMetadataPojoImpl.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public static BytecodeEnhancementMetadata from(PersistentClass persistentClass) {
	final Class mappedClass = persistentClass.getMappedClass();
	final boolean enhancedForLazyLoading = PersistentAttributeInterceptable.class.isAssignableFrom( mappedClass );
	final LazyAttributesMetadata lazyAttributesMetadata = enhancedForLazyLoading
			? LazyAttributesMetadata.from( persistentClass )
			: LazyAttributesMetadata.nonEnhanced( persistentClass.getEntityName() );

	return new BytecodeEnhancementMetadataPojoImpl(
			persistentClass.getEntityName(),
			mappedClass,
			enhancedForLazyLoading,
			lazyAttributesMetadata
	);
}
 
Example #28
Source File: PojoEntityTuplizer.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
protected ProxyFactory buildProxyFactoryInternal(
			PersistentClass persistentClass,
			Getter idGetter,
			Setter idSetter) {
		// TODO : YUCK!!!  fix after HHH-1907 is complete
		return Environment.getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory( getFactory() );
//		return getFactory().getSettings().getBytecodeProvider().getProxyFactoryFactory().buildProxyFactory();
	}
 
Example #29
Source File: HbmBinder.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
private static PersistentClass getSuperclass(Mappings mappings, Element subnode)
		throws MappingException {
	String extendsName = subnode.attributeValue( "extends" );
	PersistentClass superModel = mappings.getClass( extendsName );
	if ( superModel == null ) {
		String qualifiedExtendsName = getClassName( extendsName, mappings );
		superModel = mappings.getClass( qualifiedExtendsName );
	}

	if ( superModel == null ) {
		throw new MappingException( "Cannot extend unmapped class " + extendsName );
	}
	return superModel;
}
 
Example #30
Source File: ModelBinder.java    From lams with GNU General Public License v2.0 5 votes vote down vote up
public void process(InFlightMetadataCollector metadataCollector) {
	log.tracef(
			"Performing delayed property-ref handling [%s, %s, %s]",
			referencedEntityName,
			referencedPropertyName,
			sourceElementSynopsis
	);

	PersistentClass entityBinding = metadataCollector.getEntityBinding( referencedEntityName );
	if ( entityBinding == null ) {
		throw new MappingException(
				String.format(
						Locale.ENGLISH,
						"property-ref [%s] referenced an unmapped entity [%s]",
						sourceElementSynopsis,
						referencedEntityName
				),
				propertyRefOrigin
		);
	}

	Property propertyBinding = entityBinding.getReferencedProperty( referencedPropertyName );
	if ( propertyBinding == null ) {
		throw new MappingException(
				String.format(
						Locale.ENGLISH,
						"property-ref [%s] referenced an unknown entity property [%s#%s]",
						sourceElementSynopsis,
						referencedEntityName,
						referencedPropertyName
				),
				propertyRefOrigin
		);
	}

	if ( isUnique ) {
		( (SimpleValue) propertyBinding.getValue() ).setAlternateUniqueKey( true );
	}
}