Beispiel #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();
  }
Beispiel #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();
  }
 private void cleanSchema() {
   try {
     Session currentSession = getHibernateTemplate().getSessionFactory().getCurrentSession();
     Connection connection = currentSession.connection();
     Statement statement = connection.createStatement();
     statement.addBatch(SQL);
     statement.executeBatch();
     statement.close();
   } catch (SQLException e) {
     LOG.warn("cleaning schema", e);
   }
 }
Beispiel #4
0
  protected void runTest() throws Throwable {
    final boolean stats =
        ((SessionFactoryImplementor) sessions).getStatistics().isStatisticsEnabled();
    try {
      if (stats) sessions.getStatistics().clear();

      super.runTest();

      if (stats) sessions.getStatistics().logSummary();

      if (session != null && session.isOpen()) {
        if (session.isConnected()) session.connection().rollback();
        session.close();
        session = null;
        fail("unclosed session");
      } else {
        session = null;
      }
    } catch (Throwable e) {
      try {
        if (session != null && session.isOpen()) {
          if (session.isConnected()) session.connection().rollback();
          session.close();
        }
      } catch (Exception ignore) {
      }
      try {
        if (dropAfterFailure() && sessions != null) {
          sessions.close();
          sessions = null;
        }
      } catch (Exception ignore) {
      }
      throw e;
    }
  }
Beispiel #5
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();
  }
  private void prepareTestData(Session session) throws HibernateException {
    try {
      IdentifierGenerator stockIdGen =
          ((SessionFactoryImplementor) session.getSessionFactory())
              .getIdentifierGenerator(Stock.class.getName());
      IdentifierGenerator valIdGen =
          ((SessionFactoryImplementor) session.getSessionFactory())
              .getIdentifierGenerator(Valuation.class.getName());

      stockId = (Long) stockIdGen.generate((SessionImpl) session, null);
      valId = (Long) valIdGen.generate((SessionImpl) session, null);

      Connection conn = session.connection();
      PreparedStatement ps = conn.prepareStatement("INSERT INTO STOCK VALUES (?,?,?)");
      ps.setLong(1, stockId.longValue());
      ps.setString(2, "JBOSS");
      ps.setNull(3, Types.BIGINT);
      ps.executeUpdate();
      ps.close();

      ps = conn.prepareStatement("INSERT INTO STOCK_VAL VALUES (?,?,?,?)");
      ps.setLong(1, valId.longValue());
      ps.setLong(2, stockId.longValue());
      ps.setDate(3, new Date(new java.util.Date().getTime()));
      ps.setDouble(4, 200.0);
      ps.executeUpdate();
      ps.close();

      ps = conn.prepareStatement("UPDATE STOCK SET CURR_VAL_ID = ? WHERE STOCK_ID = ?");
      ps.setLong(1, valId.longValue());
      ps.setLong(2, stockId.longValue());
      ps.executeUpdate();
      ps.close();
    } catch (SQLException e) {
      System.err.println("Error : " + e);
      e.printStackTrace();
      throw new HibernateException("Unable to generate test data", e);
    }
  }
  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();*/

  }
Beispiel #8
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();
  }