entitymanager

How to persist a new entity containing multiple identical instances of another unpersisted entity with spring-boot and JPA?

无人久伴 提交于 2019-12-21 02:48:05
问题 Overview: I'm building a spring-boot application which, in part, retrieves some entities from an external REST service and compares it to previous versions of the entity held locally in a database. I'm injecting EntityManager with @PersistenceContext , and using that to work with the database, as there are many entity types, and the type is initially unknown to the module. I could get a JpaRepository from a factory, but the number of different entity types is liable to grow, and I'd rather

What is exact purpose of flush in JPA

倾然丶 夕夏残阳落幕 提交于 2019-12-20 12:37:04
问题 Some confusing explanation: flush(); Flushing is the process of synchronizing the underlying persistent store with persistable state held in memory.it will update or insert into your tables in the running transaction, but it may not commit those changes. If the changes are anyways going to be persisted in the database only after the commit then why to flush in the middle of the code. And after running the flush if any changes are made to the managed object then will that throw an Exception or

why EntityManager is null?

江枫思渺然 提交于 2019-12-18 23:39:08
问题 In my web applicaton I use OpenJPA on Apache Tomcat (TomEE)/7.0.37 server. I use Netbeans to auto generate class ("Entity Class from database..." and "Session Beans From Entity Class..."). At SessionBean (for example UserFacade) i want to get EntityManager: @Stateless public class UserFacade extends AbstractFacade<User> { @PersistenceContext(unitName = "CollDocPU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } } but when i get it by above way I

why EntityManager is null?

帅比萌擦擦* 提交于 2019-12-18 23:39:08
问题 In my web applicaton I use OpenJPA on Apache Tomcat (TomEE)/7.0.37 server. I use Netbeans to auto generate class ("Entity Class from database..." and "Session Beans From Entity Class..."). At SessionBean (for example UserFacade) i want to get EntityManager: @Stateless public class UserFacade extends AbstractFacade<User> { @PersistenceContext(unitName = "CollDocPU") private EntityManager em; @Override protected EntityManager getEntityManager() { return em; } } but when i get it by above way I

@PersistenceContext EntityManager thread-safety in Spring and Java EE

纵然是瞬间 提交于 2019-12-18 15:33:46
问题 EntityManager is not thread-safe by definition. Servlets specs says that in non-distributed environment and without implementing SingleThreadModel , there is only one servlet instance per definition . Therefore, in Java EE when you inject an EntityManager through the @PersistenceContext into Servlet's field - it's not thread safe: public class MyServlet extends HttpServlet { // Not thread-safe, should be using EMF instead. @PersistenceContext private EntityManager em; } Is this correct to say

How to inject EntityManager in CDI (weld)?

南笙酒味 提交于 2019-12-18 13:08:21
问题 In my project , I use JSF+JPA+CDI+WildFly 8.2 in the persistence layer. I have a BasicDao , like this: public class BasicDao<M, K> { private org.jboss.logging.Logger logger = org.jboss.logging.Logger .getLogger("BasicDao"); @Inject @Primary protected EntityManager em; Class<M> mclass; public EntityManager getEm() { return em; } public void setEm(EntityManager em) { this.em = em; } @Transactional(value=TxType.NOT_SUPPORTED) public M find(K id){ return em.find(mclass, id); } @Transactional

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

不问归期 提交于 2019-12-17 22:34:32
问题 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. 回答1: Unfortunately, JPA doesn't provide a standard way to

How to set the timeout period on a JPA EntityManager query

我与影子孤独终老i 提交于 2019-12-17 19:50:16
问题 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">

Heroku/Play/BoneCp connection issues

给你一囗甜甜゛ 提交于 2019-12-17 19:49:51
问题 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

Why in JPA EntityManager queries throw NoResultException but find does not?

核能气质少年 提交于 2019-12-17 19:18:57
问题 Can somebody tell me the intrinsic reasons why in the JPA 1.0 EntityManager when retrieving an Object via find, you have to deal with null if not found, but when using the Query interface via createQuery getResultList throws a NoResultException when not found. Maybe i am missing something but I feel its very inconsistent for a Language, and actually I had to do a lot of redesing because of changing from a simple finder to a more fine grained query using the query interface. Thanks guys. 回答1: