users@glassfish.java.net

Glassfish v3 & JPA... having difficulty with EntityManagerFactory...

From: <glassfish_at_javadesktop.org>
Date: Thu, 25 Mar 2010 15:21:02 PDT

I've been going round and round on this, and am about to rip out what's left of my hair, so I'm hoping someone here can shed some light on this for me.

Here's the deal: Basic Web Application, with JPA persistence.

Using:
Eclipse IDE 3.5 (Galileo)
Glassfish v3 (with plugin into Eclipse).
JavaDB datastore

I'm fairly certain I did something wrong, something that should be fairly obvious but I'm apparently blind to it.

Ok, first, I've set up my JDBC Connection Pool as a JavaDB connection:

Name: Playlist DB
Resource Type: java.sql.Driver
Driver Classname: org.apache.derby.jdbc.ClientDriver
URL: jdbc:derby://localhost:1527/PlaylistDB;create=true
User: APP
Password: APP

I set up the JDBC Resource as:

JNDI name: jdbc/playlistDB
Pool Name: Playlist DB
Status: Enabled


I am able to ping the Connection Pool, which suggests to me that it is set up correctly in Glassfish, but I could be wrong.

I then have the following for my persistence.xml:

<?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="playlist-db">
                <jta-data-source>jdbc/playlistDB</jta-data-source>
                <class>org.darkhelm.playlist.data.jpa.TrackGroupStored</class>
                <class>org.darkhelm.playlist.data.jpa.Stats</class>
                <class>org.darkhelm.playlist.data.jpa.DailyList</class>
                <properties>
                        <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
                </properties>
        </persistence-unit>
</persistence>


I made a singleton class for getting new EntityManager instances:

final class EMF {
        @PersistenceUnit(unitName="playlist-db" )
        private static EntityManagerFactory emf;
    
    private EMF() {}
    
    public static EntityManager getEM() {
        return emf.createEntityManager();
    }
}

My three annotated classes are TrackGroupStored.java:

@Entity
public class TrackGroupStored {
        @Transient
        private static final Logger log = Logger.getLogger("Playlist.TrackGroup");
        
        @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Basic
        private String name;

        @Basic
        private String type;

        @Basic
        private Set<Track> tracks;

        @Basic
        private Map<String, List<Track>> subLists;

        private TrackGroupStored() {
                setName(null);
                setType(null);
                setTracks(null);
                setSubLists(null);
        }
        
        private TrackGroupStored(TrackGroup that) {
                copyFrom(that);
        }

        public void setId(Long id) {
                this.id = id;
        }

        public Long getId() {
                return id;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getType() {
                return type;
        }

        public void setType(String type) {
                this.type = type;
        }

        public Set<Track> getTracks() {
                return tracks;
        }

        public void setTracks(Set<Track> tracks) {
                this.tracks = tracks;
        }

        public Map<String, List<Track>> getSubLists() {
                return subLists;
        }

        public void setSubLists(Map<String, List<Track>> subLists) {
                this.subLists = subLists;
        }

        public TrackGroup toTrackGroup() {
                Type type = Type.fromString(getType());

                TrackGroup ret = new TrackGroup(type, getName(), getTracks(), getSubLists());

                return ret;
        }

        private void copyFrom(TrackGroup that) {
                setName(that.getName());
                setType(that.getType().toString());
                setTracks(that.getTracks());
                setSubLists(that.getSubLists());
        }

        public static TrackGroup load(Type type, String name) {
                TrackGroup ret = null;

                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        TrackGroupStored results = find(em, type, name);

                        if(results != null) {
                                ret = results.toTrackGroup();
                        }

                        em.getTransaction().commit();

                } catch (Throwable e) {
                        em.getTransaction().rollback();

                        Library.logException(log, TrackGroupStored.class.getName(), "load", e);

                } finally {
                        em.close();
                }

                return ret;
        }

        public static void save(TrackGroup group) {
                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        TrackGroupStored results = find(em, group.getType(), group.getName());

                        if(results != null) {

                                results.copyFrom(group);

                        } else {
                                results = new TrackGroupStored(group);
                        }

                        em.persist(results);

                        em.getTransaction().commit();
                } catch (Throwable e) {
                        em.getTransaction().rollback();

                        Library.logException(log, TrackGroupStored.class.getName(), "save", e);
                } finally {
                        em.close();
                }
        }

        private static TrackGroupStored find(EntityManager em, Type type, String name)
        throws Throwable {

                try {
                        TypedQuery<TrackGroupStored> query = em.createQuery("SELECT * FROM TrackGroup.Stored WHERE name = :name AND type = :type", TrackGroupStored.class);

                        query.setParameter("name", name);
                        query.setParameter("type", type.toString());

                        return query.getSingleResult();
                        
                } catch(NoResultException e) {
                        return null;
                }
        }
}



DailyList.java:

@Entity
public class DailyList {
        @SuppressWarnings("unused")
        @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
        private Long id;

        @Basic
        private Set<Track> tracks;

        @Basic
        @Temporal(TemporalType.DATE)
        private Date dateGenerated;

        @Transient
        private final static Logger log = Logger.getLogger("Playlist.DailyList");

        private DailyList() {
                this.tracks = new HashSet<Track>();
                this.dateGenerated = new Date();
        }
        
        private DailyList(Set<Track> tracks) {
                this.tracks = tracks;
                this.dateGenerated = new Date();
        }

        public static Set<Track> getList() {
                Set<Track> ret = null;

                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        TypedQuery<DailyList> query = em.createQuery("SELECT * FROM DailyList", DailyList.class);
                        
                        ret = query.getSingleResult().tracks;
                        
                        em.getTransaction().commit();
                } catch (NoResultException e) {
                        
                        em.getTransaction().commit();
                        
                } catch (Throwable e) {
                        em.getTransaction().rollback();

                        Library.logException(log, DailyList.class.getName(), "getList", e);
                } finally {
                        em.close();
                }

                return ret;
        }

        public static Date getDateGenerated() {
                Date ret = null;

                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        TypedQuery<DailyList> query = em.createQuery("SELECT * FROM DailyList", DailyList.class);
                        
                        ret = query.getSingleResult().dateGenerated;

                        em.getTransaction().commit();
                } catch (NoResultException e) {
                        
                        em.getTransaction().commit();
                        
                } catch (Throwable e) {
                        em.getTransaction().rollback();

                        Library.logException(log, DailyList.class.getName(), "getDateGenerated", e);

                } finally {
                        em.close();
                }

                return ret;
        }

        public static void updateList(Set<Track> list) {
                EntityManager em = EMF.getEM();
                
                try {
                        em.getTransaction().begin();

                        TypedQuery<DailyList> query = em.createQuery("SELECT * FROM DailyList", DailyList.class);
                        
                        DailyList currList = query.getSingleResult();

                        currList.tracks = list;
                        
                        currList.dateGenerated = new Date();
                        
                        em.persist(currList);
                        
                        em.getTransaction().commit();
                        
                } catch (NoResultException e) {
                        DailyList currList = new DailyList(list);
                        
                        em.persist(currList);
                        
                        em.getTransaction().commit();
                        
                } catch (Exception e) {
                        em.getTransaction().rollback();

                        Library.logException(log, DailyList.class.getName(), "setList", e);
                } finally {
                        em.close();
                }
        }
}


and Stats.java:

@Entity
public class Stats {
        @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

        @Basic
        @Temporal(TemporalType.DATE)
        private Date date;
        
        @Basic
        private String step;
        
        @Basic
        private String type;
        
        @Basic
        private String sub;
        
        @Basic
        private byte rating;
        
        @Basic
        private String name;
        
        @Basic
        private String label;
        
        @Basic
        private int value;

        @Transient
        private final static Logger log = Logger.getLogger("Playlist.Stats");

        private Stats() {
                setDate(null);
                setStep(null);
                setType(null);
                setSub(null);
                setRating((byte)0);
                setName(null);
                setLabel(null);
                setValue(0);
        }
        
        private Stats(Date date, String step, String type, String sub, byte rating, String name, String label, int value) {
                setDate(date);
                setStep(step);
                setType(type);
                setSub(sub);
                setRating(rating);
                setName(name);
                setLabel(label);
                setValue(value);
        }

        public static void submit(Date date, String step, String type, String sub, byte rating, String name, String label, int value) {
                Stats stat = new Stats(date, step, type, sub, rating, name, label, value);
                
                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        em.persist(stat);

                        em.getTransaction().commit();

                } catch (Throwable e) {
                        em.getTransaction().rollback();
                        Library.logException(log, Stats.class.getName(), "add", e);
                } finally {
                        em.close();
                }
        }

        public static int size() {
                int ret = 0;

                EntityManager em = EMF.getEM();

                try {
                        em.getTransaction().begin();

                        TypedQuery<Stats> query = em.createQuery("SELECT * FROM Stats", Stats.class);
                        
                        List<Stats> allStats = query.getResultList();
                        
                        ret = allStats.size();
                        
                        em.getTransaction().commit();
                } finally {
                        em.close();
                }
                return ret;
        }
        
        public void setId(Long id) {
                this.id = id;
        }

        public Long getId() {
                return id;
        }

        public Date getDate() {
                return date;
        }

        public void setDate(Date date) {
                this.date = date;
        }

        public String getStep() {
                return step;
        }

        public void setStep(String step) {
                this.step = step;
        }

        public String getType() {
                return type;
        }

        public void setType(String type) {
                this.type = type;
        }

        public String getSub() {
                return sub;
        }

        public void setSub(String sub) {
                this.sub = sub;
        }

        public byte getRating() {
                return rating;
        }

        public void setRating(byte rating) {
                this.rating = rating;
        }

        public String getName() {
                return name;
        }

        public void setName(String name) {
                this.name = name;
        }

        public String getLabel() {
                return label;
        }

        public void setLabel(String label) {
                this.label = label;
        }

        public int getValue() {
                return value;
        }

        public void setValue(int value) {
                this.value = value;
        }
}



I have a servlet designed that starts the process, but when I run the servlet, I get the following exception:

WARNING: StandardWrapperValve[Main]: PWC1406: Servlet.service() for servlet Main threw exception
java.lang.NullPointerException
        at org.darkhelm.playlist.data.jpa.EMF.getEM(EMF.java:28)
        at org.darkhelm.playlist.data.jpa.DailyList.getDateGenerated(DailyList.java:85)
        at org.darkhelm.playlist.generator.Step$1.start(Step.java:52)
        at org.darkhelm.playlist.generator.Step$9.run(Step.java:477)
        at org.darkhelm.playlist.generator.Step.run(Step.java:483)
        at org.darkhelm.playlist.generator.Step.run(Step.java:461)
        at org.darkhelm.playlist.servlets.MainServlet.doGet(MainServlet.java:32)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:734)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
        at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:279)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
        at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
        at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
        at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
        at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
        at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:165)
        at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:791)
        at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:693)
        at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:954)
        at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:170)
        at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:135)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:102)
        at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:88)
        at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:76)
        at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:53)
        at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:57)
        at com.sun.grizzly.ContextTask.run(ContextTask.java:69)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:330)
        at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:309)
        at java.lang.Thread.run(Thread.java:619)


The line in question is:

return emf.createEntityManager();


So.... to me, this means that something is null. Did I set up my EMF class incorrectly? Or did I miss something else? I'd appreciate any pointers and/or help to resolve this issue.
[Message sent by forum member 'xlorepdarkhelm']

http://forums.java.net/jive/thread.jspa?messageID=393815