entitymanager

Exception not cuaght with Entity Manager

て烟熏妆下的殇ゞ 提交于 2019-12-31 06:38:31
问题 I have an Entity Manager in my EJB @PersistenceContext(unitName = "cnsbEntities") private EntityManager em; I populate an object and then I commit it in my DB, but if I have an exception, for duplicate ID, I can't catch it and I don't know why. try{ em.merge(boelLog); } catch (Exception e){ System.out.println("Generic Exception"); } 回答1: JPA uses transactions to send entity modifications to the database. You can specify those transactions manually through Bean Managed Transactions (BMT) or

entityManager.getTransaction().rollback() detaches entities?

谁说胖子不能爱 提交于 2019-12-31 02:06:30
问题 I have the following piece of code: EntityManagerFactory emf = Persistence.createEntityManagerFactory("test") EntityManager entityManager = emf.createEntityManager() User user = entityManager.find(User.class, 0); entityManager.getTransaction().begin(); entityManager.getTransaction().rollback(); entityManager.refresh(user); This throws an IllegalArgumentException on the fourth line saying "Entity not managed". If I change the third line to .commit() instead of .rollback() , everything seems to

Hibernate EntityManager, is it supposed to be used as a singleton?

这一生的挚爱 提交于 2019-12-30 04:32:08
问题 I am not using Spring so I am creating an instance of EntityManager within a class. I used Hibernate-Eclipse reverse engineering to auto-generate the classes. These classes all has an instance of EntityManager. I am not 100% sure how Hibernate works with the EntityManager so I am wondering if it is okay that so many instances of this class (EntityManager) are made, for example, will there be problems with transactions? Should I just make a separate class that distributes a static instance of

How should EntityManager be used in a nicely decoupled service layer and data access layer?

喜你入骨 提交于 2019-12-28 05:54:44
问题 Somewhat related to my other question Should raw Hibernate annotated POJO's be returned from the Data Access Layer, or Interfaces instead? , I am experienced in creation of nicely decoupled layers, but not using Hibernate or J2EE/JPA. I have been looking at documentation and tutorials, and am puzzled about how to use the EntityManger in an elegant way, as it seems it is responsible for both transactions (which I want to do at my service layer) and persistance methods (which I want to keep in

Warning: spl_object_hash() expects parameter 1 to be object

自古美人都是妖i 提交于 2019-12-25 17:19:07
问题 Warning: spl_object_hash() expects parameter 1 to be object, string given in /var/www/sitetwo/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php line 1367 I created MainBlogBundle with Category.php and Product.php using yml mapping annotation via php app/console commands. After using CRUD actions for add/edit/delete/show actions I tried to add a category and after submitting Add category form I get the Warning: spl_object_hash() expects parameter 1 to be object, string given in /var/www

NullPointerException thrown from EntityManager

ε祈祈猫儿з 提交于 2019-12-25 13:59:35
问题 I have a problem with an EntityManager , it throws a NullPointerException . This is the code: PersonServiceJpa.java package com.smartpump.service.jpa; import org.springframework.stereotype.Service; import com.smartpump.persistent.entity.Person; import com.smartpump.service.PersonService; import java.util.List; import javax.jdo.annotations.Transactional; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.Query; @Service("personService"

Read Data from JPA the ways to do it

怎甘沉沦 提交于 2019-12-25 07:37:56
问题 I read in the documentation and i see that to read the jpa data i need to use the following code. this is the only way to read the data from the JPA tables via sql? factory = Persistence.createEntityManagerFactory("abc"); EntityManager entityManager = factory.createEntityManager(); Query query = entityManager.createQuery("SELECT p FROM " + className + " p"); 回答1: There are several ways to read data using JPA. The example you provided is using JPQL. Additionally, you can: execute a native SQL

EclipseLink not populating Lazy OneToOne with nested Lazy OneToMany Relation

耗尽温柔 提交于 2019-12-25 06:25:36
问题 I migrated from Hibernate to EclipseLink because we needed composite primary keys which EclipseLink handles well and Hibernate doesn`t (really does not!). Now I am fixing our JUnit tests, I get issues with tons of OneToMany relations not loaded. I have the following classes: DatabaseSession.java package platform.data; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.function.BiConsumer; import java.util.stream.Collectors; import javax.persistence

Why does 'EntityManager.contains(..)' return false even if an entity is contained in DB?

戏子无情 提交于 2019-12-24 19:00:11
问题 I used this JPA: check whether an entity object has been persisted or not to know if i persist or merge my entity , It will look like this : if (!getEntityManager().contains(entity)) { System.out.println(" PERSIST "); } else { System.out.println(" MERGE "); } The case is that - even if I edit my entity - it will not recognized as a merge. How is it possible and how to make it work? 回答1: According to the JPA 2.1 specification (PDF page 72), the EntityManager method public boolean contains

EntityManager is always NULL using SpringFramework

孤者浪人 提交于 2019-12-24 18:09:23
问题 Good day, I've been currently working on a Maven + Spring + Hibernate project. Actually, this is just a test project just to get familiar on how Spring works with Hibernate (+Maven). I've already setup and prepare the necessary dependencies. i.e. the appcontext.xml for Spring, the persistence.xml for Hibernate, the entity and DAO objects for JPA/Persistence/Hibernate. During debug, it's observed that the EntityManager is always null . I don't know what's causing this because I've done the ff: