Java Code Examples for org.hibernate.Query.scroll()

The following are Jave code examples for showing how to use scroll() of the org.hibernate.Query class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: Equella   File: AddLastActionDate.java   View Source Code Vote up 6 votes
@Override
protected void executeDataMigration(HibernateMigrationHelper helper, MigrationResult result, Session session)
{
	Query query = session
		.createQuery("select i.id, i.moderation.id from Item i where moderating = true or status = 'rejected'");
	ScrollableResults results = query.scroll();
	while( results.next() )
	{
		Query upQuery = session
			.createQuery("update ModerationStatus m set lastAction = "
				+ "(select max(h.date) from Item i join i.history h where i.id = ? and h.type in ('approved','rejected', 'comment') group by i.id) "
				+ "where m.id = ?");
		upQuery.setParameter(0, results.get(0));
		upQuery.setParameter(1, results.get(1));
		upQuery.executeUpdate();
	}
}
 
Example 2
Project: Equella   File: MigrateNotifications.java   View Source Code Vote up 5 votes
@Override
protected void executeDataMigration(HibernateMigrationHelper helper, MigrationResult result, Session session)
{
	Date convertDate = new Date();
	Query query = session.createSQLQuery("SELECT i.uuid, i.version, i.institution_id,"
		+ " un.element FROM item_users_notified un INNER JOIN item i ON i.id = un.item_id");
	ScrollableResults results = query.scroll();
	while( results.next() )
	{
		Object[] oldnote = results.get();

		ItemId itemId = new ItemId((String) oldnote[0], ((Number) oldnote[1]).intValue());

		Institution inst = new Institution();
		inst.setDatabaseId(((Number) oldnote[2]).longValue());

		FakeNotification notification = new FakeNotification();
		notification.reason = FakeNotification.REASON_WENTLIVE;
		notification.date = convertDate;
		notification.itemid = itemId.toString();
		notification.institution = inst;
		notification.userTo = (String) oldnote[3];

		session.save(notification);
		session.flush();
		session.clear();
	}
}
 
Example 3
Project: Equella   File: DMLPartitioner.java   View Source Code Vote up 4 votes
public final int withSession(Session session)
{
	final String idFinderHql = buildIdFinderHql();
	final String dmlHql = buildDmlHql();

	int total = 0;

	long startId = -1;
	long endId = -1;
	while( true )
	{
		// Find the next start and end ID
		startId = endId + 1;

		Query idFinder = session.createQuery(idFinderHql);
		idFinder.setMaxResults(MAX_BATCH_SIZE);
		idFinder.setParameter("startId", startId);
		setWhereParams(idFinder);
		ScrollableResults sr = idFinder.scroll();
		try
		{
			if( sr.last() )
			{
				endId = sr.getLong(0);
			}
			else
			{
				// Nothing more to process
				return total;
			}
		}
		finally
		{
			sr.close();
		}

		// Process rows in our ID range
		Query dml = session.createQuery(dmlHql);
		dml.setParameter("startId", startId);
		dml.setParameter("endId", endId);
		setWhereParams(dml);
		setDmlParams(dml);

		total += dml.executeUpdate();
	}
}
 
Example 4
Project: Equella   File: AuditLogDMLPartitioner.java   View Source Code Vote up 4 votes
private final int withSession(Session session) {
	log.debug("inside withSession() method");
	final String idFinderHql = buildIdFinderHql();
	final String dmlHql = buildDeleteAuditLogDmlHql();

	int total = 0;

	long startId = -1;
	long endId = -1;
	while( true )
	{
		// Find the next start and end ID
		startId = endId + 1;
		Query idFinder = session.createQuery(idFinderHql);
		idFinder.setMaxResults(MAX_BATCH_SIZE);
		idFinder.setParameter("startId", startId);		
		idFinder.setParameter("date", daysBeforeRemoval);
		idFinder.setParameter("institution", institution);
		ScrollableResults sr = idFinder.scroll();
		try
		{
			if( sr.last() )
			{				
				endId = sr.getLong(0);
			}
			else
			{
				// Nothing more to process
				log.debug("Total Audit Log Deleted Records :: "+total);
				return total;
			}
		}
		finally
		{
			sr.close();
		}
		
		Query dml = session.createQuery(dmlHql);
		dml.setParameter("startId", startId);
		dml.setParameter("endId", endId);
		dml.setParameter("date", daysBeforeRemoval);
		dml.setParameter("institution", institution);			
		total += dml.executeUpdate();
		log.debug("Start Id:: "+startId+" End Id:: "+endId+" Total Deleted Records ::"+total);
	}		
}