entitymanager

Fixing Null EntityManger in Spring MVC application?

孤街醉人 提交于 2019-11-28 07:48:59
In the following code I am trouble with my injected EnitityManager, which always shows up as null ; public class GenericController extends AbstractController { @PersistenceContext(unitName = "GenericPU") private EntityManager em; protected ModelAndView handleRequestInternal( HttpServletRequest request, HttpServletResponse response) throws Exception { //(em == null) is always trigged if (em == null) throw new NullPointerException("em is null"); Collection<Generic> Generics = em.createNamedQuery("Generic.findAll").getResultList(); ModelAndView mav = new ModelAndView("Generic"); mav.addObject

What is the easiest way to have CDI and JPA in Java SE?

六眼飞鱼酱① 提交于 2019-11-28 07:46:36
I would like to have in Java SE @Stateless public class CarDAO { @Inject private EntityManager em; public Car findById(Long id) { return em.find(Car.class, id); } } @Singleton public class Application { @Inject private CarDAO carDAO; public void run() { Car car = carDAO.findById(44); System.out.println(car); } } public class EntryPoint { public static void main(String[] args) { Application application = // missing code application.run(); } } What I have to do to achieve that? I'm using postgres database, and maven in my project. I was already reading something about Weld (but it looks only

Handle multiple EntityManager in Java EE application

五迷三道 提交于 2019-11-28 04:15:42
问题 I have Java EE application with about 10 EntityManagers (number of EMs will probably increase). My application also contains many stateless, statefull and message driven beans. Rather than inject in each bean my EMs with @PersistenceContext (and 2 methods to detect which EM to use for user), I probably store all of that inside a singleton bean and access it with others beans. Like that, no worries about maintainability. Nevertheless, is it thread-safe to store EMs inside one singleton bean?

Getting a reference to EntityManager in Java EE applications using CDI

天涯浪子 提交于 2019-11-28 03:59:51
I'm using Java EE 7. I would like to know what is the proper way to inject a JPA EntityManager into an application scoped CDI bean. You can't just inject it using @PersistanceContext annotation, because EntityManager instances are not thread safe. Let's assume that we want our EntityManager to be created at the beginnig of every HTTP request processing and closed after the HTTP request is processed. Two options come into my mind: 1. Create a request scoped CDI bean which has a reference to an EntityManager and then inject the bean into an application scoped CDI bean. import javax.enterprise

EntityManager cannot use persist to save element to database

我只是一个虾纸丫 提交于 2019-11-28 03:59:09
问题 I met the problem of persisting element to database using EntityManager. Based on the answers I found, I tried those 4 ways in my DaoJpa to do such thing but still failed. Here I attached the four ways I tried: Code in Controller part: @Transactional SmartProduct smartProduct = new SmartProduct(); smartProduct.setName("Dove Soap"); smartProductDao.persist(smartProduct); 1. DaoJpa: @Transactional public void persist(SmartProduct smartProduct) { entityManager.persist(smartProduct); Doesn't work

JAVA: an EntityManager object in a multithread environment

徘徊边缘 提交于 2019-11-28 03:51:22
if I have multiple threads, each use injector to get the EntityManager object, each use the em object to select a list of other class objects. Ready to be used in a for loop. If a thread finishes first and calls clear(), will that affect the other threads? Like the for loop will have exception? How about close()? If the answer is "It depends", what (class definition? method call?) and where (java code? annotation? xml?) should I look at to find out how is it depended? I did not write the source, I am just using someone else's library without documentation. Thank you. Here is full working

java.lang.IllegalArgumentException: Removing a detached instance com.test.User#5

て烟熏妆下的殇ゞ 提交于 2019-11-28 03:47:56
I have a java EE project using JPA (transaction-type="JTA"), hibernate as provider. I write my beans to handle the CRUD things. The program running in JBOSS 7 AS. I have an EntityManagerDAO : @Stateful public class EntityManagerDao implements Serializable { @PersistenceContext(unitName = "dtdJpa") private EntityManager entityManager; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Object updateObject(Object object) { object = entityManager.merge(object); return object; } @TransactionAttribute(TransactionAttributeType.REQUIRED) public void createObject(Object object) {

Spring + Hibernate + JPA + multiple databases

萝らか妹 提交于 2019-11-28 01:42:23
问题 I have a Spring + Hibernate + JPA app. The user, when logging in, can choose from a list of DB's to connect to (these are the requirements). All the DB's have the same schema, so the same entities and DAO's will be used. Right now I have one EntityManager (been working with one database for the moment) that is injected in the DAO like this: @PersistenceContext private EntityManager entityManager; Is there any way to have the DAO receive the entityManager automatically (managed by Spring)

EntityManager does not write to database

喜你入骨 提交于 2019-11-27 23:18:33
i just set up a so far still quite minimal project maven/jpa/hibernate project, where i am trying to persist an object. My class is a quite simple one: @Entity public class Person { @Id @GeneratedValue private int id; private String name; } My persistence.xml is very basic as well: <persistence 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" version="2.0"> <persistence-unit name="Fahrplan_v2"> <class>model.Person</class>

how we can get JPA EntityManager Flush work

空扰寡人 提交于 2019-11-27 23:12:39
问题 my question is why flush doesn't work : 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"); } After finishing the method I see "anotherName" in the db also I check it with em.find(Customer.class,1,Lock.None); but still not work RGDS 回答1: You're flushing, but you're not committing - or otherwise ending the transaction / session which is