Search in sources :

Example 36 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class PojoEntityTuplizer method buildProxyFactory.

@Override
protected ProxyFactory buildProxyFactory(PersistentClass persistentClass, Getter idGetter, Setter idSetter) {
    // determine the id getter and setter methods from the proxy interface (if any)
    // determine all interfaces needed by the resulting proxy
    /*
		 * We need to preserve the order of the interfaces they were put into the set, since javassist will choose the
		 * first one's class-loader to construct the proxy class with. This is also the reason why HibernateProxy.class
		 * should be the last one in the order (on JBossAS7 its class-loader will be org.hibernate module's class-
		 * loader, which will not see the classes inside deployed apps.  See HHH-3078
		 */
    Set<Class> proxyInterfaces = new java.util.LinkedHashSet<Class>();
    Class mappedClass = persistentClass.getMappedClass();
    Class proxyInterface = persistentClass.getProxyInterface();
    if (proxyInterface != null && !mappedClass.equals(proxyInterface)) {
        if (!proxyInterface.isInterface()) {
            throw new MappingException("proxy must be either an interface, or the class itself: " + getEntityName());
        }
        proxyInterfaces.add(proxyInterface);
    }
    if (mappedClass.isInterface()) {
        proxyInterfaces.add(mappedClass);
    }
    Iterator<Subclass> subclasses = persistentClass.getSubclassIterator();
    while (subclasses.hasNext()) {
        final Subclass subclass = subclasses.next();
        final Class subclassProxy = subclass.getProxyInterface();
        final Class subclassClass = subclass.getMappedClass();
        if (subclassProxy != null && !subclassClass.equals(subclassProxy)) {
            if (!subclassProxy.isInterface()) {
                throw new MappingException("proxy must be either an interface, or the class itself: " + subclass.getEntityName());
            }
            proxyInterfaces.add(subclassProxy);
        }
    }
    proxyInterfaces.add(HibernateProxy.class);
    Iterator properties = persistentClass.getPropertyIterator();
    Class clazz = persistentClass.getMappedClass();
    while (properties.hasNext()) {
        Property property = (Property) properties.next();
        Method method = property.getGetter(clazz).getMethod();
        if (method != null && Modifier.isFinal(method.getModifiers())) {
            LOG.gettersOfLazyClassesCannotBeFinal(persistentClass.getEntityName(), property.getName());
        }
        method = property.getSetter(clazz).getMethod();
        if (method != null && Modifier.isFinal(method.getModifiers())) {
            LOG.settersOfLazyClassesCannotBeFinal(persistentClass.getEntityName(), property.getName());
        }
    }
    Method idGetterMethod = idGetter == null ? null : idGetter.getMethod();
    Method idSetterMethod = idSetter == null ? null : idSetter.getMethod();
    Method proxyGetIdentifierMethod = idGetterMethod == null || proxyInterface == null ? null : ReflectHelper.getMethod(proxyInterface, idGetterMethod);
    Method proxySetIdentifierMethod = idSetterMethod == null || proxyInterface == null ? null : ReflectHelper.getMethod(proxyInterface, idSetterMethod);
    ProxyFactory pf = buildProxyFactoryInternal(persistentClass, idGetter, idSetter);
    try {
        pf.postInstantiate(getEntityName(), mappedClass, proxyInterfaces, proxyGetIdentifierMethod, proxySetIdentifierMethod, persistentClass.hasEmbeddedIdentifier() ? (CompositeType) persistentClass.getIdentifier().getType() : null);
    } catch (HibernateException he) {
        LOG.unableToCreateProxyFactory(getEntityName(), he);
        pf = null;
    }
    return pf;
}
Also used : Subclass(org.hibernate.mapping.Subclass) ProxyFactory(org.hibernate.proxy.ProxyFactory) HibernateException(org.hibernate.HibernateException) Method(java.lang.reflect.Method) MappingException(org.hibernate.MappingException) Iterator(java.util.Iterator) PersistentClass(org.hibernate.mapping.PersistentClass) Property(org.hibernate.mapping.Property) CompositeType(org.hibernate.type.CompositeType)

Example 37 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class TypeFactory method type.

public Type type(Class<Type> typeClass, Properties parameters) {
    try {
        Type type = typeClass.newInstance();
        injectParameters(type, parameters);
        return type;
    } catch (Exception e) {
        throw new MappingException("Could not instantiate Type: " + typeClass.getName(), e);
    }
}
Also used : ParameterizedType(org.hibernate.usertype.ParameterizedType) CompositeUserType(org.hibernate.usertype.CompositeUserType) UserType(org.hibernate.usertype.UserType) MappingException(org.hibernate.MappingException) HibernateException(org.hibernate.HibernateException) MappingException(org.hibernate.MappingException)

Example 38 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class TypeFactory method customCollection.

public CollectionType customCollection(String typeName, Properties typeParameters, String role, String propertyRef) {
    Class typeClass;
    try {
        typeClass = ReflectHelper.classForName(typeName);
    } catch (ClassNotFoundException cnfe) {
        throw new MappingException("user collection type class not found: " + typeName, cnfe);
    }
    CustomCollectionType result = new CustomCollectionType(typeScope, typeClass, role, propertyRef);
    if (typeParameters != null) {
        injectParameters(result.getUserType(), typeParameters);
    }
    return result;
}
Also used : MappingException(org.hibernate.MappingException)

Example 39 with MappingException

use of org.hibernate.MappingException in project querydsl by querydsl.

the class JPADomainExporter method handleProperty.

private void handleProperty(EntityType entityType, Class<?> cl, Attribute<?, ?> p) throws NoSuchMethodException, ClassNotFoundException {
    Class<?> clazz = Object.class;
    try {
        clazz = p.getJavaType();
    } catch (MappingException e) {
    // ignore
    }
    Type propertyType = getType(cl, clazz, p.getName());
    AnnotatedElement annotated = getAnnotatedElement(cl, p.getName());
    propertyType = getTypeOverride(propertyType, annotated);
    if (propertyType == null) {
        return;
    }
    if (p.isCollection()) {
        if (p instanceof MapAttribute) {
            MapAttribute<?, ?, ?> map = (MapAttribute<?, ?, ?>) p;
            Type keyType = typeFactory.get(map.getKeyJavaType());
            Type valueType = typeFactory.get(map.getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), keyType), normalize(propertyType.getParameters().get(1), valueType));
        } else {
            Type valueType = typeFactory.get(((PluralAttribute<?, ?, ?>) p).getElementType().getJavaType());
            valueType = getPropertyType(p, valueType);
            propertyType = new SimpleType(propertyType, normalize(propertyType.getParameters().get(0), valueType));
        }
    } else {
        propertyType = getPropertyType(p, propertyType);
    }
    Property property = createProperty(entityType, p.getName(), propertyType, annotated);
    entityType.addProperty(property);
}
Also used : SimpleType(com.mysema.codegen.model.SimpleType) Type(com.mysema.codegen.model.Type) EntityType(com.querydsl.codegen.EntityType) SimpleType(com.mysema.codegen.model.SimpleType) AnnotatedElement(java.lang.reflect.AnnotatedElement) Property(com.querydsl.codegen.Property) MappingException(org.hibernate.MappingException)

Example 40 with MappingException

use of org.hibernate.MappingException in project hibernate-orm by hibernate.

the class AuditedDynamicComponentTest method testAuditedDynamicComponentFailure.

//@Test
public void testAuditedDynamicComponentFailure() throws URISyntaxException {
    final Configuration config = new Configuration();
    final URL hbm = Thread.currentThread().getContextClassLoader().getResource("mappings/dynamicComponents/mapAudited.hbm.xml");
    config.addFile(new File(hbm.toURI()));
    final String auditStrategy = getAuditStrategy();
    if (!StringTools.isEmpty(auditStrategy)) {
        config.setProperty(EnversSettings.AUDIT_STRATEGY, auditStrategy);
    }
    final ServiceRegistry serviceRegistry = ServiceRegistryBuilder.buildServiceRegistry(config.getProperties());
    try {
        config.buildSessionFactory(serviceRegistry);
        Assert.fail("MappingException expected");
    } catch (MappingException e) {
        Assert.assertEquals("Audited dynamic-component properties are not supported. Consider applying @NotAudited annotation to " + AuditedDynamicComponentEntity.class.getName() + "#customFields.", e.getMessage());
    } finally {
        ServiceRegistryBuilder.destroy(serviceRegistry);
    }
}
Also used : Configuration(org.hibernate.cfg.Configuration) ServiceRegistry(org.hibernate.service.ServiceRegistry) File(java.io.File) URL(java.net.URL) MappingException(org.hibernate.MappingException)

Aggregations

MappingException (org.hibernate.MappingException)94 PersistentClass (org.hibernate.mapping.PersistentClass)17 HibernateException (org.hibernate.HibernateException)12 Iterator (java.util.Iterator)11 Test (org.junit.Test)11 AnnotationException (org.hibernate.AnnotationException)10 QueryException (org.hibernate.QueryException)10 Type (org.hibernate.type.Type)10 Property (org.hibernate.mapping.Property)9 HashMap (java.util.HashMap)8 XClass (org.hibernate.annotations.common.reflection.XClass)8 DuplicateMappingException (org.hibernate.DuplicateMappingException)6 Configuration (org.hibernate.cfg.Configuration)6 UnknownSqlResultSetMappingException (org.hibernate.procedure.UnknownSqlResultSetMappingException)6 ServiceRegistry (org.hibernate.service.ServiceRegistry)6 Map (java.util.Map)5 AssociationType (org.hibernate.type.AssociationType)5 HashSet (java.util.HashSet)4 ClassLoadingException (org.hibernate.annotations.common.reflection.ClassLoadingException)4 MetadataSources (org.hibernate.boot.MetadataSources)4