entitymanager

Different ways of getting the EntityManager

十年热恋 提交于 2019-11-28 19:43:28
问题 The usual idiom I see for creating the EntityManager is something like this: public class BaseDao { private static final String PERSISTENCE_UNIT_NAME = "Employee"; EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); public EntityManager getEntityManager() { return factory.createEntityManager(); } } Then it is used like this: Employee emp = new Employee(); emp.setName("Joe M"); getEntityManager().persist(emp); Question is why not do it this way: public

What is the difference between LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean?

巧了我就是萌 提交于 2019-11-28 19:40:38
问题 Can anybody explain what is the difference between the Spring Framework's LocalContainerEntityManagerFactoryBean and LocalEntityManagerFactoryBean ? 回答1: Basically JPA specification defines two types of entity managers . They are : i) Application-Managed : Application Managed entity manager means "Entity Managers are created and managed by merely the application ( i.e. our code )" . ii) Container Managed : Container Managed entity manager means "Entity Managers are created and managed by

In Doctrine 2 can the Fetch Mode (Eager/Lazy etc.) be changed at runtime?

旧巷老猫 提交于 2019-11-28 19:23:00
I have entities which I would like to eagerly load , and on other ocassions lazy (or even extra lazy) load. My mappings have no fetch mode declared in my YAML- so they use the default (lazy loading). Currently the only way to eagerly load is to by constructing the DQL manually - and I need to update this every time I add a new entity. Ideally I would just load the root entity and the force eager loading all the associated objects. Is there any way I can do this? If not why (is there a reason beyond it being an unimplemented feature)? If you want to use built-in repository methods (find(),

EntityManager refresh

泄露秘密 提交于 2019-11-28 18:40:35
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. Rasmus Franke entityManager.getEntityManagerFactory().getCache().evictAll() Refresh is something different since it modifies your object .

When we need more than one EntityManager?

孤街醉人 提交于 2019-11-28 18:08:04
问题 I am learning JPA and have one question: In which situations we need more than one EntityManager in our application? The two situations that I am aware of are as follows: When our application is a multi-threaded application and more than one thread needs JPA transaction because EntityManager is not thread-safe and we need one EntityManager per thread. When any of the thread needs multiple concurrent transactions, we need more than one EntityManager in that thread because there is one-to-one

JPA, Entity manager, select many columns and get result list custom objects

大憨熊 提交于 2019-11-28 17:58:21
How I can get list of custom objects, like results below query: SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id By example: return getEntityManager().createQuery("SELECT p.category.id, count(p.id) FROM Product p left join p.category c WHERE p.seller.id=:id GROUP BY c.id").setParameter("id", id).getResultList(); I need a map with category id and number of products in category. Unfortunately, JPA doesn't provide a standard way to retrieve the results in a Map . However, building up your map manually by walking through the result list is

Heroku/Play/BoneCp connection issues

蹲街弑〆低调 提交于 2019-11-28 11:25:37
I have an app on heroku that uses play. It was working fine for the longest time, but somewhat recently I started getting this: Caused by: java.sql.SQLException: Timed out waiting for a free available connection. at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:169) ~[hibernate-core-4.1.9.Final.jar:4.1.9.Final] at com.jolbox.bonecp.BoneCP.getConnection(BoneCP.java:503) ~[bonecp-0.7.1.RELEASE.jar:0.7.1.RELEASE] which is caused by org.postgresql.util.PSQLException: FATAL: too many connections for role "ejmatdbwywaugk" Now this is pretty

How to set the timeout period on a JPA EntityManager query

随声附和 提交于 2019-11-28 11:24:18
I'm currently getting connection timeout errors from my EntityManager queries. Is it possible to set a timeout for these? persistence.xml <?xml version="1.0" encoding="UTF-8"?> <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"> <persistence-unit name="CallPU" transaction-type="RESOURCE_LOCAL"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <class>call.structure.Task</class>

Java /JPA | Query with specified inherited type

ⅰ亾dé卋堺 提交于 2019-11-28 11:04:05
I am building a query on a generic table "Sample" and I have several types which inherit from this table "SampleOne", "SampleTwo". I require a query like : select s from Sample where s.type = :type where type would be a discriminator value of the table. Is it possible in any way ( and avoid to create an entity specific queries, one for each SampleOne, SampleTwo... etc ) I would greatly appreciate any input in this topic, Kind regards, P. In JPA 2.0 you can use TYPE expression (though currently it doesn't work with parameters in Hibernate, see HHH-5282 ): select s from Sample s where TYPE(s) =

How to change Entity type in JPA?

余生颓废 提交于 2019-11-28 09:16:24
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 Entity types and both feel like dirty hacks: Use a native query to manually do an UPDATE on the column,