Package org.hibernate

Examples of org.hibernate.StatelessSession


            final EntityMode entityMode = event.getPersister().guessEntityMode(event.getEntity());
            Object oldPropValue = null;
            Object newPropValue = null;

            // need to have a separate session for audit save
            final StatelessSession session = event.getPersister().getFactory().openStatelessSession();
            session.beginTransaction();

            // get the existing entity from session so that we can extract existing property values
            final Object existingEntity = session.get(event.getEntity().getClass(), entityId);
            final String actorId = getActorId(session, event);

            // cycle through property names, extract corresponding property values and insert new entry in audit trail
            for (final String propertyName : event.getPersister().getPropertyNames()) {
                if (!auditColumn.contains(propertyName)) {
                    newPropValue = event.getPersister().getPropertyValue(event.getEntity(), propertyName, entityMode);
                    oldPropValue = event.getPersister().getPropertyValue(existingEntity, propertyName, entityMode);

                    // because we are performing an insert we only need to be concerned will non-null values
                    if (newPropValue != null && !newPropValue.equals(oldPropValue)
                            && !(newPropValue instanceof Collection)) {

                        final AuditTrail auditTrail = new AuditTrail(entityId.toString(), entityName, OP_TYPE_UPDATE,
                                actorId, transTime).entityProperty(propertyName)
                                .entityPropOldValue(oldPropValue == null ? null : oldPropValue.toString())
                                .entityPropNewValue(newPropValue == null ? null : newPropValue.toString());
                        session.insert(auditTrail);
                    }
                }
            }

            session.getTransaction().commit();
        } catch (final HibernateException e) {
            LOGGER.error("Unable to process audit log for UPDATE operation", e);
        }
        return false;
    }
View Full Code Here


    }
    log.trace( "finished" );
  }

  private void inTransactionWrapper() {
    StatelessSession session = sessionFactory.openStatelessSession();
    try {
      Transaction transaction = session.beginTransaction();
      loadAllIdentifiers( session );
      transaction.commit();
    } catch (InterruptedException e) {
      // just quit
    }
    finally {
      session.close();
    }
  }
View Full Code Here

  }

  public void testCriteria() {
    TestData testData=new TestData();
    testData.createData();
    StatelessSession s = getSessions().openStatelessSession();
    assertEquals( 1, s.createCriteria( Contact.class ).list().size() );
    s.close();
    testData.cleanData();
  }
View Full Code Here

  }

  public void testCriteriaWithSelectFetchMode() {
    TestData testData=new TestData();
    testData.createData();
    StatelessSession s = getSessions().openStatelessSession();
    assertEquals( 1, s.createCriteria( Contact.class ).setFetchMode( "org", FetchMode.SELECT )
        .list().size() );
    s.close();
    testData.cleanData();
  }
View Full Code Here

  }

  public void testHQL() {
    TestData testData=new TestData();
    testData.createData();
    StatelessSession s = getSessions().openStatelessSession();
    assertEquals( 1, s.createQuery( "from Contact c join fetch c.org join fetch c.org.country" )
        .list().size() );
    s.close();
    testData.cleanData();
  }
View Full Code Here

  public static Test suite() {
    return new FunctionalTestClassTestSuite( StatelessSessionTest.class );
  }

  public void testCreateUpdateReadDelete() {
    StatelessSession ss = getSessions().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Document doc = new Document("blah blah blah", "Blahs");
    ss.insert(doc);
    assertNotNull( doc.getName() );
    Date initVersion = doc.getLastModified();
    assertNotNull( initVersion );
    tx.commit();
   
    tx = ss.beginTransaction();
    doc.setText("blah blah blah .... blah");
    ss.update(doc);
    assertNotNull( doc.getLastModified() );
    assertNotSame( doc.getLastModified(), initVersion );
    tx.commit();
   
    tx = ss.beginTransaction();
    doc.setText("blah blah blah .... blah blay");
    ss.update(doc);
    tx.commit();
   
    Document doc2 = (Document) ss.get(Document.class.getName(), "Blahs");
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
       
    doc2 = (Document) ss.createQuery("from Document where text is not null").uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
   
    ScrollableResults sr = ss.createQuery("from Document where text is not null")
      .scroll(ScrollMode.FORWARD_ONLY);
    sr.next();
    doc2 = (Document) sr.get(0);
    sr.close();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
       
    doc2 = (Document) ss.createSQLQuery("select * from Document")
      .addEntity(Document.class)
      .uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
       
    doc2 = (Document) ss.createCriteria(Document.class).uniqueResult();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());
   
    sr = ss.createCriteria(Document.class).scroll(ScrollMode.FORWARD_ONLY);
    sr.next();
    doc2 = (Document) sr.get(0);
    sr.close();
    assertEquals("Blahs", doc2.getName());
    assertEquals(doc.getText(), doc2.getText());

    tx = ss.beginTransaction();
    ss.delete(doc);
    tx.commit();
    ss.close();

  }
View Full Code Here

    ss.close();

  }

  public void testHqlBulk() {
    StatelessSession ss = getSessions().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Document doc = new Document("blah blah blah", "Blahs");
    ss.insert(doc);
    Paper paper = new Paper();
    paper.setColor( "White" );
    ss.insert(paper);
    tx.commit();

    tx = ss.beginTransaction();
    int count = ss.createQuery( "update Document set name = :newName where name = :oldName" )
        .setString( "newName", "Foos" )
        .setString( "oldName", "Blahs" )
        .executeUpdate();
    assertEquals( "hql-update on stateless session", 1, count );
    count = ss.createQuery( "update Paper set color = :newColor" )
        .setString( "newColor", "Goldenrod" )
        .executeUpdate();
    assertEquals( "hql-update on stateless session", 1, count );
    tx.commit();

    tx = ss.beginTransaction();
    count = ss.createQuery( "delete Document" ).executeUpdate();
    assertEquals( "hql-delete on stateless session", 1, count );
    count = ss.createQuery( "delete Paper" ).executeUpdate();
    assertEquals( "hql-delete on stateless session", 1, count );
    tx.commit();
    ss.close();
  }
View Full Code Here

    tx.commit();
    ss.close();
  }

  public void testInitId() {
    StatelessSession ss = getSessions().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Paper paper = new Paper();
    paper.setColor( "White" );
    ss.insert(paper);
    assertNotNull( paper.getId() );
    tx.commit();

    tx = ss.beginTransaction();
    ss.delete( ss.get( Paper.class, paper.getId() ) );
    tx.commit();
    ss.close();
  }
View Full Code Here

    tx.commit();
    ss.close();
  }

  public void testRefresh() {
    StatelessSession ss = getSessions().openStatelessSession();
    Transaction tx = ss.beginTransaction();
    Paper paper = new Paper();
    paper.setColor( "whtie" );
    ss.insert( paper );
    tx.commit();
    ss.close();

    ss = getSessions().openStatelessSession();
    tx = ss.beginTransaction();
    Paper p2 = ( Paper ) ss.get( Paper.class, paper.getId() );
    p2.setColor( "White" );
    ss.update( p2 );
    tx.commit();
    ss.close();

    ss = getSessions().openStatelessSession();
    tx = ss.beginTransaction();
    assertEquals( "whtie", paper.getColor() );
    ss.refresh( paper );
    assertEquals( "White", paper.getColor() );
    ss.delete( paper );
    tx.commit();
    ss.close();
  }
View Full Code Here

    s.save( yourClock );
    s.save( task );
    s.getTransaction().commit();
    s.close();

    StatelessSession ss = sfi().openStatelessSession();
    ss.beginTransaction();
    Task taskRef = ( Task ) ss.createQuery( "from Task t join fetch t.resource join fetch t.user" ).uniqueResult();
    assertTrue( taskRef != null );
    assertTrue( Hibernate.isInitialized( taskRef ) );
    assertTrue( Hibernate.isInitialized( taskRef.getUser() ) );
    assertTrue( Hibernate.isInitialized( taskRef.getResource() ) );
    assertFalse( Hibernate.isInitialized( taskRef.getResource().getOwner() ) );
    ss.getTransaction().commit();
    ss.close();

    cleanup();
  }
View Full Code Here

TOP

Related Classes of org.hibernate.StatelessSession

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.