Esempio n. 1
0
  public void testInsert() throws HibernateException, SQLException {
    if (isUsingIdentity()) {
      reportSkip("hand sql expecting non-identity id gen", "Custom SQL");
      return;
    }

    Role p = new Role();

    p.setName("Patient");

    Session s = openSession();

    s.save(p);
    s.flush();

    s.connection().commit();
    s.close();

    getSessions().evict(Role.class);
    s = openSession();

    Role p2 = (Role) s.get(Role.class, new Long(p.getId()));
    assertNotSame(p, p2);
    assertEquals(p2.getId(), p.getId());
    assertTrue(p2.getName().equalsIgnoreCase(p.getName()));
    s.delete(p2);
    s.flush();

    s.connection().commit();
    s.close();
  }
Esempio n. 2
0
  public void testCRUD() throws HibernateException, SQLException {
    if (isUsingIdentity()) {
      reportSkip("hand sql expecting non-identity id gen", "Custom SQL");
      return;
    }

    Person p = new Person();

    p.setName("Max");
    p.setLastName("Andersen");
    p.setNationalID("110974XYZ�");
    p.setAddress("P. P. Street 8");

    Session s = openSession();

    s.save(p);
    s.flush();

    s.connection().commit();
    s.close();

    getSessions().evict(Person.class);
    s = openSession();

    Person p2 = (Person) s.get(Person.class, p.getId());
    assertNotSame(p, p2);
    assertEquals(p2.getId(), p.getId());
    assertEquals(p2.getLastName(), p.getLastName());
    s.flush();

    List list = s.createQuery("select p from Party as p").list();
    assertTrue(list.size() == 1);

    s.connection().commit();
    s.close();

    s = openSession();

    list = s.createQuery("select p from Person as p where p.address = 'L�rkev�nget 1'").list();
    assertTrue(list.size() == 0);
    p.setAddress("L�rkev�nget 1");
    s.update(p);
    list = s.createQuery("select p from Person as p where p.address = 'L�rkev�nget 1'").list();
    assertTrue(list.size() == 1);
    list = s.createQuery("select p from Party as p where p.address = 'P. P. Street 8'").list();
    assertTrue(list.size() == 0);

    s.delete(p);
    list = s.createQuery("select p from Person as p").list();
    assertTrue(list.size() == 0);

    s.connection().commit();
    s.close();
  }
  public void testInterceptorWithThreadBoundEmptyHolder() {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    SessionHolder holder = new SessionHolder("key", session);
    holder.removeSession("key");
    TransactionSynchronizationManager.bindResource(sf, holder);
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testInterceptorWithThreadBoundAndFlushEagerSwitch() throws HibernateException {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    session.isOpen();
    sessionControl.setReturnValue(true, 1);
    session.getFlushMode();
    sessionControl.setReturnValue(FlushMode.NEVER, 1);
    session.setFlushMode(FlushMode.AUTO);
    sessionControl.setVoidCallable(1);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.setFlushMode(FlushMode.NEVER);
    sessionControl.setVoidCallable(1);
    sfControl.replay();
    sessionControl.replay();

    TransactionSynchronizationManager.bindResource(sf, new SessionHolder(session));
    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setFlushMode(HibernateInterceptor.FLUSH_EAGER);
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    } finally {
      TransactionSynchronizationManager.unbindResource(sf);
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testBroken() throws Exception {
    if (getDialect() instanceof Oracle9Dialect) return;
    Session s = openSession();
    Transaction t = s.beginTransaction();
    Broken b = new Fixed();
    b.setId(new Long(123));
    b.setOtherId("foobar");
    s.save(b);
    s.flush();
    b.setTimestamp(new Date());
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    s.update(b);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    b = (Broken) s.load(Broken.class, b);
    t.commit();
    s.close();

    s = openSession();
    t = s.beginTransaction();
    s.delete(b);
    t.commit();
    s.close();
  }
  public void testInterceptorWithNewSession() throws HibernateException {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf);
    session.flush();
    sessionControl.setVoidCallable(1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
    } catch (Throwable t) {
      fail("Should not have thrown Throwable: " + t.getMessage());
    }

    sfControl.verify();
    sessionControl.verify();
  }
  public void testInterceptorWithFlushFailure() throws Throwable {
    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession();
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 1);
    SQLException sqlEx = new SQLException("argh", "27");
    session.flush();
    ConstraintViolationException jdbcEx = new ConstraintViolationException("", sqlEx, null);
    sessionControl.setThrowable(jdbcEx, 1);
    session.close();
    sessionControl.setReturnValue(null, 1);
    sfControl.replay();
    sessionControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    try {
      interceptor.invoke(new TestInvocation(sf));
      fail("Should have thrown DataIntegrityViolationException");
    } catch (DataIntegrityViolationException ex) {
      // expected
      assertEquals(jdbcEx, ex.getCause());
    }

    sfControl.verify();
    sessionControl.verify();
  }
 @Test
 @Transactional
 public void testSaveOrderWithItems() throws Exception {
   Session session = sessionFactory.getCurrentSession();
   Order order = new Order();
   order.getItems().add(new Item());
   session.save(order);
   session.flush();
   assertNotNull(order.getId());
 }
Esempio n. 9
0
  @Override
  public String execute() throws Exception {
    String name = this.getRequest().getParameter("name");
    String value = this.getRequest().getParameter("value");
    String type = this.getRequest().getParameter("type");
    String policy = this.getRequest().getParameter("policy");
    String enable = this.getRequest().getParameter("enable");

    Session session = SessionFactorySingle.getSessionFactory().openSession();
    session.beginTransaction();

    Criteria criteria = session.createCriteria(TypeDeviceFilter.class);
    criteria.add(Restrictions.eq("id", Integer.valueOf(type)));
    List<TypeDeviceFilter> typeDeviceFilters_array = criteria.list();
    TypeDeviceFilter typeDeviceFilter = typeDeviceFilters_array.get(0);

    criteria = session.createCriteria(DeviceFilter.class);
    criteria.add(Restrictions.eq("name", name));
    List<DeviceFilter> deviceFilters = (List<DeviceFilter>) criteria.list();

    DeviceFilter deviceFilter;
    if (deviceFilters.size() > 0) {
      deviceFilter = deviceFilters.get(0);
      deviceFilter.setName(name);
      deviceFilter.setValue(value);
      deviceFilter.setType(typeDeviceFilter);
      deviceFilter.setPolicy(Boolean.valueOf(policy));
      deviceFilter.setEnable(Boolean.valueOf(enable));
    } else {
      deviceFilter = new DeviceFilter(name, value, typeDeviceFilter, Boolean.valueOf(policy));
    }

    session.save(deviceFilter);
    session.getTransaction().commit();

    criteria = session.createCriteria(TroubleList.class);
    criteria.add(Restrictions.eq("name", "main"));
    TroubleList troubleList = (TroubleList) criteria.list().get(0);
    troubleList.getFilters().add(deviceFilter);

    session.beginTransaction();

    session.save(troubleList);
    session.getTransaction().commit();

    session.flush();
    session.close();

    ManagerMainDeviceFilter.getInstance().addNewDeviceInputFilter(deviceFilter);

    return null;
  }
  public void testInterceptorWithEntityInterceptorBeanName() throws HibernateException {
    MockControl interceptorControl = MockControl.createControl(org.hibernate.Interceptor.class);
    org.hibernate.Interceptor entityInterceptor =
        (org.hibernate.Interceptor) interceptorControl.getMock();
    interceptorControl.replay();
    MockControl interceptor2Control = MockControl.createControl(org.hibernate.Interceptor.class);
    org.hibernate.Interceptor entityInterceptor2 =
        (org.hibernate.Interceptor) interceptor2Control.getMock();
    interceptor2Control.replay();

    MockControl sfControl = MockControl.createControl(SessionFactory.class);
    SessionFactory sf = (SessionFactory) sfControl.getMock();
    MockControl sessionControl = MockControl.createControl(Session.class);
    Session session = (Session) sessionControl.getMock();
    sf.openSession(entityInterceptor);
    sfControl.setReturnValue(session, 1);
    sf.openSession(entityInterceptor2);
    sfControl.setReturnValue(session, 1);
    session.getSessionFactory();
    sessionControl.setReturnValue(sf, 2);
    session.flush();
    sessionControl.setVoidCallable(2);
    session.close();
    sessionControl.setReturnValue(null, 2);
    sfControl.replay();
    sessionControl.replay();

    MockControl beanFactoryControl = MockControl.createControl(BeanFactory.class);
    BeanFactory beanFactory = (BeanFactory) beanFactoryControl.getMock();
    beanFactory.getBean("entityInterceptor", org.hibernate.Interceptor.class);
    beanFactoryControl.setReturnValue(entityInterceptor, 1);
    beanFactory.getBean("entityInterceptor", org.hibernate.Interceptor.class);
    beanFactoryControl.setReturnValue(entityInterceptor2, 1);
    beanFactoryControl.replay();

    HibernateInterceptor interceptor = new HibernateInterceptor();
    interceptor.setSessionFactory(sf);
    interceptor.setEntityInterceptorBeanName("entityInterceptor");
    interceptor.setBeanFactory(beanFactory);
    for (int i = 0; i < 2; i++) {
      try {
        interceptor.invoke(new TestInvocation(sf));
      } catch (Throwable t) {
        fail("Should not have thrown Throwable: " + t.getMessage());
      }
    }

    interceptorControl.verify();
    interceptor2Control.verify();
    sfControl.verify();
    sessionControl.verify();
  }
Esempio n. 11
0
  public void testJoinedSubclass() throws HibernateException, SQLException {
    Medication m = new Medication();

    m.setPrescribedDrug(new Drug());

    m.getPrescribedDrug().setName("Morphine");

    Session s = openSession();

    s.save(m.getPrescribedDrug());
    s.save(m);

    s.flush();
    s.connection().commit();
    s.close();
    s = openSession();

    Medication m2 = (Medication) s.get(Medication.class, m.getId());
    assertNotSame(m, m2);

    s.flush();
    s.connection().commit();
    s.close();
  }
 @Test
 @Transactional
 public void testSaveAndGet() throws Exception {
   Session session = sessionFactory.getCurrentSession();
   Order order = new Order();
   order.getItems().add(new Item());
   session.save(order);
   session.flush();
   // Otherwise the query returns the existing order (and we didn't set the
   // parent in the item)...
   session.clear();
   Order other = (Order) session.get(Order.class, order.getId());
   assertEquals(1, other.getItems().size());
   assertEquals(other, other.getItems().iterator().next().getOrder());
 }
Esempio n. 13
0
  public TempBanner getTempBannerByToken(String token) throws Exception {
    Session session = null;
    TempBanner data = null;
    String queryString = "from TempBanner d where d.dataToken = ?";

    try {
      session = HibernateUtil.getSessionFactory().openSession();
      data = (TempBanner) session.createQuery(queryString).setString(0, token).uniqueResult();
    } finally {
      if (session != null && session.isOpen()) {
        session.flush();
        session.close();
      }
    }

    return data;
  }
  @Test
  public void testNestableFlushEventListener() throws Exception {
    Session session = _prepareSession();

    Transaction transaction = session.beginTransaction();

    try {
      session.merge(_className1);
      session.merge(_className2);

      session.flush();
    } finally {
      transaction.commit();
    }

    session.close();
  }
Esempio n. 15
0
  public void saveTempBanner(TempBanner data) throws Exception {
    Session session = null;

    try {
      session = HibernateUtil.getSessionFactory().openSession();
      session.getTransaction().begin();
      session.save(data);
      session.getTransaction().commit();
    } catch (HibernateException e) {
      session.getTransaction().rollback();
      throw e;
    } finally {
      if (session != null && session.isOpen()) {
        session.flush();
        session.close();
      }
    }
  }
Esempio n. 16
0
  public TempBanner getTempBannerById(Integer tempDataId) {
    System.out.println("TempDataDaoImpl.getTempBannerById()");
    System.out.println("tempDataId = " + tempDataId); // / XXX remove me

    Session session = null;
    TempBanner data = null;

    try {
      session = HibernateUtil.getSessionFactory().openSession();
      data = (TempBanner) session.get(TempBanner.class, tempDataId);
    } finally {
      if (session != null && session.isOpen()) {
        session.flush();
        session.close();
      }
    }

    return data;
  }
 @Test
 @Transactional
 public void testSaveAndFind() throws Exception {
   Session session = sessionFactory.getCurrentSession();
   Order order = new Order();
   Item item = new Item();
   item.setProduct("foo");
   order.getItems().add(item);
   session.save(order);
   session.flush();
   // Otherwise the query returns the existing order (and we didn't set the
   // parent in the item)...
   session.clear();
   Order other =
       (Order)
           session
               .createQuery("select o from Order o join o.items i where i.product=:product")
               .setString("product", "foo")
               .uniqueResult();
   assertEquals(1, other.getItems().size());
   assertEquals(other, other.getItems().iterator().next().getOrder());
 }
  private void prepareTestData(Session s) {
    Employee john = new Employee("John Doe");
    john.setCompany("JBoss");
    john.setDepartment("hr");
    john.setTitle("hr guru");
    john.setRegion("US");

    Employee polli = new Employee("Polli Wog");
    polli.setCompany("JBoss");
    polli.setDepartment("hr");
    polli.setTitle("hr novice");
    polli.setRegion("US");
    polli.setManager(john);
    john.getMinions().add(polli);

    Employee suzie = new Employee("Suzie Q");
    suzie.setCompany("JBoss");
    suzie.setDepartment("hr");
    suzie.setTitle("hr novice");
    suzie.setRegion("EMEA");
    suzie.setManager(john);
    john.getMinions().add(suzie);

    Customer cust = new Customer("John Q Public");
    cust.setCompany("Acme");
    cust.setRegion("US");
    cust.setContactOwner(john);

    Person ups = new Person("UPS guy");
    ups.setCompany("UPS");
    ups.setRegion("US");

    s.save(john);
    s.save(cust);
    s.save(ups);

    s.flush();
  }
Esempio n. 19
0
  public void testCollectionCUD() throws HibernateException, SQLException {
    if (isUsingIdentity()) {
      reportSkip("hand sql expecting non-identity id gen", "Custom SQL");
      return;
    }

    Role role = new Role();

    role.setName("Jim Flanders");

    Intervention iv = new Medication();
    iv.setDescription("JF medical intervention");

    role.getInterventions().add(iv);

    List sx = new ArrayList();
    sx.add("somewhere");
    sx.add("somehow");
    sx.add("whatever");
    role.setBunchOfStrings(sx);

    Session s = openSession();

    s.save(role);
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();

    Role r = (Role) s.get(Role.class, new Long(role.getId()));
    assertNotSame(role, r);

    assertEquals(1, r.getInterventions().size());

    assertEquals(3, r.getBunchOfStrings().size());

    r.getBunchOfStrings().set(1, "replacement");
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();

    r = (Role) s.get(Role.class, new Long(role.getId()));
    assertNotSame(role, r);

    assertEquals(r.getBunchOfStrings().get(1), "replacement");
    assertEquals(3, r.getBunchOfStrings().size());

    r.getBunchOfStrings().set(1, "replacement");

    r.getBunchOfStrings().remove(1);
    s.flush();

    r.getBunchOfStrings().clear();
    s.flush();

    s.connection().commit();
    s.close();
  }
 private void detachFromSession(Object entity) {
   session.flush();
   session.evict(entity);
 }
  public void testBlobClob() throws Exception {

    Session s = openSession();
    Blobber b = new Blobber();
    b.setBlob(Hibernate.createBlob("foo/bar/baz".getBytes()));
    b.setClob(Hibernate.createClob("foo/bar/baz"));
    s.save(b);
    // s.refresh(b);
    // assertTrue( b.getClob() instanceof ClobImpl );
    s.flush();
    s.refresh(b);
    // b.getBlob().setBytes( 2, "abc".getBytes() );
    b.getClob().getSubString(2, 3);
    // b.getClob().setString(2, "abc");
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    b = (Blobber) s.load(Blobber.class, new Integer(b.getId()));
    Blobber b2 = new Blobber();
    s.save(b2);
    b2.setBlob(b.getBlob());
    b.setBlob(null);
    // assertTrue( b.getClob().getSubString(1, 3).equals("fab") );
    b.getClob().getSubString(1, 6);
    // b.getClob().setString(1, "qwerty");
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    b = (Blobber) s.load(Blobber.class, new Integer(b.getId()));
    b.setClob(Hibernate.createClob("xcvfxvc xcvbx cvbx cvbx cvbxcvbxcvbxcvb"));
    s.flush();
    s.connection().commit();
    s.close();

    s = openSession();
    b = (Blobber) s.load(Blobber.class, new Integer(b.getId()));
    assertTrue(b.getClob().getSubString(1, 7).equals("xcvfxvc"));
    // b.getClob().setString(5, "1234567890");
    s.flush();
    s.connection().commit();
    s.close();

    /*InputStream is = getClass().getClassLoader().getResourceAsStream("jdbc20.pdf");
    s = sessionsopenSession();
    b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
    System.out.println( is.available() );
    int size = is.available();
    b.setBlob( Hibernate.createBlob( is, is.available() ) );
    s.flush();
    s.connection().commit();
    ResultSet rs = s.connection().createStatement().executeQuery("select datalength(blob_) from blobber where id=" + b.getId() );
    rs.next();
    assertTrue( size==rs.getInt(1) );
    rs.close();
    s.close();

    s = sessionsopenSession();
    b = (Blobber) s.load( Blobber.class, new Integer( b.getId() ) );
    File f = new File("C:/foo.pdf");
    f.createNewFile();
    FileOutputStream fos = new FileOutputStream(f);
    Blob blob = b.getBlob();
    byte[] bytes = blob.getBytes( 1, (int) blob.length() );
    System.out.println( bytes.length );
    fos.write(bytes);
    fos.flush();
    fos.close();
    s.close();*/

  }