Example usage for org.hibernate.metadata ClassMetadata setPropertyValue

List of usage examples for org.hibernate.metadata ClassMetadata setPropertyValue

Introduction

In this page you can find the example usage for org.hibernate.metadata ClassMetadata setPropertyValue.

Prototype

void setPropertyValue(Object object, String propertyName, Object value) throws HibernateException;

Source Link

Document

Set the value of a particular (named) property

Usage

From source file:org.cgiar.ccafs.marlo.data.dao.mysql.AuditLogMySQLDao.java

License:Open Source License

public void loadRelationsForIAuditLog(IAuditLog iAuditLog, String transactionID)
        throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
    try {/*w  w  w . j av  a  2  s. c  om*/

        Session session = super.getSessionFactory().getCurrentSession();
        ClassMetadata classMetadata = session.getSessionFactory().getClassMetadata(iAuditLog.getClass());
        String[] propertyNames = classMetadata.getPropertyNames();
        for (String name : propertyNames) {
            try {
                Type propertyType = classMetadata.getPropertyType(name);
                Field privateField = iAuditLog.getClass().getDeclaredField(name);
                privateField.setAccessible(true);
                if (propertyType instanceof OneToOneType) {
                    Object obj = privateField.get(iAuditLog);
                    if (obj != null && obj instanceof IAuditLog) {
                        this.loadRelationsForIAuditLog((IAuditLog) obj, transactionID);
                    }
                }
                if (propertyType instanceof OrderedSetType || propertyType instanceof SetType) {

                    String classNameRelation = propertyType.getName();
                    String sql = "from " + Auditlog.class.getName() + " where transaction_id='" + transactionID
                            + "' and main=3 and relation_name='" + classNameRelation + ":" + iAuditLog.getId()
                            + "'order by ABS(ENTITY_ID) asc";
                    List<Auditlog> auditLogsRelations = super.findAll(sql);

                    Set<IAuditLog> relation = new HashSet<IAuditLog>();
                    for (Auditlog auditlog : auditLogsRelations) {
                        IAuditLog relationObject = this.loadFromAuditLog(auditlog);
                        this.loadRelationsForIAuditLog(relationObject, transactionID);
                        relation.add(relationObject);
                    }
                    classMetadata.setPropertyValue(iAuditLog, name, relation);
                }
            } catch (Exception e) {

            }
        }
    } catch (JsonSyntaxException e) {
        e.printStackTrace();
    }

}

From source file:org.unitime.timetable.backup.SessionRestore.java

License:Open Source License

public void create(TableData.Table table)
        throws InstantiationException, IllegalAccessException, DocumentException {
    ClassMetadata metadata = iHibSessionFactory.getClassMetadata(table.getName());
    if (metadata == null)
        return;/*  w  w w .  j  a  v  a2s .  c  o m*/
    PersistentClass mapping = _RootDAO.getConfiguration().getClassMapping(table.getName());
    Map<String, Integer> lengths = new HashMap<String, Integer>();
    for (String property : metadata.getPropertyNames()) {
        Type type = metadata.getPropertyType(property);
        if (type instanceof StringType)
            for (Iterator<?> i = mapping.getProperty(property).getColumnIterator(); i.hasNext();) {
                Object o = i.next();
                if (o instanceof Column) {
                    Column column = (Column) o;
                    lengths.put(property, column.getLength());
                }
                break;
            }
    }
    iProgress.setPhase(metadata.getEntityName().substring(metadata.getEntityName().lastIndexOf('.') + 1) + " ["
            + table.getRecordCount() + "]", table.getRecordCount());
    for (TableData.Record record : table.getRecordList()) {
        iProgress.incProgress();
        Object object = metadata.getMappedClass().newInstance();
        for (String property : metadata.getPropertyNames()) {
            TableData.Element element = null;
            for (TableData.Element e : record.getElementList())
                if (e.getName().equals(property)) {
                    element = e;
                    break;
                }
            if (element == null)
                continue;
            Object value = null;
            Type type = metadata.getPropertyType(property);
            if (type instanceof PrimitiveType) {
                if (type instanceof BooleanType) {
                    value = new Boolean("true".equals(element.getValue(0)));
                } else if (type instanceof ByteType) {
                    value = Byte.valueOf(element.getValue(0));
                } else if (type instanceof CharacterType) {
                    value = Character.valueOf(element.getValue(0).charAt(0));
                } else if (type instanceof DoubleType) {
                    value = Double.valueOf(element.getValue(0));
                } else if (type instanceof FloatType) {
                    value = Float.valueOf(element.getValue(0));
                } else if (type instanceof IntegerType) {
                    value = Integer.valueOf(element.getValue(0));
                } else if (type instanceof LongType) {
                    value = Long.valueOf(element.getValue(0));
                } else if (type instanceof ShortType) {
                    value = Short.valueOf(element.getValue(0));
                }
            } else if (type instanceof DateType) {
                try {
                    value = new SimpleDateFormat("dd MMMM yyyy", Localization.getJavaLocale())
                            .parse(element.getValue(0));
                } catch (ParseException e) {
                    value = new DateType().fromStringValue(element.getValue(0));
                }
            } else if (type instanceof TimestampType) {
                value = new TimestampType().fromStringValue(element.getValue(0));
            } else if (type instanceof StringType) {
                value = element.getValue(0);
                Integer len = lengths.get(property);
                if (len != null && value.toString().length() > len) {
                    message("Value is  too long, truncated (property " + metadata.getEntityName() + "."
                            + property + ", length " + len + ")", record.getId());
                    value = value.toString().substring(0, len);
                }
            } else if (type instanceof BinaryType) {
                value = ByteString.copyFromUtf8(element.getValue(0)).toByteArray();
            } else if (type instanceof CustomType && type.getReturnedClass().equals(Document.class)) {
                value = new SAXReader().read(new StringReader(element.getValue(0)));
            } else if (type instanceof EntityType) {
            } else if (type instanceof CollectionType) {
            } else {
                message("Unknown type " + type.getClass().getName() + " (property " + metadata.getEntityName()
                        + "." + property + ", class " + type.getReturnedClass() + ")", record.getId());
            }
            if (value != null)
                metadata.setPropertyValue(object, property, value);
        }
        add(new Entity(metadata, record, object, record.getId()));
    }
}