Example #1
0
 private boolean auto(String operation) {
   long startTime = System.currentTimeMillis();
   System.out.println("Operation: " + operation + "...");
   HibernateEntityManagerFactory emf = null;
   EntityManager em = null;
   try {
     Map<String, String> map = getPeristencePropertiesFixedMap();
     if (operation != null) {
       map.put("hibernate.hbm2ddl.auto", operation);
     }
     if (operation.equals("update")) {
       if (getDriver().equals(TestStation.Driver.derby)) {
         String url = map.get("hibernate.connection.url");
         if (!url.contains("create=true")) {
           url += ";create=true";
         }
         //                    if (!url.contains("logDevice=")) {
         //                        url += ";logDevice=" + getUserHome() + File.separator +
         // ".jtstand";
         //                    }
         map.put("hibernate.connection.url", url);
       }
     }
     emf =
         (HibernateEntityManagerFactory)
             Persistence.createEntityManagerFactory(getTestProject().getPun(), map);
     //            emf.getSessionFactory().getAllClassMetadata();
     //            System.out.println(emf.getSessionFactory().getAllClassMetadata());
     em = emf.createEntityManager();
     em.getTransaction().begin();
     em.getTransaction().commit();
     //            System.out.println("Closing entity manager");
     em.close();
     //            System.out.println("Closing entity manager factory");
     emf.close();
     System.out.println(
         "Database "
             + operation
             + " operation succeeded in "
             + Long.toString(System.currentTimeMillis() - startTime)
             + "ms");
     return true;
   } catch (Exception ex) {
     ex.printStackTrace();
     System.out.println(ex.getMessage());
     if (em != null && em.isOpen()) {
       em.close();
     }
     if (emf != null && emf.isOpen()) {
       emf.close();
     }
   }
   System.out.println(
       "Database "
           + operation
           + " operation failed in "
           + Long.toString(System.currentTimeMillis() - startTime)
           + "ms");
   return false;
 }
 public Object getIdentifier(Object entity) {
   final Class entityClass = Hibernate.getClass(entity);
   final ClassMetadata classMetadata = emf.getSessionFactory().getClassMetadata(entityClass);
   if (classMetadata == null) {
     throw new IllegalArgumentException(entityClass + " is not an entity");
   }
   // TODO does that work for @IdClass?
   return classMetadata.getIdentifier(entity);
 }
  public EntityInformation[] locate(EntityManagerFactory entityManagerFactory, JpaGpsDevice device)
      throws JpaGpsDeviceException {

    CompassGpsInterfaceDevice gps = (CompassGpsInterfaceDevice) device.getGps();

    HibernateEntityManagerFactory hibernateEntityManagerFactory =
        (HibernateEntityManagerFactory) entityManagerFactory;
    SessionFactory sessionFactory = hibernateEntityManagerFactory.getSessionFactory();

    ArrayList<EntityInformation> entitiesList = new ArrayList<EntityInformation>();

    Map allClassMetaData = sessionFactory.getAllClassMetadata();
    for (Object o : allClassMetaData.keySet()) {
      String entityname = (String) o;
      if (!gps.hasMappingForEntityForIndex((entityname))) {
        if (log.isDebugEnabled()) {
          log.debug("Entity [" + entityname + "] does not have compass mapping, filtering it out");
        }
        continue;
      }

      ClassMetadata classMetadata = (ClassMetadata) allClassMetaData.get(entityname);
      if (shouldFilter(entityname, classMetadata, allClassMetaData, device)) {
        continue;
      }
      Class<?> clazz = classMetadata.getMappedClass();
      ResourceMapping resourceMapping = gps.getMappingForEntityForIndex(entityname);
      EntityInformation entityInformation =
          new EntityInformation(
              clazz,
              entityname,
              new HibernateJpaQueryProvider(clazz, entityname),
              resourceMapping.getSubIndexHash().getSubIndexes());
      entitiesList.add(entityInformation);
      if (log.isDebugEnabled()) {
        log.debug("Entity [" + entityname + "] will be indexed");
      }
    }
    return entitiesList.toArray(new EntityInformation[entitiesList.size()]);
  }