Example usage for org.hibernate.mapping Property getSetter

List of usage examples for org.hibernate.mapping Property getSetter

Introduction

In this page you can find the example usage for org.hibernate.mapping Property getSetter.

Prototype

public Setter getSetter(Class clazz) throws PropertyNotFoundException, MappingException 

Source Link

Usage

From source file:it.eng.qbe.datasource.hibernate.HibernatePersistenceManager.java

License:Mozilla Public License

public void updateRecord(JSONObject aRecord, RegistryConfiguration registryConf) {

    SessionFactory sf = dataSource.getHibernateSessionFactory();
    Configuration cfg = dataSource.getHibernateConfiguration();
    Session aSession = null;/* ww w .  j a v  a2  s. com*/
    Transaction tx = null;
    try {
        aSession = sf.openSession();
        tx = aSession.beginTransaction();
        String entityName = registryConf.getEntity();
        PersistentClass classMapping = cfg.getClassMapping(entityName);
        ClassMetadata classMetadata = sf.getClassMetadata(entityName);
        String keyName = classMetadata.getIdentifierPropertyName();
        Object key = aRecord.get(keyName);
        Property propertyId = classMapping.getProperty(keyName);

        //casts the id to the appropriate java type
        Object keyConverted = this.convertValue(key, propertyId);

        Object obj = aSession.load(entityName, (Serializable) keyConverted);
        Iterator it = aRecord.keys();
        while (it.hasNext()) {
            String aKey = (String) it.next();
            if (keyName.equals(aKey)) {
                continue;
            }
            Column c = registryConf.getColumnConfiguration(aKey);
            if (c.getSubEntity() != null) {
                // case of foreign key
                Property property = classMapping.getProperty(c.getSubEntity());
                Type propertyType = property.getType();
                if (propertyType instanceof ManyToOneType) {
                    ManyToOneType manyToOnePropertyType = (ManyToOneType) propertyType;
                    String entityType = manyToOnePropertyType.getAssociatedEntityName();
                    Object referenced = getReferencedObject(aSession, entityType, c.getField(),
                            aRecord.get(aKey));
                    Setter setter = property.getSetter(obj.getClass());
                    setter.getMethod().invoke(obj, referenced);
                } else {
                    throw new SpagoBIRuntimeException(
                            "Property " + c.getSubEntity() + " is not a many-to-one relation");
                }
            } else {
                // case of property
                Property property = classMapping.getProperty(aKey);
                Setter setter = property.getSetter(obj.getClass());
                Object valueObj = aRecord.get(aKey);
                if (valueObj != null && !valueObj.equals("")) {
                    Object valueConverted = this.convertValue(valueObj, property);
                    setter.getMethod().invoke(obj, valueConverted);
                }

            }

        }
        aSession.saveOrUpdate(obj);
        tx.commit();

    } catch (Exception e) {

        if (tx != null) {
            tx.rollback();
        }
        throw new RuntimeException(e);
    } finally {
        if (aSession != null) {
            if (aSession.isOpen())
                aSession.close();
        }
    }
}

From source file:org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

public static GroovyAwareJavassistProxyFactory buildProxyFactory(PersistentClass persistentClass) {
    GroovyAwareJavassistProxyFactory proxyFactory = new GroovyAwareJavassistProxyFactory();

    @SuppressWarnings("unchecked")
    Set<Class<HibernateProxy>> proxyInterfaces = CollectionUtils.newSet(HibernateProxy.class);

    final Class<?> javaClass = persistentClass.getMappedClass();
    final Property identifierProperty = persistentClass.getIdentifierProperty();
    final Getter idGetter = identifierProperty != null ? identifierProperty.getGetter(javaClass) : null;
    final Setter idSetter = identifierProperty != null ? identifierProperty.getSetter(javaClass) : null;

    if (idGetter == null || idSetter == null)
        return null;

    try {//ww  w  . jav a 2s. c om
        proxyFactory.postInstantiate(persistentClass.getEntityName(), javaClass, proxyInterfaces,
                idGetter.getMethod(), idSetter.getMethod(),
                persistentClass.hasEmbeddedIdentifier()
                        ? (CompositeType) persistentClass.getIdentifier().getType()
                        : null);
    } catch (HibernateException e) {
        LOG.warn("Cannot instantiate proxy factory: " + e.getMessage());
        return null;
    }

    return proxyFactory;
}

From source file:org.eclipse.emf.cdo.server.internal.hibernate.tuplizer.CDORevisionTuplizer.java

License:Open Source License

@Override
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
    initEClass(mappedEntity);//from w ww.  jav a2s .c  o m
    if (TRACER.isEnabled()) {
        TRACER.trace("Building property setter for " + eClass.getName() + "." + mappedProperty.getName()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    if (mappedProperty.isBackRef()) {
        return mappedProperty.getSetter(mappedEntity.getMappedClass());
    }

    final CDOPropertySetter setter;
    if (mappedProperty == mappedEntity.getIdentifierProperty()) {
        setIdentifierTypeAsAnnotation(mappedProperty);
        setter = new CDOIDPropertySetter(this, mappedProperty.getName());
    } else if (mappedProperty.getMetaAttribute("version") != null) {
        setter = new CDOVersionPropertySetter(this, mappedProperty.getName());
    } else if (mappedProperty.getName().compareTo(CDOHibernateConstants.RESOURCE_PROPERTY) == 0) {
        setter = new CDOResourceIDSetter(this, mappedProperty.getName());
    } else if (mappedProperty.getName().compareTo(CDOHibernateConstants.CONTAINER_PROPERTY) == 0) {
        setter = new CDOContainerSetter(this, mappedProperty.getName());
    } else if (mappedProperty.getName().compareTo(CDOHibernateConstants.COMMITTIMESTAMP_PROPERTY) == 0) {
        setter = new CDOBranchTimeStampSetter(this, mappedProperty.getName());
    } else {
        EStructuralFeature feature = getEClass().getEStructuralFeature(mappedProperty.getName());
        if (feature instanceof EReference && feature.isMany()
                && HibernateUtil.getInstance().isCDOResourceContents(feature)) {
            setter = new CDOManyAttributeSetter(this, mappedProperty.getName());
        } else if (feature instanceof EReference && feature.isMany()) {
            setter = new CDOManyReferenceSetter(this, mappedProperty.getName());
        } else if (feature instanceof EAttribute && feature.isMany()) {
            setter = new CDOManyAttributeSetter(this, mappedProperty.getName());
        } else

        if (feature instanceof EReference) {
            setter = new CDOReferenceSetter(this, mappedProperty.getName());
        } else {
            setter = new CDOPropertySetter(this, mappedProperty.getName());
        }
    }

    HibernateStore hbStore = HibernateStore.getCurrentHibernateStore();
    setter.setPersistenceOptions(hbStore.getCDODataStore().getPersistenceOptions());
    return setter;
}

From source file:org.eclipse.emf.teneo.hibernate.mapping.eav.EAVObjectTuplizer.java

License:Open Source License

@Override
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
    if (mappedProperty.getName().equals("values")) {
        return mappedProperty.getSetter(EObjectImpl.class);
    }//from  www. ja  v  a2  s  .c o  m
    return getPropertyAccessor(mappedProperty, mappedEntity).getSetter(null, mappedProperty.getName());
}

From source file:org.eclipse.emf.teneo.hibernate.tuplizer.EMFTuplizer.java

License:Open Source License

@Override
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
    if (HbUtil.isEAVMapped(mappedEntity) && mappedProperty.getName().equals(Constants.EAV_EOBJECT_VALUES)) {
        final HbDataStore ds = HbHelper.INSTANCE.getDataStore(mappedEntity);
        final Setter setter = mappedProperty.getSetter(EObjectImpl.class);
        if (setter instanceof EAVPropertyHandler) {
            ((EAVPropertyHandler) setter).setHbDataStore(ds);
        }/*from  ww w . j ava  2 s  .c  o m*/
        return setter;
    }
    return getPropertyAccessor(mappedProperty, mappedEntity).getSetter(null, mappedProperty.getName());
}

From source file:org.grails.orm.hibernate.cfg.GrailsHibernateUtil.java

License:Apache License

/**
 * Constructs a proxy factory instance//from   ww  w . java  2s  .c  o m
 *
 * @param persistentClass The persistent class
 * @return The factory
 */
public static ProxyFactory buildProxyFactory(PersistentClass persistentClass) {
    ProxyFactory proxyFactory = ProxyFactorySupport.createProxyFactory();

    @SuppressWarnings("unchecked")
    Set<Class> proxyInterfaces = new HashSet<>();
    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;
}

From source file:org.openbravo.dal.core.OBDynamicTuplizer.java

License:Open Source License

@Override
protected Setter buildPropertySetter(Property mappedProperty, PersistentClass mappedEntity) {
    return mappedProperty.getSetter(mappedEntity.getMappedClass());
}