Search in sources :

Example 1 with EntityMode

use of org.hibernate.EntityMode in project jbosstools-hibernate by jbosstools.

the class EntityMetamodelFacadeTest method setUp.

@SuppressWarnings("serial")
@Before
public void setUp() throws Exception {
    Configuration configuration = new Configuration();
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
    SessionFactoryImplementor sfi = new SessionFactoryImpl(configuration, null, configuration.buildSettings(), null, null);
    RootClass rc = new RootClass();
    Table t = new Table("foobar");
    rc.setTable(t);
    Column c = new Column("foo");
    t.addColumn(c);
    ArrayList<Column> keyList = new ArrayList<>();
    keyList.add(c);
    t.createUniqueKey(keyList);
    SimpleValue sv = new SimpleValue();
    sv.setNullValue("null");
    sv.setTypeName(Integer.class.getName());
    sv.addColumn(c);
    rc.setEntityName("foobar");
    rc.setIdentifier(sv);
    entityMetamodel = new EntityMetamodel(rc, sfi) {

        @Override
        public EntityTuplizer getTuplizer(EntityMode entityMode) {
            return (EntityTuplizer) Proxy.newProxyInstance(FACADE_FACTORY.getClassLoader(), new Class[] { EntityTuplizer.class }, new TestInvocationHandler());
        }

        @Override
        public Integer getPropertyIndexOrNull(String id) {
            methodName = "getPropertyIndexOrNull";
            arguments = new Object[] { id };
            return INDEX;
        }
    };
    entityMetamodelFacade = new EntityMetamodelFacadeImpl(FACADE_FACTORY, entityMetamodel);
}
Also used : RootClass(org.hibernate.mapping.RootClass) EntityTuplizer(org.hibernate.tuple.entity.EntityTuplizer) Table(org.hibernate.mapping.Table) Configuration(org.hibernate.cfg.Configuration) SessionFactoryImplementor(org.hibernate.engine.SessionFactoryImplementor) ArrayList(java.util.ArrayList) SimpleValue(org.hibernate.mapping.SimpleValue) EntityMode(org.hibernate.EntityMode) Column(org.hibernate.mapping.Column) SessionFactoryImpl(org.hibernate.impl.SessionFactoryImpl) EntityMetamodel(org.hibernate.tuple.entity.EntityMetamodel) IEntityMetamodel(org.jboss.tools.hibernate.runtime.spi.IEntityMetamodel) Before(org.junit.Before)

Example 2 with EntityMode

use of org.hibernate.EntityMode in project hibernate-orm by hibernate.

the class ModelBinder method bindBasicEntityValues.

private void bindBasicEntityValues(MappingDocument sourceDocument, AbstractEntitySourceImpl entitySource, PersistentClass entityDescriptor) {
    entityDescriptor.setEntityName(entitySource.getEntityNamingSource().getEntityName());
    entityDescriptor.setJpaEntityName(entitySource.getEntityNamingSource().getJpaEntityName());
    entityDescriptor.setClassName(entitySource.getEntityNamingSource().getClassName());
    entityDescriptor.setDiscriminatorValue(entitySource.getDiscriminatorMatchValue() != null ? entitySource.getDiscriminatorMatchValue() : entityDescriptor.getEntityName());
    // NOTE : entitySource#isLazy already accounts for MappingDefaults#areEntitiesImplicitlyLazy
    if (StringHelper.isNotEmpty(entitySource.getProxy())) {
        final String qualifiedProxyName = sourceDocument.qualifyClassName(entitySource.getProxy());
        entityDescriptor.setProxyInterfaceName(qualifiedProxyName);
        entityDescriptor.setLazy(true);
    } else if (entitySource.isLazy()) {
        entityDescriptor.setProxyInterfaceName(entityDescriptor.getClassName());
        entityDescriptor.setLazy(true);
    } else {
        entityDescriptor.setProxyInterfaceName(null);
        entityDescriptor.setLazy(false);
    }
    entityDescriptor.setAbstract(entitySource.isAbstract());
    sourceDocument.getMetadataCollector().addImport(entitySource.getEntityNamingSource().getEntityName(), entitySource.getEntityNamingSource().getEntityName());
    if (sourceDocument.getMappingDefaults().isAutoImportEnabled() && entitySource.getEntityNamingSource().getEntityName().indexOf('.') > 0) {
        sourceDocument.getMetadataCollector().addImport(StringHelper.unqualify(entitySource.getEntityNamingSource().getEntityName()), entitySource.getEntityNamingSource().getEntityName());
    }
    if (entitySource.getTuplizerClassMap() != null) {
        if (entitySource.getTuplizerClassMap().size() > 1) {
            DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfMultipleEntityModeSupport();
        }
        for (Map.Entry<EntityMode, String> tuplizerEntry : entitySource.getTuplizerClassMap().entrySet()) {
            entityDescriptor.addTuplizer(tuplizerEntry.getKey(), tuplizerEntry.getValue());
        }
    }
    if (StringHelper.isNotEmpty(entitySource.getXmlNodeName())) {
        DeprecationLogger.DEPRECATION_LOGGER.logDeprecationOfDomEntityModeSupport();
    }
    entityDescriptor.setDynamicInsert(entitySource.isDynamicInsert());
    entityDescriptor.setDynamicUpdate(entitySource.isDynamicUpdate());
    entityDescriptor.setBatchSize(entitySource.getBatchSize());
    entityDescriptor.setSelectBeforeUpdate(entitySource.isSelectBeforeUpdate());
    if (StringHelper.isNotEmpty(entitySource.getCustomPersisterClassName())) {
        try {
            entityDescriptor.setEntityPersisterClass(sourceDocument.getBootstrapContext().getClassLoaderAccess().classForName(entitySource.getCustomPersisterClassName()));
        } catch (ClassLoadingException e) {
            throw new MappingException(String.format(Locale.ENGLISH, "Unable to load specified persister class : %s", entitySource.getCustomPersisterClassName()), e, sourceDocument.getOrigin());
        }
    }
    bindCustomSql(sourceDocument, entitySource, entityDescriptor);
    final JdbcEnvironment jdbcEnvironment = sourceDocument.getMetadataCollector().getDatabase().getJdbcEnvironment();
    for (String tableName : entitySource.getSynchronizedTableNames()) {
        final Identifier physicalTableName = sourceDocument.getBuildingOptions().getPhysicalNamingStrategy().toPhysicalTableName(jdbcEnvironment.getIdentifierHelper().toIdentifier(tableName), jdbcEnvironment);
        entityDescriptor.addSynchronizedTable(physicalTableName.render(jdbcEnvironment.getDialect()));
    }
    for (FilterSource filterSource : entitySource.getFilterSources()) {
        String condition = filterSource.getCondition();
        if (condition == null) {
            final FilterDefinition filterDefinition = sourceDocument.getMetadataCollector().getFilterDefinition(filterSource.getName());
            if (filterDefinition != null) {
                condition = filterDefinition.getDefaultFilterCondition();
            }
        }
        entityDescriptor.addFilter(filterSource.getName(), condition, filterSource.shouldAutoInjectAliases(), filterSource.getAliasToTableMap(), filterSource.getAliasToEntityMap());
    }
    for (JaxbHbmNamedQueryType namedQuery : entitySource.getNamedQueries()) {
        NamedQueryBinder.processNamedQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
    }
    for (JaxbHbmNamedNativeQueryType namedQuery : entitySource.getNamedNativeQueries()) {
        NamedQueryBinder.processNamedNativeQuery(sourceDocument, namedQuery, entitySource.getEntityNamingSource().getEntityName() + ".");
    }
    entityDescriptor.setMetaAttributes(entitySource.getToolingHintContext().getMetaAttributeMap());
}
Also used : FilterDefinition(org.hibernate.engine.spi.FilterDefinition) Identifier(org.hibernate.boot.model.naming.Identifier) FilterSource(org.hibernate.boot.model.source.spi.FilterSource) JaxbHbmNamedNativeQueryType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedNativeQueryType) JaxbHbmNamedQueryType(org.hibernate.boot.jaxb.hbm.spi.JaxbHbmNamedQueryType) ClassLoadingException(org.hibernate.boot.registry.classloading.spi.ClassLoadingException) Map(java.util.Map) HashMap(java.util.HashMap) JdbcEnvironment(org.hibernate.engine.jdbc.env.spi.JdbcEnvironment) EntityMode(org.hibernate.EntityMode) MappingException(org.hibernate.boot.MappingException)

Example 3 with EntityMode

use of org.hibernate.EntityMode in project hibernate-orm by hibernate.

the class CollectionType method getCollection.

/**
 * instantiate a collection wrapper (called when loading an object)
 *
 * @param key The collection owner key
 * @param session The session from which the request is originating.
 * @param owner The collection owner
 * @return The collection
 */
public Object getCollection(Serializable key, SharedSessionContractImplementor session, Object owner, Boolean overridingEager) {
    CollectionPersister persister = getPersister(session);
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityMode entityMode = persister.getOwnerEntityPersister().getEntityMode();
    // check if collection is currently being loaded
    PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection(persister, key);
    if (collection == null) {
        // check if it is already completely loaded, but unowned
        collection = persistenceContext.useUnownedCollection(new CollectionKey(persister, key, entityMode));
        if (collection == null) {
            collection = persistenceContext.getCollection(new CollectionKey(persister, key, entityMode));
            if (collection == null) {
                // create a new collection wrapper, to be initialized later
                collection = instantiate(session, persister, key);
                collection.setOwner(owner);
                persistenceContext.addUninitializedCollection(persister, collection, key);
                // some collections are not lazy:
                boolean eager = overridingEager != null ? overridingEager : !persister.isLazy();
                if (initializeImmediately()) {
                    session.initializeCollection(collection, false);
                } else if (eager) {
                    persistenceContext.addNonLazyCollection(collection);
                }
                if (hasHolder()) {
                    session.getPersistenceContext().addCollectionHolder(collection);
                }
            }
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracef("Created collection wrapper: %s", MessageHelper.collectionInfoString(persister, collection, key, session));
        }
    }
    collection.setOwner(owner);
    return collection.getValue();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CollectionKey(org.hibernate.engine.spi.CollectionKey) EntityMode(org.hibernate.EntityMode)

Example 4 with EntityMode

use of org.hibernate.EntityMode in project hibernate-orm by hibernate.

the class AnnotationBinder method setupComponentTuplizer.

private static void setupComponentTuplizer(XProperty property, Component component) {
    if (property == null) {
        return;
    }
    if (property.isAnnotationPresent(Tuplizers.class)) {
        for (Tuplizer tuplizer : property.getAnnotation(Tuplizers.class).value()) {
            EntityMode mode = EntityMode.parse(tuplizer.entityMode());
            // todo tuplizer.entityModeType
            component.addTuplizer(mode, tuplizer.impl().getName());
        }
    }
    if (property.isAnnotationPresent(Tuplizer.class)) {
        Tuplizer tuplizer = property.getAnnotation(Tuplizer.class);
        EntityMode mode = EntityMode.parse(tuplizer.entityMode());
        // todo tuplizer.entityModeType
        component.addTuplizer(mode, tuplizer.impl().getName());
    }
}
Also used : Tuplizer(org.hibernate.annotations.Tuplizer) Tuplizers(org.hibernate.annotations.Tuplizers) EntityMode(org.hibernate.EntityMode)

Example 5 with EntityMode

use of org.hibernate.EntityMode in project hibernate-orm by hibernate.

the class CollectionType method getCollection.

/**
	 * instantiate a collection wrapper (called when loading an object)
	 *
	 * @param key The collection owner key
	 * @param session The session from which the request is originating.
	 * @param owner The collection owner
	 * @return The collection
	 */
public Object getCollection(Serializable key, SharedSessionContractImplementor session, Object owner) {
    CollectionPersister persister = getPersister(session);
    final PersistenceContext persistenceContext = session.getPersistenceContext();
    final EntityMode entityMode = persister.getOwnerEntityPersister().getEntityMode();
    // check if collection is currently being loaded
    PersistentCollection collection = persistenceContext.getLoadContexts().locateLoadingCollection(persister, key);
    if (collection == null) {
        // check if it is already completely loaded, but unowned
        collection = persistenceContext.useUnownedCollection(new CollectionKey(persister, key, entityMode));
        if (collection == null) {
            collection = persistenceContext.getCollection(new CollectionKey(persister, key, entityMode));
            if (collection == null) {
                // create a new collection wrapper, to be initialized later
                collection = instantiate(session, persister, key);
                collection.setOwner(owner);
                persistenceContext.addUninitializedCollection(persister, collection, key);
                // some collections are not lazy:
                if (initializeImmediately()) {
                    session.initializeCollection(collection, false);
                } else if (!persister.isLazy()) {
                    persistenceContext.addNonLazyCollection(collection);
                }
                if (hasHolder()) {
                    session.getPersistenceContext().addCollectionHolder(collection);
                }
            }
        }
        if (LOG.isTraceEnabled()) {
            LOG.tracef("Created collection wrapper: %s", MessageHelper.collectionInfoString(persister, collection, key, session));
        }
    }
    collection.setOwner(owner);
    return collection.getValue();
}
Also used : PersistentCollection(org.hibernate.collection.spi.PersistentCollection) AbstractPersistentCollection(org.hibernate.collection.internal.AbstractPersistentCollection) CollectionPersister(org.hibernate.persister.collection.CollectionPersister) PersistenceContext(org.hibernate.engine.spi.PersistenceContext) CollectionKey(org.hibernate.engine.spi.CollectionKey) EntityMode(org.hibernate.EntityMode)

Aggregations

EntityMode (org.hibernate.EntityMode)12 RootClass (org.hibernate.mapping.RootClass)4 ArrayList (java.util.ArrayList)3 PersistentCollection (org.hibernate.collection.spi.PersistentCollection)3 CollectionKey (org.hibernate.engine.spi.CollectionKey)3 Column (org.hibernate.mapping.Column)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Tuplizer (org.hibernate.annotations.Tuplizer)2 Tuplizers (org.hibernate.annotations.Tuplizers)2 ClassLoadingException (org.hibernate.boot.registry.classloading.spi.ClassLoadingException)2 Configuration (org.hibernate.cfg.Configuration)2 AbstractPersistentCollection (org.hibernate.collection.internal.AbstractPersistentCollection)2 SessionFactoryImplementor (org.hibernate.engine.SessionFactoryImplementor)2 JdbcEnvironment (org.hibernate.engine.jdbc.env.spi.JdbcEnvironment)2 FilterDefinition (org.hibernate.engine.spi.FilterDefinition)2 PersistenceContext (org.hibernate.engine.spi.PersistenceContext)2 SessionFactoryImpl (org.hibernate.impl.SessionFactoryImpl)2 PersistentClass (org.hibernate.mapping.PersistentClass)2 SimpleValue (org.hibernate.mapping.SimpleValue)2