Java Code Examples for org.hibernate.StatelessSession#insert()

The following examples show how to use org.hibernate.StatelessSession#insert() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HibernateEventListener.java    From development with Apache License 2.0 6 votes vote down vote up
private void createHistory(EntityPersister persister, Object entity,
        ModificationType type) {
    if (entity instanceof DomainObject<?>) {
        DomainObject<?> obj = (DomainObject<?>) entity;
        if (obj.hasHistory()) {
            final DomainHistoryObject<?> hist = HistoryObjectFactory
                    .create(obj, type,
                            DataServiceBean.getCurrentHistoryUser());

            final StatelessSession session = persister.getFactory()
                    .openStatelessSession();
            Transaction tx = session.beginTransaction();
            session.insert(hist);
            tx.commit();
            session.close();

            if (logger.isDebugLoggingEnabled()) {
                logger.logDebug(String.format("%s %s[%s, v=%s]", type, obj
                        .getClass().getSimpleName(), Long.valueOf(obj
                        .getKey()), Long.valueOf(hist.getObjVersion())));
            }
        }
    }
}
 
Example 2
Source File: StatelessSessionTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 6 votes vote down vote up
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();
}
 
Example 3
Source File: DeletedObjectPostDeleteEventListener.java    From dhis2-core with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public void onPostDelete( PostDeleteEvent event )
{
    if ( IdentifiableObject.class.isInstance( event.getEntity() )
        && MetadataObject.class.isInstance( event.getEntity() )
        && !EmbeddedObject.class.isInstance( event.getEntity() ) )
    {
        IdentifiableObject identifiableObject = (IdentifiableObject) event.getEntity();
        DeletedObject deletedObject = new DeletedObject( identifiableObject );
        deletedObject.setDeletedBy( getUsername() );

        StatelessSession session = event.getPersister().getFactory().openStatelessSession();
        session.beginTransaction();

        try
        {
            session.insert( deletedObject );
            session.getTransaction().commit();
        }
        catch ( Exception ex )
        {
            log.error( "Failed to save DeletedObject: "+ deletedObject );
            session.getTransaction().rollback();
        }
        finally
        {
            session.close();
        }
    }
}
 
Example 4
Source File: StatelessSessionTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 5
Source File: StatelessSessionTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
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();
}
 
Example 6
Source File: StatelessSessionTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 4 votes vote down vote up
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();

}