/**
  * Parse the {@link DataAccessException} to see if special problems were encountered while
  * performing the query. See issue NMS-5029 for examples of stack traces that can be thrown from
  * these calls. {@see http://issues.opennms.org/browse/NMS-5029}
  */
 private void logExtraSaveOrUpdateExceptionInformation(
     final T entity, final DataAccessException e) {
   Throwable cause = e;
   while (cause.getCause() != null) {
     // if (cause.getCause().getClass().getName().equals(PSQLException.class.getName())) {
     if (cause.getMessage().contains("duplicate key value violates unique constraint")) {
       final ClassMetadata meta = getSessionFactory().getClassMetadata(m_entityClass);
       LogUtils.warnf(
           this,
           "Duplicate key constraint violation, class: %s, key value: %s",
           m_entityClass.getName(),
           meta.getPropertyValue(entity, meta.getIdentifierPropertyName(), EntityMode.POJO));
       break;
     } else if (cause.getMessage().contains("given object has a null identifier")) {
       LogUtils.warnf(
           this,
           "Null identifier on object, class: %s: %s",
           m_entityClass.getName(),
           entity.toString());
       break;
     }
     // }
     cause = cause.getCause();
   }
 }
Beispiel #2
0
  public void initializeCollection(T instance, String collectionName) {
    ClassMetadata classMetadata = sessionFactory.getClassMetadata(instance.getClass());
    Object collection = classMetadata.getPropertyValue(instance, collectionName);

    if (!Hibernate.isInitialized(collection)) {
      attachClean(instance);
      Hibernate.initialize(collection);
    }
  }
  private void addChildExampleCriteria(Object exampleInstance, Criteria criteria, Session sesion) {
    ClassMetadata metadata =
        sesion.getSessionFactory().getClassMetadata(exampleInstance.getClass());

    String[] propertyNames = metadata.getPropertyNames();
    Type[] propertyTypes = metadata.getPropertyTypes();

    // see if this example instance has any properties that are entities
    for (int i = 0; i < propertyNames.length; i++) {

      String propertyName = propertyNames[i];
      Type propertyType = propertyTypes[i];

      if (propertyType instanceof EntityType) {
        // this property is an association - Hibernate's Example ignores
        // these
        Object value = metadata.getPropertyValue(exampleInstance, propertyName, EntityMode.POJO);

        if (value != null) {

          ClassMetadata childMetadata =
              sesion.getSessionFactory().getClassMetadata(value.getClass());
          Criteria childCriteria = criteria.createCriteria(propertyName);

          if (childMetadata.hasIdentifierProperty()) {

            Object id = childMetadata.getIdentifier(value, EntityMode.POJO);
            if (id != null) {
              // add the identifier to the child criteria
              childCriteria.add(Restrictions.eq(childMetadata.getIdentifierPropertyName(), id));
            }
          }

          // add the entity's fields as Example fields
          childCriteria.add(Example.create(value));

          // add this entity's associations
          addChildExampleCriteria(value, childCriteria, sesion);
        }
      }
    } // ~for
  }
 /** uses the {@link ClassMetadata} for this {@link Locks} tp retrieve the component value. */
 public Object getSubtypeValue(int i, int j, Object o) {
   return cm.getPropertyValue(o, subnames[i][j], EntityMode.POJO);
 }