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); }
/** Introspects. */ public BeanType introspect(Class type) throws ConfigException, SQLException { BeanType beanType = null; try { EntityType entityType = null; EntityType parentType = introspectParent(type.getSuperclass()); entityType = introspectEntityType(type, parentType); if (entityType == null) entityType = introspectMappedType(type, parentType); if (entityType == null) return introspectEmbeddableType(type); beanType = entityType; // jpa/0ge2 entityType.setInstanceClassName(type.getName() + "__ResinExt"); entityType.setEnhanced(true); MappedSuperclassConfig mappedSuperOrEntityConfig = introspectEntityConfig(type); entityType.setParentType(parentType); introspectTable(type, entityType, parentType); // inheritance must be after table since it adds columns introspectInheritance(type, entityType, parentType); introspectTableCache(entityType, type); getInternalIdClassConfig(type, _annotationCfg); IdClass idClassAnn = (IdClass) _annotationCfg.getAnnotation(); IdClassConfig idClassConfig = _annotationCfg.getIdClassConfig(); Class idClass = null; if (!_annotationCfg.isNull()) { if (idClassAnn != null) idClass = idClassAnn.value(); else { String s = idClassConfig.getClassName(); idClass = _persistenceUnit.loadTempClass(s); } // XXX: temp. introspects idClass as an embeddable type. _persistenceUnit.addEntityClass(idClass.getName(), idClass); // jpa/0i49 vs jpa/0i40 // embeddable.setFieldAccess(isField); } if (entityType.getId() != null) { } else if (entityType.isFieldAccess()) introspectIdField( _persistenceUnit, entityType, parentType, type, idClass, mappedSuperOrEntityConfig); else { introspectIdMethod( _persistenceUnit, entityType, parentType, type, idClass, mappedSuperOrEntityConfig); } HashMap<String, IdConfig> idMap = null; AttributesConfig attributes = null; if (mappedSuperOrEntityConfig != null) { attributes = mappedSuperOrEntityConfig.getAttributes(); if (attributes != null) idMap = attributes.getIdMap(); } // if ((idMap == null) || (idMap.size() == 0)) { // idMap = entityType.getSuperClass(); // } if (entityType.isEntity() && (entityType.getId() == null) && ((idMap == null) || (idMap.size() == 0))) throw new ConfigException( L.l( "{0} does not have any primary keys. Entities must have at least one @Id or exactly one @EmbeddedId field.", entityType.getName())); // Introspect overridden attributes. (jpa/0ge2) introspectAttributeOverrides(entityType, type); introspectSecondaryTable(entityType, type); if (entityType.isFieldAccess()) introspectFields( _persistenceUnit, entityType, parentType, type, mappedSuperOrEntityConfig, false); else introspectMethods( _persistenceUnit, entityType, parentType, type, mappedSuperOrEntityConfig); introspectCallbacks(type, entityType); // Adds entity listeners, if any. introspectEntityListeners(type, entityType, _persistenceUnit); // Adds sql result set mappings, if any. introspectSqlResultSetMappings(type, entityType, entityType.getName()); // Adds named queries, if any. introspectNamedQueries(type, entityType.getName()); introspectNamedNativeQueries(type, entityType.getName()); } catch (ConfigException e) { if (beanType != null) beanType.setConfigException(e); throw e; } catch (SQLException e) { if (beanType != null) beanType.setConfigException(e); throw e; } catch (RuntimeException e) { if (beanType != null) beanType.setConfigException(e); throw e; } return beanType; }
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)); }
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()); } }