Beispiel #1
0
 private Object getEntityId(Object obj, HibernateTemplate ht)
     throws IllegalAccessException, InvocationTargetException {
   ClassMetadata cm = ht.getSessionFactory().getClassMetadata(superClass);
   String idCol = cm.getIdentifierPropertyName();
   PropertyDescriptor idPropDescr = BeanUtils.getPropertyDescriptor(superClass, idCol);
   return idPropDescr.getReadMethod().invoke(obj);
 }
 /**
  * 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 #3
0
 /** 取得对象的主键名,辅助函数. */
 public String getIdName(Class<T> clazz) {
   Assert.notNull(clazz);
   ClassMetadata meta = getSessionFactory().getClassMetadata(clazz);
   Assert.notNull(meta, "Class " + clazz + " not define in hibernate session factory.");
   String idName = meta.getIdentifierPropertyName();
   Assert.hasText(idName, clazz.getSimpleName() + " has no identifier property define.");
   return idName;
 }
  /**
   * 取得对象的主键名,辅助函数.
   *
   * @param entityClass 实体类型
   * @return 主键名称
   */
  public String getIdName(Class entityClass) {
    Assert.notNull(entityClass);
    entityClass = ReflectUtils.getOriginalClass(entityClass);

    ClassMetadata meta = this.getSessionFactory().getClassMetadata(entityClass);
    Assert.notNull(meta, "Class " + entityClass + " not define in hibernate session factory.");

    String idName = meta.getIdentifierPropertyName();
    Assert.hasText(idName, entityClass.getSimpleName() + " has no identifier property define.");

    return idName;
  }
Beispiel #5
0
  @Override
  public void delete(long id) {
    String entityName = entityClassMetadata.getEntityName();
    String idPropertyName = entityClassMetadata.getIdentifierPropertyName();

    String hqlDeleteQuery =
        new StringBuffer()
            .append("delete ")
            .append(entityName)
            .append(" where ")
            .append(idPropertyName)
            .append(" = :id")
            .toString();

    Query deleteQuery = getCurrentSession().createQuery(hqlDeleteQuery);
    deleteQuery.setParameter("id", id);

    deleteQuery.executeUpdate();
  }
  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
  }
Beispiel #7
0
 /** 取得对象的主键名. */
 public String getIdName() {
   ClassMetadata meta = getSessionFactory().getClassMetadata(entityClass);
   return meta.getIdentifierPropertyName();
 }
 /** 取得对象的主键名. */
 public String getIdName() {
   ClassMetadata meta = getSessionFactory().getClassMetadata(entityClass);
   Assert.notNull(
       meta, "Class " + entityClass.getSimpleName() + " not define in HibernateSessionFactory.");
   return meta.getIdentifierPropertyName();
 }