コード例 #1
0
  @Test
  public void testNative() throws Exception {
    final ServiceReference sr = bundleContext.getServiceReference(SessionFactory.class.getName());
    final SessionFactory sf = (SessionFactory) bundleContext.getService(sr);

    Session s = sf.openSession();
    s.getTransaction().begin();
    s.persist(new DataPoint("Brett"));
    s.getTransaction().commit();
    s.close();

    s = sf.openSession();
    s.getTransaction().begin();
    DataPoint dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNotNull(dp);
    assertEquals("Brett", dp.getName());
    s.getTransaction().commit();
    s.close();

    dp.setName("Brett2");

    s = sf.openSession();
    s.getTransaction().begin();
    s.update(dp);
    s.getTransaction().commit();
    s.close();

    s = sf.openSession();
    s.getTransaction().begin();
    dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNotNull(dp);
    assertEquals("Brett2", dp.getName());
    s.getTransaction().commit();
    s.close();

    s = sf.openSession();
    s.getTransaction().begin();
    s.createQuery("delete from DataPoint").executeUpdate();
    s.getTransaction().commit();
    s.close();

    s = sf.openSession();
    s.getTransaction().begin();
    dp = (DataPoint) s.get(DataPoint.class, 1);
    assertNull(dp);
    s.getTransaction().commit();
    s.close();
  }
コード例 #2
0
  @Test
  public void testJpa() throws Exception {
    final ServiceReference serviceReference =
        bundleContext.getServiceReference(PersistenceProvider.class.getName());
    final PersistenceProvider persistenceProvider =
        (PersistenceProvider) bundleContext.getService(serviceReference);
    final EntityManagerFactory emf =
        persistenceProvider.createEntityManagerFactory("hibernate-osgi-test", null);

    EntityManager em = emf.createEntityManager();
    em.getTransaction().begin();
    em.persist(new DataPoint("Brett"));
    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();
    DataPoint dp = em.find(DataPoint.class, 1);
    assertNotNull(dp);
    assertEquals("Brett", dp.getName());
    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();
    dp = em.find(DataPoint.class, 1);
    dp.setName("Brett2");
    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();
    em.createQuery("delete from DataPoint").executeUpdate();
    em.getTransaction().commit();
    em.close();

    em = emf.createEntityManager();
    em.getTransaction().begin();
    dp = em.find(DataPoint.class, 1);
    assertNull(dp);
    em.getTransaction().commit();
    em.close();
  }