Tag: entitymanager

如何在Spring中的共享EntityManager上手动启动事务?

我有一个LocalContainerEntityManagerFactoryBean作为EntityManager实例。 要快速删除整个表的内容,我想运行以下代码: @Service public class DatabaseService { @Autowired private EntityManager em; @Transactional public void clear() { em.createNativeQuery(“TRUNCATE TABLE MyTable”).executeUpdate(); } } 结果: ERROR org.springframework.integration.handler.LoggingHandler: javax.persistence.TransactionRequiredException: Executing an update/delete query at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71) at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98) at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262) at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at […]

Spring + Hibernate + JPA

到目前为止,我有一个具有持久性的Spring应用程序。 但是现在我想使用Hibernate和JPA来完成我的所有数据库活动。 我想使用entitymanager来做这件事。 我一直在阅读有关此问题的许多文档和教程,我一直对是否需要persistence.xml文件感到困惑。 此外,我也对如何设置applicationContext.xml文件感到困惑。 有没有人知道一个好的网站,以便使用EntityManager学习Spring + Hibernate + JPA +?

获取EntityManager的不同方法

我在创建EntityManager时看到的常用习惯是这样的: public class BaseDao { private static final String PERSISTENCE_UNIT_NAME = “Employee”; EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); public EntityManager getEntityManager() { return factory.createEntityManager(); } } 然后就像这样使用: Employee emp = new Employee(); emp.setName(“Joe M”); getEntityManager().persist(emp); 问题是为什么不这样做: public class BaseDao{ private static final String PERSISTENCE_UNIT_NAME = “Employee”; EntityManagerFactory factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME); private EntityManager entityManager = null; public void […]

当我们需要多个EntityManager时?

我正在学习JPA并且有一个问题: 在哪些情况下,我们的应用程序中需要多个EntityManager ? 我所知道的两种情况如下: 当我们的应用程序是一个multithreading应用程序并且多个线程需要JPA事务时,因为EntityManager不是线程安全的,我们每个线程需要一个EntityManager 。 当任何线程需要多个并发事务时,我们在该线程中需要多个EntityManager ,因为EntityManager和EntityTransaction之间存在一对一的关系。 Q1。 当我们需要多个EntityManager时还有其他情况吗? Q2。 根据我的理解,每个Persitence单元应该只有一个EntityManagerFactory 。 我对么? 如果没有,那么当每个持久性单元需要多个EntityManagerFactory时,那些情况是什么?

在Java EE应用程序中处理多个EntityManager

我有大约10个EntityManagers的Java EE应用程序(EM的数量可能会增加)。 我的应用程序还包含许多无状态,有状态和消息驱动的bean。 我可能会将所有内容存储在一个单独的bean中,并使用其他bean访问它,而不是在每个bean中使用@PersistenceContext注入我的EM(以及两个方法来检测用于用户的EM)。 像那样,不用担心可维护性。 然而,将EM存储在一个单独的bean中是否是线程安全的? 瓶颈会出现吗? 另一个解决方案是创建一个抽象类,所有bean都将扩展它。 什么是更好的解决方案?

EntityManager无法使用persist将元素保存到数据库

我遇到了使用EntityManager将元素持久化到数据库的问题。 根据我发现的答案,我在DaoJpa中尝试了这4种方法来做这样的事情,但仍然失败了。 在这里,我附上了我尝试的四种方式: 控制器部分代码: @Transactional SmartProduct smartProduct = new SmartProduct(); smartProduct.setName(“Dove Soap”); smartProductDao.persist(smartProduct); 道道: @Transactional public void persist(SmartProduct smartProduct) { entityManager.persist(smartProduct); 不行! 2。 @Transactional public void persist(SmartProduct smartProduct) { entityManager.persist(smartProduct); entityManager.flush(); 我得到的例外:没有交易正在进行中 3。 @Transactional public void persist(SmartProduct smartProduct) { EntityTransaction emTransaction = entityManager.getTransaction(); emTransaction.begin(); entityManager.persist(smartProduct); emTransaction.commit(); entityManager.close(); 我得到的例外:不允许在共享的EntityManager上创建事务 – 使用Spring事务或EJB CMT 4。 @Transactional public void […]

Java / JPA | 使用指定的inheritance类型进行查询

我正在构建一个通用表“Sample”的查询,我有几个类型inheritance自此表“SampleOne”,“SampleTwo”。 我需要一个像这样的查询: select s from Sample where s.type = :type 其中type将是表的鉴别值。 是否有可能以任何方式(并避免创建特定于实体的查询,每个SampleOne,SampleTwo …等) 我将非常感谢本主题中的任何输入, 亲切的问候,P。

我们如何才能获得JPA EntityManager Flush工作

我的问题是为什么冲洗不起作用: 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”); } 完成方法后,我在db中看到“anotherName”,我也用em.find(Customer.class,1,Lock.None)检查它; 但仍然没有奏效 RGDS

如何坚持很多实体(JPA)

我需要处理一个CSV文件,并为每个记录(行)保留一个实体。 现在,我这样做: while ((line = reader.readNext()) != null) { Entity entity = createEntityObject(line); entityManager.save(entity); i++; } 其中save(Entity)方法基本上只是一个EntityManager.merge()调用。 CSV文件中大约有20,000个实体(行)。 这是一种有效的方法吗? 这似乎很慢。 使用EntityManager.persist()会更好吗? 这种解决方案有何缺陷? 编辑 这是一个漫长的过程(超过400s),我尝试了两种解决方案, persist和merge 。 两者都需要大约相同的时间才能完成(459s vs 443s)。 问题是如果逐个保存实体是最佳的。 据我所知,Hibernate(我的JPA提供程序)确实实现了一些缓存/刷新function,所以我不必担心这一点。

EntityManager刷新

我有使用JPA的Web应用程序。 这个实体管理器保留了大量的参与者,突然我从另一侧更新了数据库。 我使用MySQL ,我使用PhpMyAdmin并改变一些行。 如何告诉实体经理重新同步,例如忘记缓存中的所有entite? 我知道有refresh(Object)方法,但有没有可能怎么做refreshAll()或什么导致这个? 这肯定是昂贵的操作,但如果必须这样做。