/**
   * Generates the attribute representation of the identifier for a given entity mapping.
   *
   * @param mappedEntity The mapping definition of the entity.
   * @param generator The identifier value generator to use for this identifier.
   * @return The appropriate IdentifierProperty definition.
   */
  public static IdentifierProperty buildIdentifierAttribute(
      PersistentClass mappedEntity, IdentifierGenerator generator) {
    String mappedUnsavedValue = mappedEntity.getIdentifier().getNullValue();
    Type type = mappedEntity.getIdentifier().getType();
    Property property = mappedEntity.getIdentifierProperty();

    IdentifierValue unsavedValue =
        UnsavedValueFactory.getUnsavedIdentifierValue(
            mappedUnsavedValue, getGetter(property), type, getConstructor(mappedEntity));

    if (property == null) {
      // this is a virtual id property...
      return new IdentifierProperty(
          type,
          mappedEntity.hasEmbeddedIdentifier(),
          mappedEntity.hasIdentifierMapper(),
          unsavedValue,
          generator);
    } else {
      return new IdentifierProperty(
          property.getName(),
          property.getNodeName(),
          type,
          mappedEntity.hasEmbeddedIdentifier(),
          unsavedValue,
          generator);
    }
  }
 protected void applyCacheSettings(Configuration configuration) {
   if (getCacheConcurrencyStrategy() != null) {
     Iterator itr = configuration.getClassMappings();
     while (itr.hasNext()) {
       PersistentClass clazz = (PersistentClass) itr.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() && overrideCacheStrategy()) {
         configuration.setCacheConcurrencyStrategy(
             clazz.getEntityName(), getCacheConcurrencyStrategy());
       }
     }
     itr = configuration.getCollectionMappings();
     while (itr.hasNext()) {
       Collection coll = (Collection) itr.next();
       configuration.setCollectionCacheConcurrencyStrategy(
           coll.getRole(), getCacheConcurrencyStrategy());
     }
   }
 }
 /**
  * return accessors
  *
  * @param mappedProperty
  * @return
  */
 private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
   if (mappedProperty.isBackRef()) {
     PropertyAccessor ac = mappedProperty.getPropertyAccessor(null);
     if (ac != null) return ac;
   }
   return accessor;
 }
  public static GroovyAwareJavassistProxyFactory buildProxyFactory(
      PersistentClass persistentClass) {
    GroovyAwareJavassistProxyFactory proxyFactory = new GroovyAwareJavassistProxyFactory();

    @SuppressWarnings("unchecked")
    Set<Class<HibernateProxy>> proxyInterfaces = new HashSet<Class<HibernateProxy>>();
    proxyInterfaces.add(HibernateProxy.class);

    final Class<?> javaClass = persistentClass.getMappedClass();
    final Property identifierProperty = persistentClass.getIdentifierProperty();
    final Method idGetterMethod =
        identifierProperty != null ? identifierProperty.getGetter(javaClass).getMethod() : null;
    final Method idSetterMethod =
        identifierProperty != null ? identifierProperty.getSetter(javaClass).getMethod() : null;
    final Type identifierType =
        persistentClass.hasEmbeddedIdentifier() ? persistentClass.getIdentifier().getType() : null;

    try {
      proxyFactory.postInstantiate(
          persistentClass.getEntityName(),
          javaClass,
          proxyInterfaces,
          idGetterMethod,
          idSetterMethod,
          identifierType instanceof CompositeType ? (CompositeType) identifierType : null);
    } catch (HibernateException e) {
      LOG.warn("Cannot instantiate proxy factory: " + e.getMessage());
      return null;
    }

    return proxyFactory;
  }
예제 #5
0
  public void createPropertyRefConstraints(Map persistentClasses) {
    if (referencedPropertyName != null) {
      PersistentClass pc = (PersistentClass) persistentClasses.get(getReferencedEntityName());

      Property property = pc.getReferencedProperty(getReferencedPropertyName());

      if (property == null) {
        throw new MappingException(
            "Could not find property "
                + getReferencedPropertyName()
                + " on "
                + getReferencedEntityName());
      } else {
        if (!hasFormula() && !"none".equals(getForeignKeyName())) {
          java.util.List refColumns = new ArrayList();
          Iterator iter = property.getColumnIterator();
          while (iter.hasNext()) {
            Column col = (Column) iter.next();
            refColumns.add(col);
          }

          ForeignKey fk =
              getTable()
                  .createForeignKey(
                      getForeignKeyName(),
                      getConstraintColumns(),
                      ((EntityType) getType()).getAssociatedEntityName(),
                      refColumns);
          fk.setCascadeDeleteEnabled(isCascadeDeleteEnabled());
        }
      }
    }
  }
 private void addPersistentProperty(Property property) {
   if ("field".equals(property.getPropertyAccessorName())) {
     fieldAccessedPersistentProperties.add(property.getName());
   } else {
     propertyAccessedPersistentProperties.add(property.getName());
   }
 }
  public void testUpdateableHibernateMapping() throws Exception {
    GroovyClassLoader cl = new GroovyClassLoader();
    GrailsDomainClass domainClass =
        new DefaultGrailsDomainClass(
            cl.parseClass(
                "class TestInsertableDomain {\n"
                    + "    Long id \n"
                    + "    Long version \n"
                    + "    String testString1 \n"
                    + "    String testString2 \n"
                    + "\n"
                    + "    static mapping = {\n"
                    + "       testString1 updateable:false \n"
                    + "       testString2 updateable:true \n"
                    + "    }\n"
                    + "}"));

    DefaultGrailsDomainConfiguration config =
        getDomainConfig(cl, new Class[] {domainClass.getClazz()});

    PersistentClass persistentClass = config.getClassMapping("TestInsertableDomain");

    Property property1 = persistentClass.getProperty("testString1");
    assertEquals(false, property1.isUpdateable());

    Property property2 = persistentClass.getProperty("testString2");
    assertEquals(true, property2.isUpdateable());
  }
 private PropertyAccessor buildPropertyAccessor(Property mappedProperty) {
   if (mappedProperty.isBackRef()) {
     return mappedProperty.getPropertyAccessor(null);
   } else {
     return PropertyAccessorFactory.getDynamicMapPropertyAccessor();
   }
 }
 private PropertyData getIdPropertyData(Property property) {
   return new PropertyData(
       property.getName(),
       property.getName(),
       property.getPropertyAccessorName(),
       ModificationStore.FULL);
 }
예제 #10
0
  private void buildSessionFactory(String[] files) throws Exception {

    if (getSessions() != null) getSessions().close();

    try {

      setCfg(new Configuration());

      cfg.addProperties(getExtraProperties());

      if (recreateSchema()) {
        cfg.setProperty(Environment.HBM2DDL_AUTO, "create-drop");
      }

      for (int i = 0; i < files.length; i++) {
        if (!files[i].startsWith("net/")) files[i] = getBaseForMappings() + files[i];
        getCfg().addResource(files[i], TestCase.class.getClassLoader());
      }

      setDialect(Dialect.getDialect());

      configure(cfg);

      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() && overrideCacheStrategy()) {
            cfg.setCacheConcurrencyStrategy(clazz.getEntityName(), getCacheConcurrencyStrategy());
          }
        }

        iter = cfg.getCollectionMappings();
        while (iter.hasNext()) {
          Collection coll = (Collection) iter.next();
          cfg.setCollectionCacheConcurrencyStrategy(coll.getRole(), getCacheConcurrencyStrategy());
        }
      }

      setSessions(getCfg().buildSessionFactory(/*new TestInterceptor()*/ ));

      afterSessionFactoryBuilt();
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    }
  }
 @SuppressWarnings("unchecked")
 private void createPropertiesGroupMapping(Property property) {
   final Component component = (Component) property.getValue();
   final Iterator<Property> componentProperties = component.getPropertyIterator();
   while (componentProperties.hasNext()) {
     final Property componentProperty = componentProperties.next();
     propertiesGroupMapping.put(componentProperty.getName(), component.getNodeName());
   }
 }
  private static Getter getGetter(Property mappingProperty) {
    if (mappingProperty == null || !mappingProperty.getPersistentClass().hasPojoRepresentation()) {
      return null;
    }

    PropertyAccessor pa =
        PropertyAccessorFactory.getPropertyAccessor(mappingProperty, EntityMode.POJO);
    return pa.getGetter(
        mappingProperty.getPersistentClass().getMappedClass(), mappingProperty.getName());
  }
예제 #13
0
 private PropertyAuditingData getIdPersistentPropertyAuditingData(Property property) {
   return new PropertyAuditingData(
       property.getName(),
       property.getPropertyAccessorName(),
       ModificationStore.FULL,
       RelationTargetAuditMode.AUDITED,
       null,
       null,
       false);
 }
예제 #14
0
 private ValueInclusion determineInsertValueGenerationType(
     Property mappingProperty, StandardProperty runtimeProperty) {
   if (runtimeProperty.isInsertGenerated()) {
     return ValueInclusion.FULL;
   } else if (mappingProperty.getValue() instanceof Component) {
     if (hasPartialInsertComponentGeneration((Component) mappingProperty.getValue())) {
       return ValueInclusion.PARTIAL;
     }
   }
   return ValueInclusion.NONE;
 }
 private ValueInclusion determineUpdateValueGenerationType(
     Property mappingProperty, NonIdentifierAttribute runtimeProperty) {
   if (isUpdateGenerated(runtimeProperty)) {
     return ValueInclusion.FULL;
   } else if (mappingProperty.getValue() instanceof Component) {
     if (hasPartialUpdateComponentGeneration((Component) mappingProperty.getValue())) {
       return ValueInclusion.PARTIAL;
     }
   }
   return ValueInclusion.NONE;
 }
예제 #16
0
 private Property getConstrainedOneToOne(RootClass rc) {
   Iterator propertyClosureIterator = rc.getPropertyClosureIterator();
   while (propertyClosureIterator.hasNext()) {
     Property property = (Property) propertyClosureIterator.next();
     if (property.getValue() instanceof OneToOne) {
       OneToOne oto = (OneToOne) property.getValue();
       if (oto.isConstrained()) {
         return property;
       }
     }
   }
   return null;
 }
  @SuppressWarnings({"unchecked"})
  private String searchMappedBy(PersistentClass referencedClass, Collection collectionValue) {
    Iterator<Property> assocClassProps = referencedClass.getPropertyIterator();
    while (assocClassProps.hasNext()) {
      Property property = assocClassProps.next();

      if (Tools.iteratorsContentEqual(
          property.getValue().getColumnIterator(), collectionValue.getKey().getColumnIterator())) {
        return property.getName();
      }
    }
    return null;
  }
예제 #18
0
  private Property bindMeta(Property property, TableIdentifier identifier) {
    Iterator columnIterator = property.getValue().getColumnIterator();
    while (columnIterator.hasNext()) {
      Column col = (Column) columnIterator.next();

      Map map = revengStrategy.columnToMetaAttributes(identifier, col.getName());
      if (map != null) { // TODO: merge from each column ?
        property.setMetaAttributes(map);
      }
    }

    return property;
  }
 private boolean hasPartialUpdateComponentGeneration(Component component) {
   Iterator subProperties = component.getPropertyIterator();
   while (subProperties.hasNext()) {
     Property prop = (Property) subProperties.next();
     if (isUpdateGenerated(prop)) {
       return true;
     } else if (prop.getValue() instanceof Component) {
       if (hasPartialUpdateComponentGeneration((Component) prop.getValue())) {
         return true;
       }
     }
   }
   return false;
 }
 private void readPersistentPropertiesAccess() {
   final Iterator<Property> propertyIter = persistentPropertiesSource.getPropertyIterator();
   while (propertyIter.hasNext()) {
     final Property property = propertyIter.next();
     addPersistentProperty(property);
     if ("embedded".equals(property.getPropertyAccessorName())
         && property.getName().equals(property.getNodeName())) {
       // If property name equals node name and embedded accessor type is used, processing
       // component
       // has been defined with <properties> tag. See HHH-6636 JIRA issue.
       createPropertiesGroupMapping(property);
     }
   }
 }
예제 #21
0
 private boolean hasPartialInsertComponentGeneration(Component component) {
   Iterator subProperties = component.getPropertyIterator();
   while (subProperties.hasNext()) {
     Property prop = (Property) subProperties.next();
     if (prop.getGeneration() == PropertyGeneration.ALWAYS
         || prop.getGeneration() == PropertyGeneration.INSERT) {
       return true;
     } else if (prop.getValue() instanceof Component) {
       if (hasPartialInsertComponentGeneration((Component) prop.getValue())) {
         return true;
       }
     }
   }
   return false;
 }
예제 #22
0
 /** @param property */
 private void markAsUseInEquals(Property property) {
   Map m = new HashMap();
   MetaAttribute ma = new MetaAttribute("use-in-equals");
   ma.addValue("true");
   m.put(ma.getName(), ma);
   property.setMetaAttributes(m);
 }
예제 #23
0
  private void updatePrimaryKey(RootClass rc, PrimaryKeyInfo pki) {
    SimpleValue idValue = (SimpleValue) rc.getIdentifierProperty().getValue();

    Properties defaultStrategyProperties = new Properties();
    Property constrainedOneToOne = getConstrainedOneToOne(rc);
    if (constrainedOneToOne != null) {
      if (pki.suggestedStrategy == null) {
        idValue.setIdentifierGeneratorStrategy("foreign");
      }

      if (pki.suggestedProperties == null) {
        defaultStrategyProperties.setProperty("property", constrainedOneToOne.getName());
        idValue.setIdentifierGeneratorProperties(defaultStrategyProperties);
      }
    }
  }
 @SuppressWarnings({"unchecked"})
 private String searchMappedBy(PersistentClass referencedClass, Table collectionTable) {
   Iterator<Property> properties = referencedClass.getPropertyIterator();
   while (properties.hasNext()) {
     Property property = properties.next();
     if (property.getValue() instanceof Collection) {
       // The equality is intentional. We want to find a collection property with the same
       // collection table.
       //noinspection ObjectEquality
       if (((Collection) property.getValue()).getCollectionTable() == collectionTable) {
         return property.getName();
       }
     }
   }
   return null;
 }
예제 #25
0
 // TODO defaultModifiers
 private String getModifiers(Property property, String modifiername, String defaultModifiers) {
   MetaAttribute override = property.getMetaAttribute(modifiername);
   if (override != null) {
     return MetaAttributeHelper.getMetaAsString(override);
   } else {
     return defaultModifiers;
   }
 }
예제 #26
0
 public boolean isComponent(Property property) {
   Value value = property.getValue();
   if (value != null && value instanceof Component) {
     return true;
   } else {
     return false;
   }
 }
예제 #27
0
 public String getFieldDescription(Property property) {
   MetaAttribute c = property.getMetaAttribute("field-description");
   if (c == null) {
     return "";
   } else {
     return c2j.getMetaAsString(property, "field-description");
   }
 }
예제 #28
0
 public String getFieldJavaDoc(Property property, int indent) {
   MetaAttribute c = property.getMetaAttribute("field-description");
   if (c == null) {
     return c2j.toJavaDoc("", indent);
   } else {
     return c2j.toJavaDoc(c2j.getMetaAsString(property, "field-description"), indent);
   }
 }
예제 #29
0
  public String generateBasicAnnotation(Property property) {
    StringBuffer annotations = new StringBuffer("    ");
    if (property.getValue() instanceof SimpleValue) {
      if (hasVersionProperty())
        if (property.equals(getVersionProperty())) buildVersionAnnotation(annotations);
      String typeName = ((SimpleValue) property.getValue()).getTypeName();
      if ("date".equals(typeName) || "java.sql.Date".equals(typeName)) {
        buildTemporalAnnotation(annotations, "DATE");
      } else if ("timestamp".equals(typeName) || "java.sql.Timestamp".equals(typeName)) {
        buildTemporalAnnotation(annotations, "TIMESTAMP");
      } else if ("time".equals(typeName) || "java.sql.Time".equals(typeName)) {
        buildTemporalAnnotation(annotations, "TIME");
      } // TODO: calendar etc. ?
    }

    return annotations.toString();
  }
  //	public ComponentMetamodel(Component component, SessionFactoryImplementor sessionFactory) {
  public ComponentMetamodel(Component component) {
    //		this.sessionFactory = sessionFactory;
    this.role = component.getRoleName();
    this.isKey = component.isKey();
    propertySpan = component.getPropertySpan();
    properties = new StandardProperty[propertySpan];
    Iterator itr = component.getPropertyIterator();
    int i = 0;
    while (itr.hasNext()) {
      Property property = (Property) itr.next();
      properties[i] = PropertyFactory.buildStandardProperty(property, false);
      propertyIndexes.put(property.getName(), new Integer(i));
      i++;
    }

    tuplizerMapping = new ComponentEntityModeToTuplizerMapping(component);
  }