Package org.hibernate.mapping

Examples of org.hibernate.mapping.PersistentClass


    }
  }

  private static PersistentClass makePersistentClass(InheritanceState inheritanceState, PersistentClass superEntity) {
    //we now know what kind of persistent entity it is
    PersistentClass persistentClass;
    //create persistent class
    if ( !inheritanceState.hasParents() ) {
      persistentClass = new RootClass();
    }
    else if ( InheritanceType.SINGLE_TABLE.equals( inheritanceState.getType() ) ) {
View Full Code Here


  private static PersistentClass getSuperEntity(XClass clazzToProcess, Map<XClass, InheritanceState> inheritanceStatePerClass, Mappings mappings, InheritanceState inheritanceState) {
    InheritanceState superEntityState = InheritanceState.getInheritanceStateOfSuperEntity(
        clazzToProcess, inheritanceStatePerClass
    );
    PersistentClass superEntity = superEntityState != null ?
        mappings.getClass(
            superEntityState.getClazz().getName()
        ) :
        null;
    if ( superEntity == null ) {
View Full Code Here

      Map<XClass, InheritanceState> inheritanceStatePerClass) {

    /*
     * Fill simple value and property since and Id is a property
     */
    PersistentClass persistentClass = propertyHolder.getPersistentClass();
    if ( !( persistentClass instanceof RootClass ) ) {
      throw new AnnotationException(
          "Unable to define/override @Id(s) on a subclass: "
              + propertyHolder.getEntityName()
      );
View Full Code Here

    if(entityName==null) {
      throw new MappingException( "<return alias='" + alias + "'> must specify either a class or entity-name");
    }
    LockMode lockMode = getLockMode( returnElem.attributeValue( "lock-mode" ) );

    PersistentClass pc = mappings.getClass( entityName );
    java.util.Map propertyResults = bindPropertyResults(alias, returnElem, pc, mappings );

    return new NativeSQLQueryRootReturn(
        alias,
        entityName,
View Full Code Here

          Component comp = (Component) value;
          parentPropIter = comp.getPropertyIterator();
        }
        else if ( value instanceof ToOne ) {
          ToOne toOne = (ToOne) value;
          PersistentClass referencedPc = mappings.getClass( toOne.getReferencedEntityName() );
          if ( toOne.getReferencedPropertyName() != null ) {
            try {
              parentPropIter = ( (Component) referencedPc.getRecursiveProperty( toOne.getReferencedPropertyName() ).getValue() ).getPropertyIterator();
            } catch (ClassCastException e) {
              throw new MappingException("dotted notation reference neither a component nor a many/one to one", e);
            }
          }
          else {
            try {
              if ( referencedPc.getIdentifierMapper() == null ) {
                parentPropIter = ( (Component) referencedPc.getIdentifierProperty().getValue() ).getPropertyIterator();
              }
              else {
                parentPropIter = referencedPc.getIdentifierMapper().getPropertyIterator();
              }
            }
            catch (ClassCastException e) {
              throw new MappingException("dotted notation reference neither a component nor a many/one to one", e);
            }
View Full Code Here

    public PersistentClass getClass(String entityName) {
      return classes.get( entityName );
    }

    public PersistentClass locatePersistentClassByEntityName(String entityName) {
      PersistentClass persistentClass = classes.get( entityName );
      if ( persistentClass == null ) {
        String actualEntityName = imports.get( entityName );
        if ( StringHelper.isNotEmpty( actualEntityName ) ) {
          persistentClass = classes.get( actualEntityName );
        }
View Full Code Here

        : buildBeanManagerListenerFactory( beanManagerRef );
    this.callbackProcessor = new LegacyCallbackProcessor( jpaListenerFactory, configuration.getReflectionManager() );

    Iterator classes = configuration.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass clazz = (PersistentClass) classes.next();
      if ( clazz.getClassName() == null ) {
        // we can have non java class persisted by hibernate
        continue;
      }
      callbackProcessor.processCallbacksForEntity( clazz.getClassName(), callbackRegistry );
    }

    for ( EventType eventType : EventType.values() ) {
      final EventListenerGroup eventListenerGroup = eventListenerRegistry.getEventListenerGroup( eventType );
      for ( Object listener : eventListenerGroup.listeners() ) {
View Full Code Here

      if ( getCacheConcurrencyStrategy() != null ) {

        Iterator iter = cfg.getClassMappings();
        while ( iter.hasNext() ) {
          PersistentClass clazz = (PersistentClass) iter.next();
          Iterator props = clazz.getPropertyClosureIterator();
          boolean hasLob = false;
          while ( props.hasNext() ) {
            Property prop = (Property) props.next();
            if ( prop.getValue().isSimpleValue() ) {
              String type = ( (SimpleValue) prop.getValue() ).getTypeName();
              if ( "blob".equals( type ) || "clob".equals( type ) ) hasLob = true;
              if ( Blob.class.getName().equals( type ) || Clob.class.getName().equals( type ) ) {
                hasLob = true;
              }
            }
          }
          if ( !hasLob && !clazz.isInherited() ) {
            cfg.setCacheConcurrencyStrategy(
                clazz.getEntityName(),
                getCacheConcurrencyStrategy()
            );
          }
        }
View Full Code Here

    //Generators:

    identifierGenerators = new HashMap();
    Iterator classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      PersistentClass model = (PersistentClass) classes.next();
      if ( !model.isInherited() ) {
        IdentifierGenerator generator = model.getIdentifier().createIdentifierGenerator(
            cfg.getIdentifierGeneratorFactory(),
            getDialect(),
                settings.getDefaultCatalogName(),
                settings.getDefaultSchemaName(),
                (RootClass) model
        );
        identifierGenerators.put( model.getEntityName(), generator );
      }
    }

    imports = new HashMap<String,String>( cfg.getImports() );

    ///////////////////////////////////////////////////////////////////////
    // Prepare persisters and link them up with their cache
    // region/access-strategy

    final RegionFactory regionFactory = cacheAccess.getRegionFactory();
    final String cacheRegionPrefix = settings.getCacheRegionPrefix() == null ? "" : settings.getCacheRegionPrefix() + ".";
    final PersisterFactory persisterFactory = serviceRegistry.getService( PersisterFactory.class );

    // todo : consider removing this silliness and just have EntityPersister directly implement ClassMetadata
    //    EntityPersister.getClassMetadata() for the internal impls simply "return this";
    //    collapsing those would allow us to remove this "extra" Map
    //
    // todo : similar for CollectionPersister/CollectionMetadata

    entityPersisters = new HashMap();
    Map entityAccessStrategies = new HashMap();
    Map<String,ClassMetadata> classMeta = new HashMap<String,ClassMetadata>();
    classes = cfg.getClassMappings();
    while ( classes.hasNext() ) {
      final PersistentClass model = (PersistentClass) classes.next();
      model.prepareTemporaryTables( mapping, getDialect() );
      final String cacheRegionName = cacheRegionPrefix + model.getRootClass().getCacheRegionName();
      // cache region is defined by the root-class in the hierarchy...
      EntityRegionAccessStrategy accessStrategy = ( EntityRegionAccessStrategy ) entityAccessStrategies.get( cacheRegionName );
      if ( accessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
        final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
        if ( accessType != null ) {
          LOG.tracef( "Building shared cache region for entity data [%s]", model.getEntityName() );
          EntityRegion entityRegion = regionFactory.buildEntityRegion( cacheRegionName, properties, CacheDataDescriptionImpl.decode( model ) );
          accessStrategy = entityRegion.buildAccessStrategy( accessType );
          entityAccessStrategies.put( cacheRegionName, accessStrategy );
          cacheAccess.addCacheRegion( cacheRegionName, entityRegion );
        }
      }

      NaturalIdRegionAccessStrategy naturalIdAccessStrategy = null;
      if ( model.hasNaturalId() && model.getNaturalIdCacheRegionName() != null ) {
        final String naturalIdCacheRegionName = cacheRegionPrefix + model.getNaturalIdCacheRegionName();
        naturalIdAccessStrategy = ( NaturalIdRegionAccessStrategy ) entityAccessStrategies.get( naturalIdCacheRegionName );

        if ( naturalIdAccessStrategy == null && settings.isSecondLevelCacheEnabled() ) {
          final CacheDataDescriptionImpl cacheDataDescription = CacheDataDescriptionImpl.decode( model );

          NaturalIdRegion naturalIdRegion = null;
          try {
            naturalIdRegion = regionFactory.buildNaturalIdRegion( naturalIdCacheRegionName, properties,
                cacheDataDescription );
          }
          catch ( UnsupportedOperationException e ) {
            LOG.warnf(
                "Shared cache region factory [%s] does not support natural id caching; " +
                    "shared NaturalId caching will be disabled for not be enabled for %s",
                regionFactory.getClass().getName(),
                model.getEntityName()
            );
          }

          if (naturalIdRegion != null) {
            naturalIdAccessStrategy = naturalIdRegion.buildAccessStrategy( regionFactory.getDefaultAccessType() );
            entityAccessStrategies.put( naturalIdCacheRegionName, naturalIdAccessStrategy );
            cacheAccess.addCacheRegionnaturalIdCacheRegionName, naturalIdRegion );
          }
        }
      }

      EntityPersister cp = persisterFactory.createEntityPersister(
          model,
          accessStrategy,
          naturalIdAccessStrategy,
          this,
          mapping
      );
      entityPersisters.put( model.getEntityName(), cp );
      classMeta.put( model.getEntityName(), cp.getClassMetadata() );
    }
    this.classMetadata = Collections.unmodifiableMap(classMeta);

    Map<String,Set<String>> tmpEntityToCollectionRoleMap = new HashMap<String,Set<String>>();
    collectionPersisters = new HashMap<String,CollectionPersister>();
    Map<String,CollectionMetadata> tmpCollectionMetadata = new HashMap<String,CollectionMetadata>();
    Iterator collections = cfg.getCollectionMappings();
    while ( collections.hasNext() ) {
      Collection model = (Collection) collections.next();
      final String cacheRegionName = cacheRegionPrefix + model.getCacheRegionName();
      final AccessType accessType = AccessType.fromExternalName( model.getCacheConcurrencyStrategy() );
      CollectionRegionAccessStrategy accessStrategy = null;
      if ( accessType != null && settings.isSecondLevelCacheEnabled() ) {
        LOG.tracev( "Building shared cache region for collection data [{0}]", model.getRole() );
        CollectionRegion collectionRegion = regionFactory.buildCollectionRegion( cacheRegionName, properties, CacheDataDescriptionImpl
            .decode( model ) );
        accessStrategy = collectionRegion.buildAccessStrategy( accessType );
        entityAccessStrategies.put( cacheRegionName, accessStrategy );
        cacheAccess.addCacheRegion( cacheRegionName, collectionRegion );
      }
      CollectionPersister persister = persisterFactory.createCollectionPersister(
          cfg,
          model,
          accessStrategy,
          this
      ) ;
      collectionPersisters.put( model.getRole(), persister );
      tmpCollectionMetadata.put( model.getRole(), persister.getCollectionMetadata() );
      Type indexType = persister.getIndexType();
      if ( indexType != null && indexType.isAssociationType() && !indexType.isAnyType() ) {
        String entityName = ( ( AssociationType ) indexType ).getAssociatedEntityName( this );
        Set roles = tmpEntityToCollectionRoleMap.get( entityName );
        if ( roles == null ) {
View Full Code Here

      boolean inverse,
      Mappings mappings) {
    //associated entity only used for more precise exception, yuk!
    if ( columns[0].isImplicit() || StringHelper.isNotEmpty( columns[0].getMappedBy() ) ) return;
    int fkEnum = Ejb3JoinColumn.checkReferencedColumnsType( columns, ownerEntity, mappings );
    PersistentClass associatedClass = columns[0].getPropertyHolder() != null ?
        columns[0].getPropertyHolder().getPersistentClass() :
        null;
    if ( Ejb3JoinColumn.NON_PK_REFERENCE == fkEnum ) {
      /**
       * Create a synthetic property to refer to including an
       * embedded component value containing all the properties
       * mapped to the referenced columns
       * We need to shallow copy those properties to mark them
       * as non insertable / non updatable
       */
      StringBuilder propertyNameBuffer = new StringBuilder( "_" );
      propertyNameBuffer.append( associatedClass.getEntityName().replace( '.', '_' ) );
      propertyNameBuffer.append( "_" ).append( columns[0].getPropertyName().replace( '.', '_' ) );
      String syntheticPropertyName = propertyNameBuffer.toString();
      //find properties associated to a certain column
      Object columnOwner = findColumnOwner( ownerEntity, columns[0].getReferencedColumn(), mappings );
      List<Property> properties = findPropertiesByColumns( columnOwner, columns, mappings );
      //create an embeddable component
                        Property synthProp = null;
      if ( properties != null ) {
                        //todo how about properties.size() == 1, this should be much simpler
        Component embeddedComp = columnOwner instanceof PersistentClass ?
            new Component( mappings, (PersistentClass) columnOwner ) :
            new Component( mappings, (Join) columnOwner );
        embeddedComp.setEmbedded( true );
        embeddedComp.setNodeName( syntheticPropertyName );
        embeddedComp.setComponentClassName( embeddedComp.getOwner().getClassName() );
        for (Property property : properties) {
          Property clone = BinderHelper.shallowCopy( property );
          clone.setInsertable( false );
          clone.setUpdateable( false );
          clone.setNaturalIdentifier( false );
          clone.setValueGenerationStrategy( property.getValueGenerationStrategy() );
          embeddedComp.addProperty( clone );
                                }
                                    synthProp = new SyntheticProperty();
        synthProp.setName( syntheticPropertyName );
        synthProp.setNodeName( syntheticPropertyName );
        synthProp.setPersistentClass( ownerEntity );
        synthProp.setUpdateable( false );
        synthProp.setInsertable( false );
        synthProp.setValue( embeddedComp );
        synthProp.setPropertyAccessorName( "embedded" );
        ownerEntity.addProperty( synthProp );
                                //make it unique
        TableBinder.createUniqueConstraint( embeddedComp );
                            }
      else {
        //TODO use a ToOne type doing a second select
        StringBuilder columnsList = new StringBuilder();
        columnsList.append( "referencedColumnNames(" );
        for (Ejb3JoinColumn column : columns) {
          columnsList.append( column.getReferencedColumn() ).append( ", " );
        }
        columnsList.setLength( columnsList.length() - 2 );
        columnsList.append( ") " );

        if ( associatedEntity != null ) {
          //overidden destination
          columnsList.append( "of " )
              .append( associatedEntity.getEntityName() )
              .append( "." )
              .append( columns[0].getPropertyName() )
              .append( " " );
        }
        else {
          if ( columns[0].getPropertyHolder() != null ) {
            columnsList.append( "of " )
                .append( columns[0].getPropertyHolder().getEntityName() )
                .append( "." )
                .append( columns[0].getPropertyName() )
                .append( " " );
          }
        }
        columnsList.append( "referencing " )
            .append( ownerEntity.getEntityName() )
            .append( " not mapped to a single property" );
        throw new AnnotationException( columnsList.toString() );
      }

      /**
       * creating the property ref to the new synthetic property
       */
      if ( value instanceof ToOne ) {
        ( (ToOne) value ).setReferencedPropertyName( syntheticPropertyName );
        ( (ToOne) value ).setReferenceToPrimaryKey( syntheticPropertyName == null );
        mappings.addUniquePropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
      }
      else if ( value instanceof Collection ) {
        ( (Collection) value ).setReferencedPropertyName( syntheticPropertyName );
        //not unique because we could create a mtm wo association table
        mappings.addPropertyReference( ownerEntity.getEntityName(), syntheticPropertyName );
      }
      else {
        throw new AssertionFailure(
            "Do a property ref on an unexpected Value type: "
                + value.getClass().getName()
        );
      }
      mappings.addPropertyReferencedAssociation(
          ( inverse ? "inverse__" : "" ) + associatedClass.getEntityName(),
          columns[0].getPropertyName(),
          syntheticPropertyName
      );
    }
  }
View Full Code Here

TOP

Related Classes of org.hibernate.mapping.PersistentClass

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.