hi nikolaos,
thank you for your help. i removed the lines that you
suggested. however, i am now receiving a NullPointerException
on a call to:
Stripersist.getEntityManager();
i have attached the class BaseDaoImpl.java -- where the
offending line of code resides.
the stack trace is as follows:
java.lang.NullPointerException
at
org.stripesstuff.stripersist.Stripersist.getEntityManager(Stripersist.java:472)
at
org.stripesstuff.stripersist.Stripersist.getEntityManager(Stripersist.java:472)
at
stripesbook.dao.impl.stripersist.BaseDaoImpl.save(BaseDaoImpl.java:48)
at
stripesbook.dao.impl.stripersist.BaseDaoImpl.save(BaseDaoImpl.java:48)
at
stripesbook.dao.impl.stripersist.UserDaoImplTest.testSave(UserDaoImplTest.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
stripesbook.dao.impl.stripersist.UserDaoImplTest.testSave(UserDaoImplTest.java:62)
thank you again,
lev
On Thu, Jul 8, 2010 at 1:11 PM, Nikolaos Giannopoulos
<nikol...@brightminds.org> wrote:
> Lev,
>
> So in case I wasn't totally clear... in looking at your attached code... if
> you remove the lines:
>
> protected static Stripersist stripersist;
> public static final String persistenceFile =
> "/home/lev/projects/stripes/code/MavenEmail36WebApp/"
> + "target/classes/META-INF/persistence.xml";
>
> ... and also remove these lines:
>
> stripersist = new Stripersist();
> // System.out.println("Opening persistence file:\n" +
> persistenceFile);
> URL url = (new File(persistenceFile)).toURI().toURL();
> System.out.println("Url:\n" + url);
> stripersist.init(url);
>
> ... it should work assuming your Dao code is OK.
>
> And "/META-INF/persistence.xml" is on your class path i.e. as a subfolder
> under your classes folder then it should work.
>
> If it doesn't after doing that then please provide the complete stack trace
> and the code for your Dao.
>
> --Nikolaos
>
>
>
> Nikolaos Giannopoulos wrote:
>
> Lev,
>
> OK. Well first of all as long as you have your persistence.xml in a
> location on the classpath under "/META-INF/persistence.xml" then Stripersist
> will find it. The second thing is that as Aaron pointed out you should use
> the static methods:
>
> Stripersist.requestInit();
> ...
> <test code>
> ...
> Stripersist.requestComplete();
>
> As part of the requestInit() the entity manager factories will load and the
> init() will trigger and the persistence.xml will be loaded.
>
> The above static methods are explicitly provided so that you can use
> Stripersist outside of normal Stripes request processing... so you should
> not be trying to construct the URL to the persistence.xml yourself and
> calling init()... that is bound to result in problems.
>
> HTH,
>
> --Nikolaos
>
>
>
>
> Lev wrote:
>
> apologies: i forgot to attach my persistence.xml file to the
> previous email. please find it attached. thanks!
>
> On Thu, Jul 8, 2010 at 1:57 AM, Lev <d...@plektos.com> wrote:
>
>
> hi,
>
> i am trying to test code from "stripes... and java web development
> is fun again". specifically, i have imported the email_36 into a
> netbeans maven project.
>
> i have written a simple JUnit test for the project file:
> com.dao.impl.stripersist.UserDaoImpl. i have attached the
> test file.
>
> further, i have attached my persistence.xml file. it is located in:
> target/classes/META-INF/persistence.xml
>
> the error that i receive when executing the aforementioned
> JUnit test is:
>
> javax.persistence.PersistenceException: No Persistence provider for
> EntityManager named stripesbook_MavenEmail36WebApp_war_1.0-SNAPSHOTPU
>
> the exception is thrown at line 34 of the attached Junit test
> file. specifically, the call that triggers the error is:
>
> stripersist.init(url);
>
> i have confirmed that the URL is valid -- if i change it to something
> malformed, i receive an exception complaining that the file cannot
> be found.
>
> thank you again for your help,
> lev
>
>
> On Wed, Jul 7, 2010 at 10:19 AM, Aaron Porter <aa...@mongus.com> wrote:
>
>
> Lev,
> Sounds like you're getting closer! Now it looks like you need to check
> your JPA configuration - make sure your persistence.xml is correct and
> you've got all the jars to support it. I use Hibernate as the JPA
> provider and PostgreSQL as the database.
>
> Aaron
>
> On 07/06/2010 11:42 PM, Lev wrote:
>
>
> hi aaron,
>
> thank you for your help.
>
> 1) my persistence.xml ends up in the project root at:
>
> target/classes/META-INF/persistence.xml
>
> this appears to be correct to me. please correct me if
> i am wrong.
>
> 2) i implemented the calls that you suggested in the
> junit setUpClass() and tearDownClass() methods.
>
> it appears that Stripersist is successfully instantiated.
> however, the call "stirpersist.init(url)" fails with the
> following exception:
>
> javax.persistence.PersistenceException: No Persistence provider for
> EntityManager named...
>
> i believe that the URL is correctly formed. it points to
> the persistence.xml file in target/classes/META-INF.
>
> thank you again for your help,
> lev
>
> On Wed, Jul 7, 2010 at 12:56 AM, Aaron Porter<aa...@mongus.com> wrote:
>
>
>
> Lev,
> The META-INF directory that contains persistence.xml needs to end up at
> /WEB-INF/classes/META-INF. It looks like the way you've set it up it
> would be at /WEB-INF/classes/resources/META-INF. What I was proposing
> was to create a new source folder called resources. In Eclipse you do
> that by clicking File->New->Source Folder and enter resources as the
> folder name.
>
> I'm guessing that even if you do have the persistence.xml file in the
> correct location you'll get an error about trying to use Stripersist
> without the Interceptor part of it because you're trying to test with
> JUnit without a servlet container. It can be done but you'll need to
> call some methods in Stripersist yourself. Create a new instance of
> Stripersist and call init(new URL("path/to/persistence.xml")). Call
> requestInit() to initialize for the current thread then call
> requestComplete() to clean up when you're done.
>
> Aaron
>
>
> On 07/06/2010 10:25 PM, Lev wrote:
>
>
>
> hi,
>
> i moved persistence.xml to src/resources/META-INF.
>
> however, the original problem persists -- i receive a
> NullPointerException on the call:
>
> Stripersist.getEntityManager();
>
> further information: i'm getting this exception when
> executing a Junit test of a DAO class -- i'm not
> executing web code (action beans, etc). would that
> contribute to the problem?
>
> any suggestions welcomed.
>
> thank you,
> lev
>
>
> On Tue, Jul 6, 2010 at 11:59 AM, Aaron Porter<aa...@mongus.com> wrote:
>
>
>
>
> Oops! Thanks for catching that Marcus!
>
> Aaron
>
> On 07/06/2010 09:09 AM, M.C.S. wrote:
>
>
>
>
> Hi,
>
> Aaron Porter wrote:
>
>
>
>
>
> If you're using Eclipse I recommend creating a source directory named
> resources and then creating a subdirectory named WEB-INF and placing
> persistence.xml in there.
>
>
>
>
>
>
> Surely just a typo, but it surely should be named META-INF. I really
> hate these standard folder names :-)
>
> Best wishes
> Marcus
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>
>
> ------------------------------------------------------------------------------
> This SF.net email is sponsored by Sprint
> What will you do first with EVO, the first 4G phone?
> Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
> _______________________________________________
> Stripes-users mailing list
> Stripes-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/stripes-users
>
>
/***
* Excerpted from "Stripes: and Java Web Development is Fun Again",
* published by The Pragmatic Bookshelf.
* Copyrights apply to this code. It may not be used to create training material,
* courses, books, articles, and the like. Contact us if you are in doubt.
* We make no guarantees that this code is fit for any purpose.
* Visit http://www.pragmaticprogrammer.com/titles/fdstr for more book information.
***/
package stripesbook.dao.impl.stripersist;
import org.stripesstuff.stripersist.Stripersist;
import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NonUniqueResultException;
import javax.persistence.NoResultException;
import javax.persistence.Query;
import stripesbook.dao.Dao;
import stripesbook.model.User;
public abstract class BaseDaoImpl<T,ID extends Serializable>
implements Dao<T,ID>
{
private Class<T> entityClass;
@SuppressWarnings("unchecked")
public BaseDaoImpl() {
entityClass = (Class<T>)
((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0];
}
@Override
@SuppressWarnings("unchecked")
public List<T> read() {
return Stripersist.getEntityManager()
.createQuery("from " + entityClass.getName())
.getResultList();
}
@Override
public T read(ID id) {
return Stripersist.getEntityManager().find(entityClass, id);
}
@Override
@SuppressWarnings("unchecked")
public void save(T object) {
EntityManager manager = Stripersist.getEntityManager();
manager.persist(object);
}
@Override
public void delete(T object) {
Stripersist.getEntityManager().remove(object);
}
@Override
public void commit() {
Stripersist.getEntityManager().getTransaction().commit();
}
@SuppressWarnings("unchecked")
public T findBy(String fieldName, Object value) {
Query query = Stripersist.getEntityManager()
.createQuery(getQuery(fieldName, null))
.setParameter(fieldName, value);
return getSingleResult(query);
}
@SuppressWarnings("unchecked")
public T findBy(String fieldName, Object value, User user) {
Query query = Stripersist.getEntityManager()
.createQuery(getQuery(fieldName, user))
.setParameter(fieldName, value)
.setParameter("user", user);
return getSingleResult(query);
}
protected Class<T> getEntityClass() {
return entityClass;
}
private String getQuery(String fieldName, User user){
String query =
"from " + entityClass.getName() + " t " +
"where t." + fieldName + " = :" + fieldName;
if (user == null) {
return query;
}
return query + " and t.user = :user";
}
@SuppressWarnings("unchecked")
private T getSingleResult(Query query) {
try {
return (T) query.getSingleResult();
}
catch (NonUniqueResultException exc) {
return (T) query.getResultList().get(0);
}
catch (NoResultException exc) {
return null;
}
}
}
------------------------------------------------------------------------------
This SF.net email is sponsored by Sprint
What will you do first with EVO, the first 4G phone?
Visit sprint.com/first -- http://p.sf.net/sfu/sprint-com-first
_______________________________________________
Stripes-users mailing list
Stripes-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/stripes-users