entitymanager

Transaction required exception on execute update for JPQL update query

六眼飞鱼酱① 提交于 2019-12-04 07:17:19
I get this error when I try to run this code. Error: javax.persistence.TransactionRequiredException: executeUpdate is not supported for a Query object obtained through non-transactional access of a container-managed transactional EntityManager Code: (_ut is a UserTransaction object) public void setMainCategory(Integer deptId, Integer catId) { try { Query setmain = _entityManager.createNamedQuery("Category.setAsMain"); Query removeMain = _entityManager.createNamedQuery("Category.removeMain"); setmain.setParameter("categoryId", catId); Department d; d=_entityManager.find(Department.class, deptId

How do I resolve “Unable to resolve attribute [organizationType.id] against path” exception?

…衆ロ難τιáo~ 提交于 2019-12-04 07:08:45
I'm using Spring 3.1.1.RELEASE, Hibernate 4.1.0.Final, JUnit 4.8, and JPA 2.0 (hibernate-jpa-2.0-api). I'm trying to write a query and search based on fields of member fields. What I mean is I have this entity … @GenericGenerator(name = "uuid-strategy", strategy = "uuid.hex") @Entity @Table(name = "cb_organization", uniqueConstraints = {@UniqueConstraint(columnNames={"organization_id"})}) public class Organization implements Serializable { @Id @NotNull @GeneratedValue(generator = "uuid-strategy") @Column(name = "id") /* the database id of the Organization */ private String id; @ManyToOne

Spring - JPA - Reads work but persist give me No transactional EntityManager available. Why?

可紊 提交于 2019-12-04 04:19:34
问题 I am building an application with spring and JPA. My read functions are working but when i try to save any objects, i get the exception No transactional EntityManager available . Here is my web.xml file: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- The definition of the

How to use entityManager inside Entity?

烂漫一生 提交于 2019-12-04 03:44:08
问题 I have this function in Entity class but the getDoctrine do not fond... public function getObject() { $em = $this->getDoctrine()->getEntityManager(); switch($this->objectType) { case 'video': return $em->getRepository('fdj2012AdminBundle:Video')->find($this->objectId); break; case 'default': return false; break; } } How to use entityManager inside my Entity ? 回答1: Actually Entity shouldn't know about EM. I use Event Listeners if I need advance logic in my Entity. When you register Listeners

When using JPA entityManager why do you have to merge before you remove?

旧巷老猫 提交于 2019-12-04 02:48:33
For a while now I have been wondering why when using JPA, do I have to write my delete methods like this: @Transactional public void delete(Account account) { if (entityManager.contains(account)) { entityManager.remove(account); } else { entityManager.remove(entityManager.merge(account)); } } Perhaps the contains isn't needed since the transaction begins and ends with this method, but I still wonder why the remove couldn't just take an unmanaged object. Is it because it needs to be managed in order to know what the id is for that object? Any other insights would be great to hear. I just want

Injecting Entitymanager via XML and not annnotations

我的未来我决定 提交于 2019-12-04 02:32:57
What I am trying to do is inject through XML almost the same way that is done through A @PersistenceContext annotation. I am in need of this because of the fact I have different entity managers I need to inject into the same DAO. The databases mirror one another and I would rather have 1 base class and for instances of that base class then create multiple classes just so I can use the @PersistenceContext annotation. Here is my example. This is what I am doing now and it works. public class ItemDaoImpl { protected EntityManager entityManager; public List<Item> getItems() { Query query =

Spring Data JPA: Repositories for multiple database / Entitymanger configurations

北慕城南 提交于 2019-12-03 12:13:08
问题 I have two Entitymanager bean configurations. Each pointing to a separate database with a different schema (one is Oracle, the other one is an in-memory H2) What could I do to solve the ambiguity of what Entitymanager should be used for each Repository? Right now I'm getting this error: No unique bean of type [javax.persistence.EntityManagerFactory] is defined: expected single bean but found 2 I guess I could provide a quick-fix simply by using something like <jpa:repositories base-package=

Application vs Container Managed EntityManager

青春壹個敷衍的年華 提交于 2019-12-03 09:58:20
I am currently having a problem with understanding a concept of JPA. I am currently using/developing recent EclipseLink, Glassfish, Derby database to demonstrate a project. Before I develop something in much bigger picture, I need to be absolutely sure of how this PersistingUnit work in terms of different scopes. I have bunch of servlets 3.0 and currently saving user's associated entity classes in the request.session object (everything in the same war file). I am currently using Application-managed EntityManager using EntityManagerFactory and UserTransaction injection. It works smooth when it

EJB: Using EntityManager in PostConstruct method

走远了吗. 提交于 2019-12-03 09:55:59
问题 After constructing the bean, I want to retrieve data from the database, using the EntityManager. It is not possible in the constructor, because the EntityManager is injected after the constructor is called. So I tried to do it in a method annotated with @PostConstruct. According to the API, a PostConstruct Methods gets called after all injection are done. Executing the query works, but it always returns an empty list. If I use the same query in an other method, it returns the correct result.

GWT RequestFactory: how to use single EntityManager per request

江枫思渺然 提交于 2019-12-03 08:53:52
问题 In order to get RequestFactory to persist attached entities, I need to ensure that I use the same EntityManager for each request. I cobbled together my own Factory class for this based on a ThreadLocal implementation, but I'm unsure how to properly release resources (e.g. how to know that the request has finished and call close() ). Is there a simple way to ensure that a single EntityManager is used throughout a given ServletRequest without resorting to full-on J2EE/CDI? I'll take that route