public void remove(Object entity) {
   checkTransactionNeeded();
   try {
     getSession().delete(entity);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (RuntimeException e) { // including HibernateException
     throw convert(e);
   }
 }
 public void persist(Object entity) {
   checkTransactionNeeded();
   try {
     getSession().persist(entity);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage());
   } catch (RuntimeException e) {
     throw convert(e);
   }
 }
Exemplo n.º 3
0
  private JosekiManager() {
    logger = Logger.getLogger(JosekiManager.class);
    Configuration config = new Configuration();

    try {
      config.addClass(MCJosekiEntry.class);
      sessionFactory = config.buildSessionFactory();
    } catch (MappingException ex) {
      logger.error("MappingException: " + ex.getMessage());
    } catch (HibernateException ex) {
      logger.error("HibernateException: " + ex.getMessage());
    }
  }
 @SuppressWarnings("unchecked")
 public <A> A merge(A entity) {
   checkTransactionNeeded();
   try {
     return (A) getSession().merge(entity);
   } catch (ObjectDeletedException sse) {
     throw new IllegalArgumentException(sse);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (RuntimeException e) { // including HibernateException
     throw convert(e);
   }
 }
 public boolean contains(Object entity) {
   try {
     if (entity != null
         && !(entity instanceof HibernateProxy)
         && getSession().getSessionFactory().getClassMetadata(entity.getClass()) == null) {
       throw new IllegalArgumentException("Not an entity:" + entity.getClass());
     }
     return getSession().contains(entity);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (HibernateException he) {
     throw convert(he);
   }
 }
 @SuppressWarnings("unchecked")
 public <T> T getReference(Class<T> entityClass, Object primaryKey) {
   try {
     return (T) getSession().load(entityClass, (Serializable) primaryKey);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (TypeMismatchException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (ClassCastException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (HibernateException he) {
     throw convert(he);
   }
 }
 public <A> A find(
     Class<A> entityClass,
     Object primaryKey,
     LockModeType lockModeType,
     Map<String, Object> properties) {
   CacheMode previousCacheMode = getSession().getCacheMode();
   CacheMode cacheMode = determineAppropriateLocalCacheMode(properties);
   LockOptions lockOptions = null;
   try {
     getSession().setCacheMode(cacheMode);
     if (lockModeType != null) {
       return (A)
           getSession()
               .get(
                   entityClass,
                   (Serializable) primaryKey,
                   getLockRequest(lockModeType, properties));
     } else {
       return (A) getSession().get(entityClass, (Serializable) primaryKey);
     }
   } catch (ObjectDeletedException e) {
     // the spec is silent about people doing remove() find() on the same PC
     return null;
   } catch (ObjectNotFoundException e) {
     // should not happen on the entity itself with get
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (TypeMismatchException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (ClassCastException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (HibernateException he) {
     throw convert(he, lockOptions);
   } finally {
     getSession().setCacheMode(previousCacheMode);
   }
 }
 public void refresh(Object entity, LockModeType lockModeType, Map<String, Object> properties) {
   checkTransactionNeeded();
   CacheMode previousCacheMode = getSession().getCacheMode();
   CacheMode localCacheMode = determineAppropriateLocalCacheMode(properties);
   LockOptions lockOptions = null;
   try {
     getSession().setCacheMode(localCacheMode);
     if (!getSession().contains(entity)) {
       throw new IllegalArgumentException("Entity not managed");
     }
     if (lockModeType != null) {
       getSession().refresh(entity, (lockOptions = getLockRequest(lockModeType, properties)));
     } else {
       getSession().refresh(entity);
     }
   } catch (MappingException e) {
     throw new IllegalArgumentException(e.getMessage(), e);
   } catch (HibernateException he) {
     throw convert(he, lockOptions);
   } finally {
     getSession().setCacheMode(previousCacheMode);
   }
 }
 private Integer internalGetPropertyMaxLength(final String entityName, final String propertyName) {
   Integer length = columnLengthMap.get(getKey(entityName, propertyName));
   if (length != null) {
     return length;
   }
   if (columnLengthFailedSet.contains(getKey(entityName, propertyName)) == true) {
     return null;
   }
   final PersistentClass persistentClass = configuration.getClassMapping(entityName);
   if (persistentClass == null) {
     final String msg =
         "Could not find persistent class for entityName '"
             + entityName
             + "' (OK for non hibernate objects).";
     if (entityName.endsWith("DO") == true) {
       log.error(msg);
     } else {
       log.info(msg);
     }
     putFailedEntry(entityName, propertyName);
     return null;
   }
   Column column = persistentClass.getTable().getColumn(new Column(propertyName));
   if (column == null) {
     // OK, may be the database name of the column differs, e. g. if a different name is set via
     // @Column(name = "...").
     Property property = null;
     try {
       property = persistentClass.getProperty(propertyName);
     } catch (final MappingException ex) {
       if (TEST_MODE == false) {
         log.error(ex.getMessage(), ex);
       } else {
         log.info("***** TESTMODE: property '" + propertyName + "' not found (OK in test mode).");
       }
       putFailedEntry(entityName, propertyName);
       return null;
     }
     final Iterator<?> it = property.getColumnIterator();
     if (it.hasNext() == true) {
       column = (Column) it.next();
       if (it.hasNext() == true) {
         putFailedEntry(entityName, propertyName);
         throw new UnsupportedOperationException(
             "Multiple columns for selected entity '"
                 + entityName
                 + "' with name '"
                 + propertyName
                 + "' not predictable, aborting.");
       }
     }
   }
   if (column == null) {
     log.error(
         "Could not find column for entity '"
             + entityName
             + "' with name '"
             + propertyName
             + "'.");
     return null;
   }
   length = column.getLength();
   columnLengthMap.put(entityName + "#" + propertyName, length);
   return length;
 }
Exemplo n.º 10
0
  public static int checkReferencedColumnsType(
      Ejb3JoinColumn[] columns, PersistentClass referencedEntity, MetadataBuildingContext context) {
    // convenient container to find whether a column is an id one or not
    Set<Column> idColumns = new HashSet<Column>();
    Iterator idColumnsIt = referencedEntity.getKey().getColumnIterator();
    while (idColumnsIt.hasNext()) {
      idColumns.add((Column) idColumnsIt.next());
    }

    boolean isFkReferencedColumnName = false;
    boolean noReferencedColumn = true;
    // build the list of potential tables
    if (columns.length == 0) return NO_REFERENCE; // shortcut
    Object columnOwner =
        BinderHelper.findColumnOwner(referencedEntity, columns[0].getReferencedColumn(), context);
    if (columnOwner == null) {
      try {
        throw new MappingException(
            "Unable to find column with logical name: "
                + columns[0].getReferencedColumn()
                + " in "
                + referencedEntity.getTable()
                + " and its related "
                + "supertables and secondary tables");
      } catch (MappingException e) {
        throw new RecoverableException(e.getMessage(), e);
      }
    }
    Table matchingTable =
        columnOwner instanceof PersistentClass
            ? ((PersistentClass) columnOwner).getTable()
            : ((Join) columnOwner).getTable();
    // check each referenced column
    for (Ejb3JoinColumn ejb3Column : columns) {
      String logicalReferencedColumnName = ejb3Column.getReferencedColumn();
      if (StringHelper.isNotEmpty(logicalReferencedColumnName)) {
        String referencedColumnName;
        try {
          referencedColumnName =
              context
                  .getMetadataCollector()
                  .getPhysicalColumnName(matchingTable, logicalReferencedColumnName);
        } catch (MappingException me) {
          // rewrite the exception
          throw new MappingException(
              "Unable to find column with logical name: "
                  + logicalReferencedColumnName
                  + " in "
                  + matchingTable.getName());
        }
        noReferencedColumn = false;
        Column refCol = new Column(referencedColumnName);
        boolean contains = idColumns.contains(refCol);
        if (!contains) {
          isFkReferencedColumnName = true;
          break; // we know the state
        }
      }
    }
    if (isFkReferencedColumnName) {
      return NON_PK_REFERENCE;
    } else if (noReferencedColumn) {
      return NO_REFERENCE;
    } else if (idColumns.size() != columns.length) {
      // reference use PK but is a subset or a superset
      return NON_PK_REFERENCE;
    } else {
      return PK_REFERENCE;
    }
  }