entitymanager

EntityManager exception handling in session bean

烈酒焚心 提交于 2019-12-09 06:45:55
问题 I have a managed stateless session bean with injected EntityManager em. What I am trying to do is to have a database table with unique column. Then I run some algorithm which is trying to insert an entity. If entity exists however it will update it or skip it. I would like to have something like this: try { em.persist(cd); em.flush(); } catch (PersistenceException e) { // Check if the exception is DatabaseException and ConstraintViolation // Update instead or skip it } Problem is that I am

Insert and update with CriteriaBuilder JPA in the same transactional method thows error “Foreign Key not exist”

大城市里の小女人 提交于 2019-12-08 14:52:31
问题 I am trying to make an insertion and with the resulting entity update N records in the same transaction. The error "FK does not exist" occurs and observing the log trace of the query is executed in the reverse order to which the instructions were triggered. Service: @Transactional public Entity1 createEntity(Entity1 newEntity){ Entity1 inserted = dao.createEntity(newEntity); Integer numUpdated = dao.updateEntity2(newEntity) return inserted; } Dao: public Entity1 createEntity(Entity1 newEntity

java.lang.ClassCastException: sametype cannot be cast to sametype

≡放荡痞女 提交于 2019-12-08 12:18:59
问题 very strange exception my code Query q = em.createQuery("SELECT u FROM Chatuser u"); List<Chatuser> list = (List<Chatuser>) q.getResultList(); for (int i = 0; i < list.size(); i++) { Object aa = list.get(i); Chatuser chatuser = (Chatuser)aa; exception ex = (java.lang.ClassCastException) java.lang.ClassCastException: Entities.service.Chatuser cannot be cast to Entities.service.Chatuser what is the problem ? 回答1: This post probably answers also your question Java, getting class cast exception

Spring + Eclipselink + JtaTransactionManager = javax.persistence.TransactionRequiredException

核能气质少年 提交于 2019-12-08 09:10:36
问题 I really hope you can help me. I've been looking all over the internet for answers and none of them works. I use Spring 3 + JTA + EclipseLink but i'm getting a TransactionRequiredException when flushing a transaction. Now i'm very used to just defining my persistence context and injecting the EntityManager and the transaction is handled by the app server. So here's what I've got. persistence.xml <persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3

TestNG: Identifying which tests methods are next

喜夏-厌秋 提交于 2019-12-07 21:31:22
问题 My goal is to clear() my javax.persistence.EntityManager after each test method. Here's an example of a test class: public class Example { @Test(dataProvider = "sampleDataProvider") public void testA(String parameter) { System.out.println(parameter); } @Test(dataProvider = "sampleDataProvider") public void testB(String parameter) { System.out.println(parameter); } } The entityManager is used in the dataProvider "sampleDataProvider" by querying the DB for test data which is then compiled in

JTA EntityManager cannot use getTransaction() [Spring + Hibernate + EntityManager]

*爱你&永不变心* 提交于 2019-12-07 21:18:48
问题 I am using Spring + JPA + Hibernate + EntityManager to talk to the database. I am getting 'A JTA EntityManager cannot use getTransaction() ' error. Please provide your insights and help me resolve the issue. beans.xml <?xml version="1.0" encoding="UTF-8"?> <beans default-autowire="byName" ... xmlns definitions... xsi:schemaLocation="..."> <context:component-scan base-package="com.mycompany.myproject" /> <bean id="entityManagerFactory" class="org.springframework.orm.jpa

AbstractMethodError when creating typed query with Hibernate 3.6.3 and JPA 2.0

自闭症网瘾萝莉.ら 提交于 2019-12-07 13:13:56
问题 I'm using Hibernate and JPA for a small project. Somehow when trying to obtain an typed Query, the java.lang.AbstractMethodError: org.hibernate.ejb.EntityManagerImpl.createQuery(Ljava/lang/String;Ljava/lang/Class;)Ljavax/persistence/TypedQuery is thrown; org.hibernate.ejb.EntityManagerImpl is from hibernate-entitymanager-3.3.2.GA.jar . This is not okay throwing the above exception: public Account read(Account entity) { EntityManager em = ManagedEntityManagerFactory.getEntityManager(); String

Is there a stateless version of the JPA EntityManager?

China☆狼群 提交于 2019-12-07 12:19:27
问题 Hibernate has a Stateless Version of its Session: Does something similar exist for the JPA EntityManager? I.e. an EntityManager that does not use the first level cache? 回答1: Not part of the JPA API or spec. Individual implementations may allow disabling the L1 cache. DataNucleus JPA, the one I have used, does allow this 回答2: From JPA point of view: javax.persistence.EntityManager stands for 1st level cache (persistence context, transactional cache) javax.persistence.EntityManagerFactory

Spring + Hibernate + JPA: How to reload EntityManagerFactory at runtime

戏子无情 提交于 2019-12-07 09:13:49
问题 I have been searching for this the last few hours maybe some of you can help me. I try to achieve a reload of my mapping info in EntityManagerFactory (or SessionFactory) at runtime in spring The EntityManagerFactory is defined as follows: <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceXmlLocation" value="persistence.xml" /> <property name="persistenceUnitName" value="JPAService" /> <property name=

Can you access EntityManagers from EntityListeners?

↘锁芯ラ 提交于 2019-12-07 07:57:49
问题 I'm aware that JSR-000220 Enterprise JavaBeans 3.0 Final Release (persistence) spec states: "In general, portable applications should not invoke EntityManager or Query operations, access other entity instances, or modify relationships in a lifecycle callback method." This appears extremely restrictive. We have a situation in which we would like to access the EntityManager from within an EntityListener. Has anyone come across any adverse effects/pitfulls when using the EntityManager from