Search in sources :

Example 56 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class NonReflectiveBinderTest method testMetaInheritance.

@Test
public void testMetaInheritance() {
    PersistentClass cm = metadata.getEntityBinding("org.hibernate.test.legacy.Wicked");
    Map m = cm.getMetaAttributes();
    assertNotNull(m);
    assertNotNull(cm.getMetaAttribute("global"));
    assertNull(cm.getMetaAttribute("globalnoinherit"));
    MetaAttribute metaAttribute = cm.getMetaAttribute("implements");
    assertNotNull(metaAttribute);
    assertEquals("implements", metaAttribute.getName());
    assertTrue(metaAttribute.isMultiValued());
    assertEquals(3, metaAttribute.getValues().size());
    assertEquals("java.lang.Observer", metaAttribute.getValues().get(0));
    assertEquals("java.lang.Observer", metaAttribute.getValues().get(1));
    assertEquals("org.foo.BogusVisitor", metaAttribute.getValues().get(2));
    /*Property property = cm.getIdentifierProperty();
		property.getMetaAttribute(null);*/
    Iterator propertyIterator = cm.getPropertyIterator();
    while (propertyIterator.hasNext()) {
        Property element = (Property) propertyIterator.next();
        System.out.println(element);
        Map ma = element.getMetaAttributes();
        assertNotNull(ma);
        assertNotNull(element.getMetaAttribute("global"));
        MetaAttribute metaAttribute2 = element.getMetaAttribute("implements");
        assertNotNull(metaAttribute2);
        assertNull(element.getMetaAttribute("globalnoinherit"));
    }
    Property element = cm.getProperty("component");
    Map ma = element.getMetaAttributes();
    assertNotNull(ma);
    assertNotNull(element.getMetaAttribute("global"));
    assertNotNull(element.getMetaAttribute("componentonly"));
    assertNotNull(element.getMetaAttribute("allcomponent"));
    assertNull(element.getMetaAttribute("globalnoinherit"));
    MetaAttribute compimplements = element.getMetaAttribute("implements");
    assertNotNull(compimplements);
    assertEquals(compimplements.getValue(), "AnotherInterface");
    Property xp = ((Component) element.getValue()).getProperty("x");
    MetaAttribute propximplements = xp.getMetaAttribute("implements");
    assertNotNull(propximplements);
    assertEquals(propximplements.getValue(), "AnotherInterface");
}
Also used : Iterator(java.util.Iterator) MetaAttribute(org.hibernate.mapping.MetaAttribute) Component(org.hibernate.mapping.Component) Map(java.util.Map) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 57 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class NonReflectiveBinderTest method testComparator.

@Test
public void testComparator() {
    PersistentClass cm = metadata.getEntityBinding("org.hibernate.test.legacy.Wicked");
    Property property = cm.getProperty("sortedEmployee");
    Collection col = (Collection) property.getValue();
    assertEquals(col.getComparatorClassName(), "org.hibernate.test.legacy.NonExistingComparator");
}
Also used : Collection(org.hibernate.mapping.Collection) Property(org.hibernate.mapping.Property) PersistentClass(org.hibernate.mapping.PersistentClass) Test(org.junit.Test)

Example 58 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class AnnotationBinder method mapAsIdClass.

private static boolean mapAsIdClass(Map<XClass, InheritanceState> inheritanceStatePerClass, InheritanceState inheritanceState, PersistentClass persistentClass, EntityBinder entityBinder, PropertyHolder propertyHolder, InheritanceState.ElementsToProcess elementsToProcess, Set<String> idPropertiesIfIdClass, MetadataBuildingContext context) {
    /*
		 * We are looking for @IdClass
		 * In general we map the id class as identifier using the mapping metadata of the main entity's properties
		 * and we create an identifier mapper containing the id properties of the main entity
		 *
		 * In JPA 2, there is a shortcut if the id class is the Pk of the associated class pointed to by the id
		 * it ought to be treated as an embedded and not a real IdClass (at least in the Hibernate's internal way
		 */
    XClass classWithIdClass = inheritanceState.getClassWithIdClass(false);
    if (classWithIdClass != null) {
        IdClass idClass = classWithIdClass.getAnnotation(IdClass.class);
        XClass compositeClass = context.getBuildingOptions().getReflectionManager().toXClass(idClass.value());
        PropertyData inferredData = new PropertyPreloadedData(entityBinder.getPropertyAccessType(), "id", compositeClass);
        PropertyData baseInferredData = new PropertyPreloadedData(entityBinder.getPropertyAccessType(), "id", classWithIdClass);
        AccessType propertyAccessor = entityBinder.getPropertyAccessor(compositeClass);
        //In JPA 2, there is a shortcut if the IdClass is the Pk of the associated class pointed to by the id
        //it ought to be treated as an embedded and not a real IdClass (at least in the Hibernate's internal way
        final boolean isFakeIdClass = isIdClassPkOfTheAssociatedEntity(elementsToProcess, compositeClass, inferredData, baseInferredData, propertyAccessor, inheritanceStatePerClass, context);
        if (isFakeIdClass) {
            return false;
        }
        boolean isComponent = true;
        String generatorType = "assigned";
        String generator = BinderHelper.ANNOTATION_STRING_DEFAULT;
        boolean ignoreIdAnnotations = entityBinder.isIgnoreIdAnnotations();
        entityBinder.setIgnoreIdAnnotations(true);
        propertyHolder.setInIdClass(true);
        bindIdClass(generatorType, generator, inferredData, baseInferredData, null, propertyHolder, isComponent, propertyAccessor, entityBinder, true, false, context, inheritanceStatePerClass);
        propertyHolder.setInIdClass(null);
        inferredData = new PropertyPreloadedData(propertyAccessor, PropertyPath.IDENTIFIER_MAPPER_PROPERTY, compositeClass);
        Component mapper = fillComponent(propertyHolder, inferredData, baseInferredData, propertyAccessor, false, entityBinder, true, true, false, context, inheritanceStatePerClass);
        entityBinder.setIgnoreIdAnnotations(ignoreIdAnnotations);
        persistentClass.setIdentifierMapper(mapper);
        //If id definition is on a mapped superclass, update the mapping
        final org.hibernate.mapping.MappedSuperclass superclass = BinderHelper.getMappedSuperclassOrNull(classWithIdClass, inheritanceStatePerClass, context);
        if (superclass != null) {
            superclass.setDeclaredIdentifierMapper(mapper);
        } else {
            //we are for sure on the entity
            persistentClass.setDeclaredIdentifierMapper(mapper);
        }
        Property property = new Property();
        property.setName(PropertyPath.IDENTIFIER_MAPPER_PROPERTY);
        property.setUpdateable(false);
        property.setInsertable(false);
        property.setValue(mapper);
        property.setPropertyAccessorName("embedded");
        persistentClass.addProperty(property);
        entityBinder.setIgnoreIdAnnotations(true);
        Iterator properties = mapper.getPropertyIterator();
        while (properties.hasNext()) {
            idPropertiesIfIdClass.add(((Property) properties.next()).getName());
        }
        return true;
    } else {
        return false;
    }
}
Also used : XClass(org.hibernate.annotations.common.reflection.XClass) IdClass(javax.persistence.IdClass) Iterator(java.util.Iterator) Component(org.hibernate.mapping.Component) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 59 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class AnnotationBinder method bindAny.

private static void bindAny(String cascadeStrategy, Ejb3JoinColumn[] columns, boolean cascadeOnDelete, Nullability nullability, PropertyHolder propertyHolder, PropertyData inferredData, EntityBinder entityBinder, boolean isIdentifierMapper, MetadataBuildingContext buildingContext) {
    org.hibernate.annotations.Any anyAnn = inferredData.getProperty().getAnnotation(org.hibernate.annotations.Any.class);
    if (anyAnn == null) {
        throw new AssertionFailure("Missing @Any annotation: " + BinderHelper.getPath(propertyHolder, inferredData));
    }
    Any value = BinderHelper.buildAnyValue(anyAnn.metaDef(), columns, anyAnn.metaColumn(), inferredData, cascadeOnDelete, nullability, propertyHolder, entityBinder, anyAnn.optional(), buildingContext);
    PropertyBinder binder = new PropertyBinder();
    binder.setName(inferredData.getPropertyName());
    binder.setValue(value);
    binder.setLazy(anyAnn.fetch() == FetchType.LAZY);
    //binder.setCascade(cascadeStrategy);
    if (isIdentifierMapper) {
        binder.setInsertable(false);
        binder.setUpdatable(false);
    } else {
        binder.setInsertable(columns[0].isInsertable());
        binder.setUpdatable(columns[0].isUpdatable());
    }
    binder.setAccessType(inferredData.getDefaultAccess());
    binder.setCascade(cascadeStrategy);
    Property prop = binder.makeProperty();
    //composite FK columns are in the same table so its OK
    propertyHolder.addProperty(prop, columns, inferredData.getDeclaringClass());
}
Also used : AssertionFailure(org.hibernate.AssertionFailure) PropertyBinder(org.hibernate.cfg.annotations.PropertyBinder) Any(org.hibernate.mapping.Any) ManyToAny(org.hibernate.annotations.ManyToAny) Property(org.hibernate.mapping.Property) XProperty(org.hibernate.annotations.common.reflection.XProperty)

Example 60 with Property

use of org.hibernate.mapping.Property in project hibernate-orm by hibernate.

the class BinderHelper method shallowCopy.

/**
	 * create a property copy reusing the same value
	 */
public static Property shallowCopy(Property property) {
    Property clone = new Property();
    clone.setCascade(property.getCascade());
    clone.setInsertable(property.isInsertable());
    clone.setLazy(property.isLazy());
    clone.setName(property.getName());
    clone.setNaturalIdentifier(property.isNaturalIdentifier());
    clone.setOptimisticLocked(property.isOptimisticLocked());
    clone.setOptional(property.isOptional());
    clone.setPersistentClass(property.getPersistentClass());
    clone.setPropertyAccessorName(property.getPropertyAccessorName());
    clone.setSelectable(property.isSelectable());
    clone.setUpdateable(property.isUpdateable());
    clone.setValue(property.getValue());
    return clone;
}
Also used : Property(org.hibernate.mapping.Property) SyntheticProperty(org.hibernate.mapping.SyntheticProperty)

Aggregations

Property (org.hibernate.mapping.Property)94 PersistentClass (org.hibernate.mapping.PersistentClass)53 Component (org.hibernate.mapping.Component)30 Test (org.junit.Test)29 SimpleValue (org.hibernate.mapping.SimpleValue)24 Iterator (java.util.Iterator)23 SyntheticProperty (org.hibernate.mapping.SyntheticProperty)18 MetadataSources (org.hibernate.boot.MetadataSources)17 Column (org.hibernate.mapping.Column)17 AnnotationException (org.hibernate.AnnotationException)14 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)14 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)14 XProperty (org.hibernate.annotations.common.reflection.XProperty)12 Metadata (org.hibernate.boot.Metadata)11 Collection (org.hibernate.mapping.Collection)11 HashMap (java.util.HashMap)10 AssertionFailure (org.hibernate.AssertionFailure)10 MappingException (org.hibernate.MappingException)9 TestForIssue (org.hibernate.testing.TestForIssue)9 Map (java.util.Map)7