users@glassfish.java.net

Re: JEE 6 / Glassfish v3 / Eclipse - JPA Entity Manager Issues

From: Alexis Moussine-Pouchkine <alexis.mp_at_sun.com>
Date: Wed, 07 Apr 2010 12:35:55 +0200

you can only perform injection (of your persistence manager) in managed objects such as EJB's, (JSF) managed beans, servlets, but not in plain POJOs.
also, you should probably update your persistence.xml to use a JPA 2.0 declaration header.
-Alexis

On 7 avr. 2010, at 03:41, glassfish_at_javadesktop.org wrote:

> Hey gang,
>
> This is my first post on the java.net forums. It seems like a very nice community, hopefully ya'll can help me out on my issue.
>
> I am creating a layered project
>
> - EAR Project
> - EJB Project
> - JPA Project
> - WAR Project
>
> Right now I have my web layer creating a new "user" entity with a username/password (via JSF 2.0 & AJAX; which is really cool).
>
> @ManagedBean(name = "userBeanUtil")
> @SessionScoped
> public class UserBeanUtil
> {
> private User user;
>
> public UserBeanUtil()
> {
> this.user = new User();
> }
>
> public User getUser()
> {
> return user;
> }
>
> public void setUser(User user)
> {
> this.user = user;
> }
>
> public void createUserAction(ActionEvent event)
> {
> DevUserController.update(user);
> }
>
> I then call the EJB layer UserController.update(user) hitting a local EJB which then calls an update method in my JPA layer.
>
> @LocalBean
> @Stateless
> public class UserController
> {
>
> /**
> * Default constructor.
> */
> public UserController()
> {
> }
>
> public static void update(User user)
> {
> UserHandler.persist(user);
> }
>
> }
>
> This layer will actually persist the data to the database. However, no matter how I try, I the "EntityManager" is always null.
>
> public class UserHandler
> {
> @PersistenceUnit(unitName = "MyPersistenceUnit")
> private static EntityManager em1;
>
> @PersistenceUnit(name = "MyPersistenceUnit")
> private static EntityManager em2;
>
> @PersistenceUnit
> private static EntityManager em3;
>
> @PersistenceContext(unitName = "MyPersistenceUnit")
> private static EntityManager em4;
>
> @PersistenceContext(unitName = "MyPersistenceUnit")
> private static EntityManager em5;
>
> @PersistenceContext
> private static EntityManager em6;
>
> public static void persist(User user)
> {
> em1.persist(user);
> }
>
> }
>
> Here is my persistence.xml file:
>
> <?xml version="1.0" encoding="UTF-8"?>
> <persistence version="1.0"
> 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_1_0.xsd">
> <persistence-unit name="MyPersistenceUnit" transaction-type="JTA">
> <jta-data-source>jdbc/RDF</jta-data-source>
> </persistence-unit>
> </persistence>
>
> Now, I did go through the MySQL / Glassfish V3 tutorial and setup a connection pool and JDBC Resource. I did a ping test on jdbc/RDF and it was successful.
>
> Long story short; I have no clue how come my EntityManager is not being injected.
>
> Anyone have any ideas?
>
> Hope I've given enough information. If not let me know; I'll be happy to provide more info.
>
> Also, if ya'll have links to "JEE6 Best Practices Documents" please feel free to share.
>
> Thanks,
> Chris
> [Message sent by forum member 'iceman3479']
>
> http://forums.java.net/jive/thread.jspa?messageID=395613
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>