Last active
December 13, 2015 23:28
-
-
Save mcgivrer/4991379 to your computer and use it in GitHub Desktop.
Here is a small piece of code to parse package and detect @entity annotated classes. Starting from a persistence.xml file wich describe a "application" persistence unit, Annotated classes would be added to the EntityManager created, based on persistence.xml configuration.
No more need to add <class /> tags to the persistence file. thanks to Refl…
This file contains hidden or 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
package fr.mcgivrer.applications.angulargames.test.dao; | |
import java.util.Map; | |
import java.util.Properties; | |
import javax.persistence.Entity; | |
import javax.persistence.EntityManager; | |
import javax.persistence.EntityManagerFactory; | |
import javax.persistence.Persistence; | |
import org.hibernate.ejb.Ejb3Configuration; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.BeforeClass; | |
import org.reflections.Reflections; | |
import org.reflections.scanners.ResourcesScanner; | |
import org.reflections.scanners.TypeAnnotationsScanner; | |
import org.reflections.util.ClasspathHelper; | |
import org.reflections.util.ConfigurationBuilder; | |
import fr.mcgivrer.applications.angulargames.models.Game; | |
/** | |
* @author FDELORME | |
* | |
*/ | |
@SuppressWarnings("deprecation") | |
public class GenericDaoTest { | |
protected static EntityManagerFactory emf; | |
protected static EntityManager em; | |
@BeforeClass | |
public static void init() { | |
try { | |
if (emf.equals(null)) { | |
emf = Persistence.createEntityManagerFactory("application"); | |
if (em.equals(null)) { | |
em = load(emf); | |
} | |
} | |
} catch (ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
} | |
private static EntityManager load(EntityManagerFactory emf) | |
throws ClassNotFoundException { | |
// Entity Detection configuration | |
ConfigurationBuilder reflectConfig = new ConfigurationBuilder() | |
.addUrls(ClasspathHelper.forPackage("fr.mcgivrer.applications")) | |
.addUrls(ClasspathHelper.forClass(Entity.class)) | |
.setScanners(new ResourcesScanner(), | |
new TypeAnnotationsScanner()); | |
// prepare scan for annotated entities | |
Map<String, Object> props = emf.getProperties(); | |
// Load jdbc driver class onto classpath. | |
Class.forName((String) (props.get("hibernate.connection.driver_class")) | |
.toString()); | |
Properties properties = new Properties(); | |
properties.putAll(props); | |
Ejb3Configuration jpaConfig = new Ejb3Configuration(); | |
// TODO: by default force the auto-commit mode to true. to be | |
// removed | |
// on next version. | |
properties.put("hibernate.connection.autocommit", "true"); | |
jpaConfig.addProperties(properties); | |
// Parse annotated entities and add these to the Hibernate | |
// configuration component | |
Reflections entitiesFilter = new Reflections(reflectConfig); | |
for (Class<?> entity : entitiesFilter | |
.getTypesAnnotatedWith(Entity.class)) { | |
jpaConfig.addAnnotatedClass(entity); | |
} | |
return jpaConfig.buildEntityManagerFactory().createEntityManager(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment