/**
* Initializes and returns singleton EntityManagerFactory.
*
* @return singleton EntityManagerFactory.
*/
public static EntityManagerFactory getEntityManagerFactory() {
if (EntityManagerFactoryProvider.entityManagerFactory == null
|| !EntityManagerFactoryProvider.entityManagerFactory.isOpen()) {
synchronized (EntityManagerFactoryProvider.class) {
if (EntityManagerFactoryProvider.entityManagerFactory == null
|| !EntityManagerFactoryProvider.entityManagerFactory.isOpen()) {
initEntityManagerFactory(DataSourceProvider.getInstance().getDataSource());
}
}
}
return EntityManagerFactoryProvider.entityManagerFactory;
}
java类javax.persistence.EntityManagerFactory的实例源码
EntityManagerFactoryProvider.java 文件源码
项目:cloud-ariba-partner-flow-extension-ext
阅读 28
收藏 0
点赞 0
评论 0
LiquibaseHelperTest.java 文件源码
项目:minijax
阅读 25
收藏 0
点赞 0
评论 0
@Test
public void testCloseEntityManagerFactoryException() throws Exception {
final EntityManagerFactory emf = mock(EntityManagerFactory.class);
doThrow(new RuntimeException("Boom")).when(emf).close();
LiquibaseHelper.closeQuietly(emf);
verify(emf).close();
}
JPAConnection.java 文件源码
项目:ThermalComfortBack
阅读 24
收藏 0
点赞 0
评论 0
public JPAConnection() {
if (entityManager == null) {
EntityManagerFactory emf;
if (DB.equals(DERBY)) {
emf = Persistence.createEntityManagerFactory(DERBY);
} else {
Map<String, String> propertyMap = new HashMap<>();
propertyMap.put(CassandraConstants.CQL_VERSION, CassandraConstants.CQL_VERSION_3_0);
emf = Persistence.createEntityManagerFactory(CASSANDRA, propertyMap);
}
entityManager = emf.createEntityManager();
}
}
UserRoleDAO.java 文件源码
项目:bibliometrics
阅读 32
收藏 0
点赞 0
评论 0
/**
* retrieves the list of <code>UserRole</code>-objects by its email.
*
* @param email
* the email
* @return userRoles the roles
*
*/
public static List<UserRole> getUserRolesByEmail(String username) {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("userData");
EntityManager em = emf.createEntityManager();
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<UserRole> q = cb.createQuery(UserRole.class);
Root<UserRole> c = q.from(UserRole.class);
q.select(c).where(cb.equal(c.get("username"), username));
TypedQuery<UserRole> query = em.createQuery(q);
List<UserRole> roles = query.getResultList();
em.close();
return roles;
}
PersistenceAnnotationBeanPostProcessor.java 文件源码
项目:lams
阅读 31
收藏 0
点赞 0
评论 0
private EntityManager resolveEntityManager(String requestingBeanName) {
// Obtain EntityManager reference from JNDI?
EntityManager em = getPersistenceContext(this.unitName, false);
if (em == null) {
// No pre-built EntityManager found -> build one based on factory.
// Obtain EntityManagerFactory from JNDI?
EntityManagerFactory emf = getPersistenceUnit(this.unitName);
if (emf == null) {
// Need to search for EntityManagerFactory beans.
emf = findEntityManagerFactory(this.unitName, requestingBeanName);
}
// Inject a shared transactional EntityManager proxy.
if (emf instanceof EntityManagerFactoryInfo &&
((EntityManagerFactoryInfo) emf).getEntityManagerInterface() != null) {
// Create EntityManager based on the info's vendor-specific type
// (which might be more specific than the field's type).
em = SharedEntityManagerCreator.createSharedEntityManager(
emf, this.properties, this.synchronizedWithTransaction);
}
else {
// Create EntityManager based on the field's type.
em = SharedEntityManagerCreator.createSharedEntityManager(
emf, this.properties, this.synchronizedWithTransaction, getResourceType());
}
}
return em;
}
TestPersistence.java 文件源码
项目:oscm
阅读 32
收藏 0
点赞 0
评论 0
private EntityManagerFactory buildEntityManagerFactory(ITestDB testDb,
String unitName) throws Exception {
Map<Object, Object> properties = new HashMap<Object, Object>();
properties.put(Environment.HBM2DDL_AUTO, "");
properties.put(Environment.DATASOURCE, createManagedDataSource(testDb
.getDataSource()));
properties.put("hibernate.search.autoregister_listeners", System.getProperty("hibernate.search.autoregister_listeners"));
properties.put("hibernate.transaction.jta.platform", "org.hibernate.service.jta.platform.internal.SunOneJtaPlatform");
properties.put("hibernate.id.new_generator_mappings", "false");
properties.put("org.hibernate.SQL", "false");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory(unitName, properties);
sf = ((EntityManagerFactoryImpl) entityManagerFactory).getSessionFactory();
return entityManagerFactory;
}
HibernateEntityManagerFactoryProvider.java 文件源码
项目:vertx-jpa
阅读 26
收藏 0
点赞 0
评论 0
@Override
public Future<EntityManagerFactory> getEntityManagerFactory(String datasourceName) {
Properties properties = createProperties();
PersistenceProvider provider = new HibernatePersistenceProvider();
SmartPersistanceUnitInfo persistenceUnitInfo = new DefaultPersistenceUnitInfoImpl(datasourceName);
persistenceUnitInfo.setProperties(properties);
// Using RESOURCE_LOCAL for manage transactions on DAO side.
persistenceUnitInfo.setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL);
Map<Object, Object> configuration = new HashMap<>();
properties.entrySet().stream().forEach(e -> configuration.put(e.getKey(), e.getValue()));
synchronized (vertx) {
Future<EntityManagerFactory> future = Future.future();
vertx.executeBlocking(f1 -> {
config.getJsonArray("annotated_classes", new JsonArray()).stream()
.forEach(p -> scanAnnotatedClasses(p.toString(), persistenceUnitInfo));
EntityManagerFactory emf = provider.createContainerEntityManagerFactory(persistenceUnitInfo, configuration);
future.complete(emf);
}, future.completer());
return future;
}
}
JPABookService.java 文件源码
项目:BecomeJavaHero
阅读 21
收藏 0
点赞 0
评论 0
@Override
public List<Book> getBooksOfAuthorWithId(int id) {
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pl.edu.bogdan.training.db.entity");
EntityManager em = entityManagerFactory.createEntityManager();
// begining of transaction
em.getTransaction().begin();
Author author = em.find(Author.class, id);
System.out.println(author.toString());
for (Book b : author.getBooks()) {
System.out.println(b.toString());
}
return author == null ? new ArrayList<Book>() : author.getBooks();
}
EntityManagerFactoryUtils.java 文件源码
项目:lams
阅读 38
收藏 0
点赞 0
评论 0
/**
* Prepare a transaction on the given EntityManager, if possible.
* @param em the EntityManager to prepare
* @param emf the EntityManagerFactory that the EntityManager has been created with
* @return an arbitrary object that holds transaction data, if any
* (to be passed into cleanupTransaction)
* @see JpaDialect#prepareTransaction
*/
private static Object prepareTransaction(EntityManager em, EntityManagerFactory emf) {
if (emf instanceof EntityManagerFactoryInfo) {
EntityManagerFactoryInfo emfInfo = (EntityManagerFactoryInfo) emf;
JpaDialect jpaDialect = emfInfo.getJpaDialect();
if (jpaDialect != null) {
return jpaDialect.prepareTransaction(em,
TransactionSynchronizationManager.isCurrentTransactionReadOnly(),
TransactionSynchronizationManager.getCurrentTransactionName());
}
}
return null;
}
CidadeTest.java 文件源码
项目:ProjetoFinalInitium
阅读 27
收藏 0
点赞 0
评论 0
@Test
public void selectSqlNativoTest() {
EntityManagerFactory factory = Persistence.createEntityManagerFactory("db1start");
EntityManager manager = factory.createEntityManager();
Query query = manager.createNativeQuery("Select * from cidade c");
List<Cidade> cidades = query.getResultList();
factory.close();
}