org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor.AbstractCollectionInitializor Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-envers Show documentation
Show all versions of hibernate-envers Show documentation
Hibernate's entity version (audit/history) support
/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
* See the lgpl.txt file in the root directory or .
*/
package org.hibernate.envers.internal.entities.mapper.relation.lazy.initializor;
import java.util.List;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.envers.boot.internal.EnversService;
import org.hibernate.envers.internal.entities.EntityInstantiator;
import org.hibernate.envers.internal.entities.mapper.relation.query.RelationQueryGenerator;
import org.hibernate.envers.internal.reader.AuditReaderImplementor;
/**
* Initializes a persistent collection.
*
* @author Adam Warski (adam at warski dot org)
*/
public abstract class AbstractCollectionInitializor implements Initializor {
private final AuditReaderImplementor versionsReader;
private final RelationQueryGenerator queryGenerator;
private final Object primaryKey;
protected final Number revision;
protected final boolean removed;
protected final EntityInstantiator entityInstantiator;
public AbstractCollectionInitializor(
EnversService enversService,
AuditReaderImplementor versionsReader,
RelationQueryGenerator queryGenerator,
Object primaryKey, Number revision, boolean removed) {
this.versionsReader = versionsReader;
this.queryGenerator = queryGenerator;
this.primaryKey = primaryKey;
this.revision = revision;
this.removed = removed;
entityInstantiator = new EntityInstantiator( enversService, versionsReader );
}
protected abstract T initializeCollection(int size);
protected abstract void addToCollection(T collection, Object collectionRow);
@Override
public T initialize() {
final SharedSessionContractImplementor session = versionsReader.getSessionImplementor();
final List collectionContent = queryGenerator.getQuery( session, primaryKey, revision, removed ).list();
final T collection = initializeCollection( collectionContent.size() );
for ( Object collectionRow : collectionContent ) {
addToCollection( collection, collectionRow );
}
return collection;
}
}