entitymanager

Handle multiple EntityManager in Java EE application

时光毁灭记忆、已成空白 提交于 2019-11-29 10:55:27
I have Java EE application with about 10 EntityManagers (number of EMs will probably increase). My application also contains many stateless, statefull and message driven beans. Rather than inject in each bean my EMs with @PersistenceContext (and 2 methods to detect which EM to use for user), I probably store all of that inside a singleton bean and access it with others beans. Like that, no worries about maintainability. Nevertheless, is it thread-safe to store EMs inside one singleton bean? Can a bottleneck appear? Another solution is to create an abstract class and all beans will extend it.

How frequently should I create an EntityManager?

こ雲淡風輕ζ 提交于 2019-11-29 10:50:28
问题 I have an EntityManagerFactory for which I can create one (or multiple) EntityManager instances. I'm using a Servlet environment, and I've got one EntityManagerFactory wired up to the servlet (via the servlet context) which is shared for the lifetime of the servlet (and therefore, for all users). I can do one of the following: Create a single EntityManager for the lifetime of my servlet (e.g. shared between all users) Create one per user (so each user gets their own in the HttpSession) Create

EntityManager cannot use persist to save element to database

白昼怎懂夜的黑 提交于 2019-11-29 10:42:42
I met the problem of persisting element to database using EntityManager. Based on the answers I found, I tried those 4 ways in my DaoJpa to do such thing but still failed. Here I attached the four ways I tried: Code in Controller part: @Transactional SmartProduct smartProduct = new SmartProduct(); smartProduct.setName("Dove Soap"); smartProductDao.persist(smartProduct); 1. DaoJpa: @Transactional public void persist(SmartProduct smartProduct) { entityManager.persist(smartProduct); Doesn't work! 2. @Transactional public void persist(SmartProduct smartProduct) { entityManager.persist(smartProduct

Spring + Hibernate + JPA + multiple databases

泄露秘密 提交于 2019-11-29 08:10:49
I have a Spring + Hibernate + JPA app. The user, when logging in, can choose from a list of DB's to connect to (these are the requirements). All the DB's have the same schema, so the same entities and DAO's will be used. Right now I have one EntityManager (been working with one database for the moment) that is injected in the DAO like this: @PersistenceContext private EntityManager entityManager; Is there any way to have the DAO receive the entityManager automatically (managed by Spring) based on a parameter/property received from the service layer ? (The web layer sends a kind of context, and

how we can get JPA EntityManager Flush work

限于喜欢 提交于 2019-11-29 05:39:05
my question is why flush doesn't work : public void ejbService(){ Customer c = em.find(Customer.class,1); c.setName("newName"); em.flush(); //at this point when I query mysql table I can not see "newName" thread.sleep(10000); c.setName("anotherName"); } After finishing the method I see "anotherName" in the db also I check it with em.find(Customer.class,1,Lock.None); but still not work RGDS You're flushing, but you're not committing - or otherwise ending the transaction / session which is likely configured for auto-commit. Yes, after calling flush() , the DBMS is now aware of your data - but

How to persist a lot of entities (JPA)

ぐ巨炮叔叔 提交于 2019-11-29 03:21:41
I need to process a CSV file and for each record (line) persist an entity. Right now, I do it this way: while ((line = reader.readNext()) != null) { Entity entity = createEntityObject(line); entityManager.save(entity); i++; } where the save(Entity) method is basically just an EntityManager.merge() call. There are about 20,000 entities (lines) in the CSV file. Is this an effective way to do it? It seems to be quite slow. Would it be better to use EntityManager.persist() ? Is this solution flawed in any way? EDIT It's a lengthy process (over 400s) and I tried both solutions, with persist and

Hibernate EntityManager: remove referenced entity not working

£可爱£侵袭症+ 提交于 2019-11-29 02:31:57
I'm currenetly trying hard to delete an entity, that is involved in various relations (Only @ManyToOne ) - However, Hibernate does not delete anything - after calling em.remove everything remains unchanged. I have 5 entities ( E1 - E5 ), referencing the entity ( E6 ) in question like this: @Entity public class EX { @OneToMany(mappedBy = "eX", fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.REMOVE }) private Set<E6> e6s; } E6 itself has the reverse @ManyToOne Relations: public class E6{ @ManyToOne(optional = false) private E1 e1; @ManyToOne(optional = false) private E2 e2;

Is there a way to pass detached object to JPA persist? (detached entity passed to persist)

感情迁移 提交于 2019-11-29 01:35:23
问题 I have 2 entities : Account and AccountRole . public class Account { private AccountRole accountRole; @ManyToOne(cascade = CascadeType.PERSIST, fetch = FetchType.EAGER) public AccountRole getAccountRole() { return accountRole; } . public class AccountRole { private Collection<Account> accounts = new ArrayList<Account>(); @OneToMany(mappedBy = "accountRole", fetch = FetchType.EAGER) public Collection<Account> getAccounts() { return accounts; } Problem comes when I take the accountRole from

How to manually start a transaction on a shared EntityManager in Spring?

醉酒当歌 提交于 2019-11-28 23:02:29
问题 I have a LocalContainerEntityManagerFactoryBean as EntityManager instance. To quickly drop a full tables' content, I want to run the following code: @Service public class DatabaseService { @Autowired private EntityManager em; @Transactional public void clear() { em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate(); } } Result: ERROR org.springframework.integration.handler.LoggingHandler: javax.persistence.TransactionRequiredException: Executing an update/delete query at org

Entities Not Persisting - Spring + Hibernate + JPA

坚强是说给别人听的谎言 提交于 2019-11-28 21:57:36
I'm using Spring + Hibernate + JPA and I have a situation where I can't get my entities to persist to the database. I've set up a service class that is annotated with @Transactional. It uses a DAO that contains an injected EntityManager. When I call the function on the service object I see a bunch of selects for the reads the DAO is doing, but no updates/deletes as a result of merges and removes issued by my DAO. Surely there's something wrong with my setup, but I can't see it. persistence.xml <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001