Persistence example with Stripersist, JPA, and Hibernate

View: New views
7 Messages — Rating Filter:   Alert me  

Persistence example with Stripersist, JPA, and Hibernate

by dan06 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I've put together a simple persistence example, but unfortunately it doesn't work and I can't figure out why (I originally thought I was misusing Stripersist, but after some clarification realized that was not the case). Following are the pertinent pieces of code, if anyone sees what is wrong please let me know. Note: the error stack shows NullPointerException - practice.example.dao.BookDao.listBooks (BookDao.java:16)
.

/* BookDao.java */
public class BookDao {
    @SuppressWarnings("unchecked")
    public List<Book> listBooks(){
        return Stripersist.getEntityManager().createQuery("from Book").getResultList();
    }
}

/* Book.java */
@Entity
@Table(name="books")
public class Book {
   
    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;
   
    @Column(name="title")
    private String title;
   
    @Column(name="author_name")
    private String authorName;
   
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

       /* The remaining getters/setters and an empty constructor */

-Dan



     

------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Persistence example with Stripersist, JPA, and Hibernate

by Ben Gunter-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Most likely your entity manager isn't getting initialized, and that's where the NPE is coming from. Try breaking down listBooks(), assigning each call to a variable before accessing it, like so:

EntityManager em = Stipersist.getEntityManager();
Query query = em.createQuery("from Book");
List books = query.getResultList();
log.debug("Got " + books.size() + " books");
return books;

The stack trace will show the line where the NPE happens, and it will be clear what is null. Really, though, if it is in listBooks() it's almost certainly the entity manager that is null.

You should set the log level to debug for org.stripesstuff so you can see exactly what it's doing. If the entity manager is null, then it's because Stripersist isn't initializing properly.

What version of Stripes are you using and what app server?

-Ben

On Mon, Oct 26, 2009 at 4:30 PM, Dan King <dan.king106@...> wrote:
I've put together a simple persistence example, but unfortunately it doesn't work and I can't figure out why (I originally thought I was misusing Stripersist, but after some clarification realized that was not the case). Following are the pertinent pieces of code, if anyone sees what is wrong please let me know. Note: the error stack shows NullPointerException - practice.example.dao.BookDao.listBooks (BookDao.java:16)
.

/* BookDao.java */
public class BookDao {
   @SuppressWarnings("unchecked")
   public List<Book> listBooks(){
       return Stripersist.getEntityManager().createQuery("from Book").getResultList();
   }
}

/* Book.java */
@Entity
@Table(name="books")
public class Book {

   @Id
   @GeneratedValue
   @Column(name="id")
   private int id;

   @Column(name="title")
   private String title;

   @Column(name="author_name")
   private String authorName;

   public int getId() {
       return id;
   }
   public void setId(int id) {
       this.id = id;
   }

      /* The remaining getters/setters and an empty constructor */

-Dan





------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Persistence example with Stripersist, JPA, and Hibernate

by dan06 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Ben,
Ben Gunter-2 wrote:
Most likely your entity manager isn't getting initialized, and that's where
the NPE is coming from.... If the entity manager is null, then it's because
Stripersist isn't initializing properly.

What version of Stripes are you using and what app server?

-Ben
I did as you suggested, and it turns out that the entity manager was null; I'm lost as to why. I'm using Stripes 1.5.1, Stripersist 1.0, and Tomcat v6.0.20.

-Dan

Re: Persistence example with Stripersist, JPA, and Hibernate

by Freddy D. :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Dan,

> it turns out that the entity manager was null;
> I'm lost as to why.

Just a thought, maybe the problem resides in the location, or the contents,
of the persistence.xml file?

Another possibility is an error in one of your entity classes, in terms
of JPA annotations.

I think Stripersist does a pretty good job of logging, so cranking it up
to DEBUG might help.

Cheers,
Freddy
http://www.stripesbook.com



------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Persistence example with Stripersist, JPA, and Hibernate

by dan06 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
Freddy D. wrote:
Hi Dan,

Just a thought, maybe the problem resides in the location, or the contents,
of the persistence.xml file?...I think Stripersist does a pretty good job of logging, so cranking it up
to DEBUG might help.

Cheers,
Freddy
http://www.stripesbook.com
I changed logging to debug, it appears there is something wrong with the persistence.xml file. The debug message states: In order to call Stripersist.getEntityManager() without any parameters there must be exactly one persistence unit defined. When I specify the unit the message states: Couldn't find EntityManagerFactory for persistence unit persistEx.

I'm following along with the Stripes book (chap. 12), and so there is only one persistence unit, located in /WEB-INF/classes/META-INF; the only difference between the book and my example is that I'm using postgresql (rather than hsql). Please find below the my persistence.xml file; I've racked my brains trying to find what is wrong with it, if someone sees the error(s), let me know.

<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_1_0.xsd" version="1.0" >
        <persistence-unit name="persistEx">
                <provider>org.hibernate.ejb.HibernatePersistence</provider>
                <properties>
                        <property name="hibernate.archive.autodetection" value="class"/>
                        <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
                        <property name="hibernate.connect.driver_class" value="org.postgresql.Driver"/>
                        <property name="hibernate.connection.url" value="jdbc:postgresql://xxx.xxx.xxx.xxx:5432/sample"/>
                        <property name="hibernate.connection.username" value="user"/>
                        <property name="hibernate.connection.password" value="password"/>
                        <property name="jdbc.batch_size" value="0"/>
                        <property name="hibernate.c3p0.min_size" value="5"/>
                        <property name="hibernate.c3p0.max_size" value="20"/>
                        <property name="hibernate.c3p0.timeout" value="300"/>
                        <property name="hibernate.c3p0.max_statements" value="50"/>
                        <property name="hibernate.c3p0.idle_test_period" value="3000"/>
                </properties>
        </persistence-unit>
</persistence>

-Dan

Re: Persistence example with Stripersist, JPA, and Hibernate

by Aaron Porter-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Dan,
When the app initializes you should see some messages from Stripersist initializing that tell you what persistence.xml files are being loaded. If you don't see anything like that you're probably missing org.stripesstuff.stripersist under Extension.Packages in your web.xml.

Aaron

dan06 wrote:
Hi,

Freddy D. wrote:
  
Hi Dan,

Just a thought, maybe the problem resides in the location, or the
contents,
of the persistence.xml file?...I think Stripersist does a pretty good job
of logging, so cranking it up
to DEBUG might help.

Cheers,
Freddy
http://www.stripesbook.com

    

I changed logging to debug, it appears there is something wrong with the
persistence.xml file. The debug message states: In order to call
Stripersist.getEntityManager() without any parameters there must be exactly
one persistence unit defined. When I specify the unit the message states:
Couldn't find EntityManagerFactory for persistence unit persistEx.

I'm following along with the Stripes book (chap. 12), and so there is only
one persistence unit, located in /WEB-INF/classes/META-INF; the only
difference between the book and my example is that I'm using postgresql
(rather than hsql). Please find below the my persistence.xml file; I've
racked my brains trying to find what is wrong with it, if someone sees the
error(s), let me know.

<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_1_0.xsd" version="1.0" >
	<persistence-unit name="persistEx">
		<provider>org.hibernate.ejb.HibernatePersistence</provider>
		<properties>
			<property name="hibernate.archive.autodetection" value="class"/>
			<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQLDialect"/>
			<property name="hibernate.connect.driver_class"
value="org.postgresql.Driver"/>
			<property name="hibernate.connection.url"
value="jdbc:postgresql://xxx.xxx.xxx.xxx:5432/sample"/>
			<property name="hibernate.connection.username" value="user"/>
			<property name="hibernate.connection.password" value="password"/>			
			<property name="jdbc.batch_size" value="0"/>
			<property name="hibernate.c3p0.min_size" value="5"/>
			<property name="hibernate.c3p0.max_size" value="20"/>
			<property name="hibernate.c3p0.timeout" value="300"/>
			<property name="hibernate.c3p0.max_statements" value="50"/>
			<property name="hibernate.c3p0.idle_test_period" value="3000"/>
		</properties>
	</persistence-unit>
</persistence>

-Dan
  


------------------------------------------------------------------------------
Come build with us! The BlackBerry(R) Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9 - 12, 2009. Register now!
http://p.sf.net/sfu/devconference
_______________________________________________
Stripes-users mailing list
Stripes-users@...
https://lists.sourceforge.net/lists/listinfo/stripes-users

Re: Persistence example with Stripersist, JPA, and Hibernate

by dan06 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
Aaron Porter-3 wrote:
Hi Dan,
When the app initializes you should see some messages from Stripersist
initializing that tell you what persistence.xml files are being loaded.
If you don't see anything like that you're probably missing
org.stripesstuff.stripersist under Extension.Packages in your web.xml.

Aaron
After tweaking my log4j settings and reviewing all the debug message I discovered the cause of the issue(s) were inconsistent jar files. I had a hodge-podge of different jar versions of hibernate and its dependencies. By using all the latest versions the problem was solved. Thanks for bearing through my questions, I appreciate the help.

Best,
Dan