entitymanager

When should EntityManagerFactory instance be created/opened?

白昼怎懂夜的黑 提交于 2019-11-26 09:30:30
问题 Ok, I read bunch of articles/examples how to write Entity Manager Factory in singleton. One of them easiest for me to understand a bit: http://javanotepad.blogspot.com/2007/05/jpa-entitymanagerfactory-in-web.html I learned that EntityManagerFactory (EMF) should only be created once preferably in application scope. And also make sure to close the EMF once it\'s used (?) So I wrote EMF helper class for business methods to use: public class EmProvider { private static final String DB_PU = \

No Persistence provider for EntityManager named X

半世苍凉 提交于 2019-11-26 07:48:00
问题 I am developing a JavaSE application using JPA. Unfortunately, I get null after calling: Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); Below you will find: A snippet of my code that invokes EntityManagerFactory and unexpectedly returns null My persistence.xml file My project structure Snippet of my code: public class Main { private static final String PERSISTENCE_UNIT_NAME = \"MeineJpaPU\"; private static EntityManagerFactory factory; public static void main(String[] args) {

Struggling to understand EntityManager proper use

吃可爱长大的小学妹 提交于 2019-11-26 06:46:26
问题 I\'m starting a new project and I\'m totally new to JPA/Hibernate use. I\'m trying to understand how to use EntityManager properly. More precisely, when to instantiate them, how many do I need, should I close them, should I put everything into transactions? Anyway, in my current code, I got an org.hibernate.LazyInitializationException while trying to read an entity that I previously saved. I would understand the opposite (reading an antity in a transaction then trying to save the read entity

Using EntityManager inside Doctrine 2.0 entities

≯℡__Kan透↙ 提交于 2019-11-26 06:33:52
问题 I have 2 entities: Country (id, name) and Mapping (id, object, internalId, externalId). Country and Mapping are not connected with associations (because Mapping has rows not only for country). I need to get external id for country using following conditions: country.id = mapping.internalId mapping.object = \'country\' So I plan to add function getExternalId() in Country function getExternalId() { $em = Registry::getEntityManager(); $mapping = $em->getRepository(\'Mapping\')->findOneBy(array(

JPA EntityManager: Why use persist() over merge()?

ε祈祈猫儿з 提交于 2019-11-25 22:06:44
问题 EntityManager.merge() can insert new objects and update existing ones. Why would one want to use persist() (which can only create new objects)? 回答1: Either way will add an entity to a PersistenceContext, the difference is in what you do with the entity afterwards. Persist takes an entity instance, adds it to the context and makes that instance managed (ie future updates to the entity will be tracked). Merge creates a new instance of your entity, copies the state from the supplied entity, and