Created
May 23, 2014 09:00
-
-
Save edmondop/fe63b8f4a144e2e9e1fb to your computer and use it in GitHub Desktop.
SkipSpaceClassesWithoutIdHibernateSpaceDataSource
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SkipSpaceClassesWithoutIdHibernateSpaceDataSource(@BeanProperty val sessionFactory:SessionFactory) extends SpaceDataSource { | |
protected final val logger: Logger = LoggerFactory.getLogger(getClass) | |
import scala.collection.JavaConverters._ | |
lazy val managedEntries = sessionFactory.getAllClassMetadata.asScala.keySet | |
lazy val initialLoadEntries = { | |
val allClassMetaData = sessionFactory.getAllClassMetadata.asScala | |
allClassMetaData flatMap { | |
case (entityName,classMetaData) => | |
if(classMetaData.isInherited){ | |
val superClassEntityName: String = classMetaData.asInstanceOf[AbstractEntityPersister].getMappedSuperclass | |
val superClassMetadata: ClassMetadata = allClassMetaData(superClassEntityName) | |
val superClass: Class[_] = superClassMetadata.getMappedClass(EntityMode.POJO) | |
if (superClass != null) { | |
logger.debug("Entity {} is inherited and has a super class {} filtering it out for initial load managedEntries",entityName,superClass) | |
None | |
} | |
else | |
Some(entityName) | |
} | |
else | |
Some(entityName) | |
} | |
} | |
override def initialDataLoad: DataIterator[AnyRef] = { | |
val session = sessionFactory.openSession() | |
val iterators = initialLoadEntries map { | |
entity => createIterator(session,entity) | |
} | |
val allIterators = iterators reduce { _ ++ _} | |
new DataIterator[AnyRef]{ | |
def hasNext: Boolean = allIterators.hasNext | |
def next(): AnyRef = HibernateIteratorUtils unproxy allIterators.next() | |
def remove() { | |
throw new UnsupportedOperationException("Can't remove from an hibernate iterator")} | |
def close() { | |
session.close() | |
} | |
} | |
} | |
def createIterator(session:Session,entityName:String):Iterator[AnyRef] = { | |
val klass = Class forName entityName | |
val spaceIdMethods = for { | |
method <- klass.getMethods.toList if method.isAnnotationPresent(classOf[SpaceId]) | |
} yield method | |
spaceIdMethods.length match{ | |
case 1 => { | |
val criteria: Criteria = session createCriteria entityName | |
criteria setCacheMode CacheMode.IGNORE | |
criteria setCacheable false | |
criteria setFlushMode FlushMode.MANUAL | |
criteria.list.iterator().asInstanceOf[java.util.Iterator[AnyRef]].asScala | |
} | |
case 0 => Iterator.empty | |
case _ => throw new IllegalStateException("A single SpaceId can be defined at maximum") | |
} | |
} | |
override def supportsInheritance(): Boolean = false | |
def shutdown() { | |
logger.debug("Shutting down hibernate data source with embeddable support session factory") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Elegant solution.