entityintrospector.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 843 行 · 第 1/2 页

JAVA
843
字号
    if (! _annotationCfg.isNull())      return _annotationCfg.getEntityConfig();        getInternalMappedSuperclassConfig(type, _annotationCfg);    return _annotationCfg.getMappedSuperclassConfig();  }  private EntityType introspectParent(Class parentClass)    throws SQLException  {    if (parentClass == null)      return null;    getInternalEntityConfig(parentClass, _annotationCfg);    if (! _annotationCfg.isNull())      return (EntityType) _configManager.introspect(parentClass);    else if (parentClass.isAnnotationPresent(javax.persistence.Entity.class))      return (EntityType) _configManager.introspect(parentClass);    else if (parentClass.isAnnotationPresent(javax.persistence.MappedSuperclass.class))      return (EntityType) _configManager.introspect(parentClass);    else      return null;  }  private void introspectInheritance(Class type,				     EntityType entityType,				     EntityType parentType)    throws SQLException  {    // Inheritance annotation/configuration is specified    // on the entity class that is the root of the entity    // class hierarachy.    InheritanceConfig inheritanceConfig;    Inheritance inheritanceAnn;    getInternalInheritanceConfig(type, _annotationCfg);    inheritanceAnn = (Inheritance) _annotationCfg.getAnnotation();    inheritanceConfig = _annotationCfg.getInheritanceConfig();    boolean hasInheritance = ! _annotationCfg.isNull();    if (! hasInheritance &&	(parentType == null || ! parentType.isEntity()))      return;    introspectDiscriminatorValue(type, entityType);        if (parentType != null) {      if (hasInheritance)	throw new ConfigException(L.l("'{0}' cannot have @Inheritance. It must be specified on the entity class that is the root of the entity class hierarchy.",				      type));            EntityType rootType = entityType.getRootType();      rootType.addSubClass(entityType);      getInternalPrimaryKeyJoinColumnConfig(type, _annotationCfg);      PrimaryKeyJoinColumn joinAnn	= (PrimaryKeyJoinColumn) _annotationCfg.getAnnotation();      PrimaryKeyJoinColumnConfig primaryKeyJoinColumnConfig	= _annotationCfg.getPrimaryKeyJoinColumnConfig();      if (rootType.isJoinedSubClass()) {        linkInheritanceTable(rootType.getTable(),                             entityType.getTable(),                             joinAnn,                             primaryKeyJoinColumnConfig);        entityType.setId(new SubId(entityType, rootType));      }      return;    }        if (! hasInheritance)      return;    if ((inheritanceAnn != null) || (inheritanceConfig != null))      introspectInheritance(_persistenceUnit, entityType, type,			    inheritanceAnn, inheritanceConfig);  }  private void introspectDiscriminatorValue(Class type, EntityType entityType)  {    DiscriminatorValue discValueAnn = (DiscriminatorValue) type.getAnnotation(DiscriminatorValue.class);    String discriminatorValue = null;    if (discValueAnn != null)      discriminatorValue = discValueAnn.value();    if (discriminatorValue == null || discriminatorValue.equals("")) {      String name = entityType.getBeanClass().getSimpleName();      discriminatorValue = name;    }    entityType.setDiscriminatorValue(discriminatorValue);  }  /**   * Introspects the Inheritance   */  void introspectInheritance(AmberPersistenceUnit persistenceUnit,                             EntityType entityType,                             Class type,                             Inheritance inheritanceAnn,                             InheritanceConfig inheritanceConfig)    throws ConfigException, SQLException  {    InheritanceType strategy;    if (inheritanceAnn != null)      strategy = inheritanceAnn.strategy();    else      strategy = inheritanceConfig.getStrategy();    switch (strategy) {    case JOINED:      entityType.setJoinedSubClass(true);      break;    }    getInternalDiscriminatorColumnConfig(type, _annotationCfg);    DiscriminatorColumn discriminatorAnn = (DiscriminatorColumn) _annotationCfg.getAnnotation();    DiscriminatorColumnConfig discriminatorConfig = _annotationCfg.getDiscriminatorColumnConfig();    String columnName = null;    if (discriminatorAnn != null)      columnName = discriminatorAnn.name();    if (columnName == null || columnName.equals(""))      columnName = "DTYPE";    AmberType columnType = null;    DiscriminatorType discType = DiscriminatorType.STRING;    if (discriminatorAnn != null)      discType = discriminatorAnn.discriminatorType();    switch (discType) {    case STRING:      columnType = StringType.create();      break;    case CHAR:      columnType = PrimitiveCharType.create();      break;    case INTEGER:      columnType = PrimitiveIntType.create();      break;    default:      throw new IllegalStateException();    }    AmberTable table = entityType.getTable();    // jpa/0gg0    if (table == null)      return;    com.caucho.amber.table.AmberColumn column      = table.createColumn(columnName, columnType);    if (discriminatorAnn != null) {//      column.setNotNull(! discriminatorAnn.nullable());      column.setLength(discriminatorAnn.length());      if (! "".equals(discriminatorAnn.columnDefinition()))        column.setSQLType(discriminatorAnn.columnDefinition());    }    else {      column.setNotNull(true);      column.setLength(10);    }    entityType.setDiscriminator(column);  }  private void introspectTable(Class type,                               EntityType entityType,			       EntityType parentType)  {    // XXX: need better test    boolean isEntity = ! (entityType instanceof MappedSuperclassType);        AmberTable table = null;    getInternalTableConfig(type, _annotationCfg);    Table tableAnn = (Table) _annotationCfg.getAnnotation();    TableConfig tableConfig = _annotationCfg.getTableConfig();    String tableName = null;    if (tableAnn != null)      tableName = tableAnn.name();    else if (tableConfig != null)      tableName = tableConfig.getName();    // jpa/0gg0, jpa/0gg2    if (isEntity) { // && ! type.isAbstract()) {      boolean hasTableConfig = true;      if (tableName == null || tableName.equals("")) {	hasTableConfig = false;	tableName = toSqlName(entityType.getName());      }      /*      InheritanceType strategy = null;            if (parentType != null)        strategy = parentType.getInheritanceStrategy();      if (inheritanceAnn != null)	strategy = (InheritanceType) inheritanceAnn.get("strategy");      else if (inheritanceConfig != null)	strategy = inheritanceConfig.getStrategy();       */      // jpa/0gg2      if (! entityType.isEntity())        return;      /*      if (type.isAbstract()	  && strategy != InheritanceType.JOINED	  && ! hasTableConfig) {	// jpa/0gg0      }       */      else if (parentType == null || parentType.getTable() == null) {	entityType.setTable(_persistenceUnit.createTable(tableName));      }      else if (parentType.isJoinedSubClass()) {	entityType.setTable(_persistenceUnit.createTable(tableName));	EntityType rootType = parentType.getRootType();	Class rootClass = rootType.getBeanClass();	getInternalTableConfig(rootClass, _annotationCfg);	Table rootTableAnn = (Table) _annotationCfg.getAnnotation();	TableConfig rootTableConfig = _annotationCfg.getTableConfig();	String rootTableName = null;	if (rootTableAnn != null)	  rootTableName = rootTableAnn.name();	else if (rootTableConfig != null)	  rootTableName = rootTableConfig.getName();	if (rootTableName == null || rootTableName.equals("")) {	  String rootEntityName = rootType.getName();	  rootTableName = toSqlName(rootEntityName);	}	entityType.setRootTableName(rootTableName);      }      else	entityType.setTable(parentType.getTable());    }  }  private void introspectSecondaryTable(EntityType entityType, Class type)  {    getInternalSecondaryTableConfig(type, _annotationCfg);    SecondaryTable secondaryTableAnn = (SecondaryTable) _annotationCfg.getAnnotation();    SecondaryTableConfig secondaryTableConfig = _annotationCfg.getSecondaryTableConfig();    AmberTable secondaryTable = null;    if ((secondaryTableAnn != null) || (secondaryTableConfig != null)) {      String secondaryName;      if (secondaryTableAnn != null)	secondaryName = secondaryTableAnn.name();      else	secondaryName = secondaryTableConfig.getName();      secondaryTable = _persistenceUnit.createTable(secondaryName);      entityType.addSecondaryTable(secondaryTable);      // XXX: pk    }    if (secondaryTableAnn != null) {      PrimaryKeyJoinColumn[] joinAnn = secondaryTableAnn.pkJoinColumns();      linkSecondaryTable(entityType.getTable(),			 secondaryTable,			 joinAnn);    }  }  private void introspectTableCache(EntityType entityType, Class type)  {    AmberTableCache tableCache = (AmberTableCache) type.getAnnotation(AmberTableCache.class);    if (tableCache != null) {      entityType.getTable().setReadOnly(tableCache.readOnly());      long cacheTimeout = Period.toPeriod(tableCache.timeout());      entityType.getTable().setCacheTimeout(cacheTimeout);    }  }  private void introspectAttributeOverrides(EntityType entityType,                                            Class type)  {    EntityType parent = entityType.getParentType();    if (parent == null)      return;    boolean isAbstract = Modifier.isAbstract(parent.getBeanClass().getModifiers());    if (parent.isEntity() && ! isAbstract)      return;    HashMap<String,ColumnConfig> overrideMap      = new HashMap<String,ColumnConfig>();        getInternalAttributeOverrideConfig(type, _annotationCfg);    AttributeOverride attributeOverrideAnn = (AttributeOverride) _annotationCfg.getAnnotation();    boolean hasAttributeOverride = (attributeOverrideAnn != null);    AttributeOverrides attributeOverridesAnn      = (AttributeOverrides) type.getAnnotation(AttributeOverrides.class);    ArrayList<AttributeOverrideConfig> attributeOverrideList = null;    EntityConfig entityConfig = getEntityConfig(type.getName());    if (entityConfig != null)      attributeOverrideList = entityConfig.getAttributeOverrideList();    boolean hasAttributeOverrides = false;    if ((attributeOverrideList != null) &&	(attributeOverrideList.size() > 0)) {      hasAttributeOverrides = true;    }    else if (attributeOverridesAnn != null)      hasAttributeOverrides = true;    if (hasAttributeOverride && hasAttributeOverrides)      throw new ConfigException(L.l("{0} may not have both @AttributeOverride and @AttributeOverrides",				    type));    if (attributeOverrideList == null)      attributeOverrideList = new ArrayList<AttributeOverrideConfig>();    if (hasAttributeOverride) {      // Convert annotation to configuration.      AttributeOverrideConfig attOverrideConfig	= convertAttributeOverrideAnnotationToConfig(attributeOverrideAnn);      attributeOverrideList.add(attOverrideConfig);    }    else if (hasAttributeOverrides) {      if (attributeOverrideList.size() > 0) {	// OK: attributeOverrideList came from orm.xml      }      else {	// Convert annotations to configurations.	AttributeOverride attOverridesAnn[]	  = attributeOverridesAnn.value();	AttributeOverrideConfig attOverrideConfig;        /* XXX:	for (int i = 0; i < attOverridesAnn.length; i++) {	  attOverrideConfig	    = convertAttributeOverrideAnnotationToConfig((JAnnotation) attOverridesAnn[i]);	  attributeOverrideList.add(attOverrideConfig);	}         * */      }    }    for (AttributeOverrideConfig override : attributeOverrideList) {      overrideMap.put(override.getName(), override.getColumn());    }    _depCompletions.add(new AttributeOverrideCompletion(this, entityType, type,							overrideMap));  }  boolean isField(Class type,                  AbstractEnhancedConfig typeConfig)    throws ConfigException  {    for (Method method : type.getDeclaredMethods()) {      Annotation ann[] = method.getDeclaredAnnotations();      for (int i = 0; ann != null && i < ann.length; i++) {	if (ann[i] instanceof Basic || ann[i] instanceof Column)	  return false;      }    }    return true;  }  private boolean isPropertyAnnotation(Class cl)  {    return (Basic.class.equals(cl)	    || Column.class.equals(cl));  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?