entitymanager

EntityManager refresh

孤街醉人 提交于 2019-11-27 11:35:11
问题 I have web application using JPA. This entity manager keeps bunch of entites and suddenly I update the database from other side. I use MySQL and I use PhpMyAdmin and change some row. How to tell entity manager to re-synchronize, e.g. to forgot all the entites in cache? I know there is refresh(Object) method, but is there any possibility how to do refreshAll() or something what results in this? It is sure this is expensive operation but if it has to be done. 回答1: entityManager

What is the difference between EntityManager.find() and EntityManger.getReference()?

China☆狼群 提交于 2019-11-27 10:31:55
Whats is the difference between <T> T EntityManager.find(Class<T> entityClass, Object primaryKey) and <T> T EntityManager.getReference(Class<T> entityClass, Object primaryKey) ? I think getReference returns entity if it is managed. and find returns entity if it is managed else executes SQL on database to make it managed. Please confirm. Context: From webapp I get primary key of object to be deleted (pk of type long); to entity should be managed to delete. EntityManager.remove(Object entity) to pass managed entity to entitymanager remove method 'whats the better and correct option? find or

@Autowired vs @PersistenceContext for EntityManager bean

人走茶凉 提交于 2019-11-27 10:26:36
问题 What is the difference between: @Autowired private EntityManager em; versus: @PersistenceContext private EntityManager em; Both options work in my application, but can I break something by using the @Autowired annotation? 回答1: @PersistenceContext allows you to specify which persistence unit you want to use. Your project might have multiple data sources connected to different DBs and @PersistenceContext allows you to say which one you want to operate on check the explanation here: http://www

The EntityManager is closed

荒凉一梦 提交于 2019-11-27 06:28:49
[Doctrine\ORM\ORMException] The EntityManager is closed. After I get a DBAL exception when inserting data, EntityManager closes and I'm not able to reconnect it. I tried like this but it didn't get a connection. $this->em->close(); $this->set('doctrine.orm.entity_manager', null); $this->set('doctrine.orm.default_entity_manager', null); $this->get('doctrine')->resetEntityManager(); $this->em = $this->get('doctrine')->getEntityManager(); Anyone an idea how to reconnect? This is a very tricky problem since, at least for Symfony 2.0 and Doctrine 2.1, it is not possible in any way to reopen the

java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

╄→尐↘猪︶ㄣ 提交于 2019-11-27 05:12:30
问题 I have a java EE project using JPA (transaction-type="JTA"), hibernate as provider. I write my beans to handle the CRUD things. The program running in JBOSS 7 AS. I have an EntityManagerDAO : @Stateful public class EntityManagerDao implements Serializable { @PersistenceContext(unitName = "dtdJpa") private EntityManager entityManager; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Object updateObject(Object object) { object = entityManager.merge(object); return object; }

Injecting EntityManager Vs. EntityManagerFactory

*爱你&永不变心* 提交于 2019-11-27 04:02:10
问题 A long question, please bear with me. We are using Spring+JPA for a web application. My team is debating over injecting EntityManagerFactory in the GenericDAO (a DAO based on Generics something on the lines provided by APPFUSE, we do not use JpaDaosupport for some reason) over injecting an EntityManager . We are using "application managed persistence". The arguments against injecting a EntityManagerFactory is that its too heavy and so is not required, the EntityManager does what we need. Also

static access to entity manager in spring and unusual architecture

天涯浪子 提交于 2019-11-27 02:59:28
问题 quick question: I have webapplication (wicket+spring+jpa) and was thinking about rather unusual architecture design. Please check it out and give your comments. Consider class Wrapper: @Service public class Wrapper { protected static EntityManager entityManager; @PersistenceContext private void injectEntityManager(EntityManager entityManager) { Wrapper.entityManager = entityManager; } as you see I have now EntityManager injected statically. Now consider simple entity DogEntity @Entity public

How to change Entity type in JPA?

雨燕双飞 提交于 2019-11-27 02:47:57
问题 In my specific case, I am making use of a discriminator column strategy. This means that my JPA implementation (Hibernate) creates a users table with a special DTYPE column. This column contains the class name of the entity. For example, my users table can have subclasses of TrialUser and PayingUser . These class names would be in the DTYPE column so that when the EntityManager loads the entity from the database, it knows which type of class to instantiate. I've tried two ways of converting

Does Guice Persist provide transaction scoped or application managed EntityManager?

限于喜欢 提交于 2019-11-27 02:38:52
问题 We use Guice Persist to inject EntityManager in our project. E.g. public class MyDao{ @Inject EntityManager em; public void someMethod(){ //uses em instance } } But it is unclear for us how injected instance of EntityManager is about to be used. What type of EntityManager is this? (see e.g.: types of entity managers) Under the hood Guice Persist instantiates it via EntityManagerFactory.createEntityManager() so I'd say it's application-managed entity manager. But in official Wiki they write

When should EntityManagerFactory instance be created/opened?

Deadly 提交于 2019-11-27 00:58:12
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 = "KogaAlphaPU"; public static final boolean DEBUG = true; private static final EmProvider singleton = new