Re: Issue with using JPA in openejb

2009-01-07 Thread Debarshi Sanyal
Hi,

Thank you for the helpful response. I have incorporated the changes and it's
working fine! It is now clear to me that the issue was due to the object
passed from the client to the server not being attached to the bean's entity
manager context.

Thanks a lot!

Regards,
Debarshi



On Wed, Jan 7, 2009 at 9:30 PM, Jazon  wrote:

> This is not an error or bug.
> the problem is: before your deletion, the entity that is about to be
> deleted
> must be in the persistence context. means you must re-attach it if it is
> not
> managed
>
> the following lines should do the trick
>
> public void deleteMovie(Movie movie){
>
>Movie m = entityManager.find(Movie.class, movie.getId());
>
>entityManager.remove(m);
>
> }
>
>
> On Wed, Jan 7, 2009 at 7:02 AM, Jonathan Gallimore <
> jonathan.gallim...@gmail.com> wrote:
>
> > It looks like this works if you're using the LocalInitialContextFactory
> and
> > looking up a local session bean, but not if you're using
> > RemoteInitialContextFactory (if you're running against a standalone
> server -
> > your test refers to RemoteInitialContextFactory, so I assume that's what
> > you're trying to do) - presumably because the movie object passed back to
> > the Movies.deleteMovie() is no longer "attached" in the bean
> EntityManager
> > context.
> >
> > I managed to get your code to work on a standalone server by adding a
> > private int id field to the Movie entity, along with a getId() and
> setId()
> > method (and annotating the setId method with @Id), and changing the
> > MoviesImpl.deleteMovie() method as follows:
> >
> >   public void deleteMovie(Movie movie) throws Exception {
> >   Query query = entityManager.createQuery("SELECT m from Movie as m
> > where m.id = ?1");
> >   query.setParameter(1, movie.getId());
> >   Movie retrievedMovie = (Movie) query.getSingleResult();
> > if (retrievedMovie != null) {
> >   entityManager.remove(retrievedMovie);
> >   entityManager.flush();
> >   }
> >   }
> >
> > Hope that helps.
> >
> > Jon
> >
> > Debarshi Sanyal wrote:
> >
> >> Hi,
> >>
> >> I have tried both the suggestions but I couldn't get it right.
> >> However, if I add the movies.remove(movie) call just after
> >> movies.persist(movie); in the same function, i.e. in
> >> MoviesImpl::addMovie(Movie movie), the movie indeed DOES NOT get added
> to
> >> the DB.
> >>
> >> The MoviesImpl.java and MoviesTest.java are attached.
> >> Your help is greatly appreciated.
> >>
> >> Regards,
> >> Debarshi
> >>
> >>
> >>
> >>  On Sun, Jan 4, 2009 at 10:48 AM, David Blevins   >> david.blev...@visi.com>> wrote:
> >>
> >>
> >>On Jan 2, 2009, at 1:51 AM, Debarshi Sanyal wrote:
> >>
> >>Hi All,
> >>
> >>New Year greetings!
> >>
> >>
> >>Happy new year to you too!
> >>
> >>
> >>I was trying to execute the example on EJB entity-manager
> >>provided at *
> >>
> >> http://openejb.apache.org/3.0/injection-of-entitymanager-example.html.*
> >>
> >>I have replaced the test case with a standalone client that,
> >>however,
> >>performs exactly the same steps. However, I observed that
> >>while insertion to
> >>the database is occurring perfectly, the deletion does not
> >>take place.
> >>
> >>Movies movies = (Movies) context.lookup("MoviesLocal");
> >>
> >>  movies.addMovie(new Movie("Quentin Tarantino",
> >>"Reservoir Dogs", 1992));
> >>  movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
> >>  movies.addMovie(new Movie("Joel Coen", "The Big
> >>Lebowski", 1998));
> >>
> >>  List list = movies.getMovies();
> >>  assertEquals("List.size()", 3, list.size());
> >>
> >>  *for (Movie movie : list) {
> >>  movies.deleteMovie(movie);
> >>  }*
> >>
> >>Even after the previous steps, *movies.getMovies().size()*
> >>returns 3 which
> >>is very strange.
> >>
> >>
> >>That is very strange.  I'm not sure what could be happening.  When
> >>in doubt add a entityManager.flush() statement and hope for an
> >>error message :) (useful debug tactic with JPA -- just make sure
> >>to remove it in production as it will affect performance)  In this
> >>case I'd add it right after the entityManager.remove(movie); call.
> >>
> >>Hopefully that will dig up something.
> >>
> >>Also double check that one of your modifications did not involve
> >>deleting "PersistenceContextType.EXTENDED" from the example.  That
> >>could cause the issue as well -- entityManager.remove(foo) only
> >>works if the object passed in is still "attached" [1].
> >>
> >>-David
> >>
> >>[1] Attached/Detached explained here
> >>http://openejb.apache.org/3.0/jpa-concepts.html
> >>
> >>
> >>
> >
>


Re: Issue with using JPA in openejb

2009-01-07 Thread Jazon
This is not an error or bug.
the problem is: before your deletion, the entity that is about to be deleted
must be in the persistence context. means you must re-attach it if it is not
managed

the following lines should do the trick

public void deleteMovie(Movie movie){

Movie m = entityManager.find(Movie.class, movie.getId());

entityManager.remove(m);

}


On Wed, Jan 7, 2009 at 7:02 AM, Jonathan Gallimore <
jonathan.gallim...@gmail.com> wrote:

> It looks like this works if you're using the LocalInitialContextFactory and
> looking up a local session bean, but not if you're using
> RemoteInitialContextFactory (if you're running against a standalone server -
> your test refers to RemoteInitialContextFactory, so I assume that's what
> you're trying to do) - presumably because the movie object passed back to
> the Movies.deleteMovie() is no longer "attached" in the bean EntityManager
> context.
>
> I managed to get your code to work on a standalone server by adding a
> private int id field to the Movie entity, along with a getId() and setId()
> method (and annotating the setId method with @Id), and changing the
> MoviesImpl.deleteMovie() method as follows:
>
>   public void deleteMovie(Movie movie) throws Exception {
>   Query query = entityManager.createQuery("SELECT m from Movie as m
> where m.id = ?1");
>   query.setParameter(1, movie.getId());
>   Movie retrievedMovie = (Movie) query.getSingleResult();
> if (retrievedMovie != null) {
>   entityManager.remove(retrievedMovie);
>   entityManager.flush();
>   }
>   }
>
> Hope that helps.
>
> Jon
>
> Debarshi Sanyal wrote:
>
>> Hi,
>>
>> I have tried both the suggestions but I couldn't get it right.
>> However, if I add the movies.remove(movie) call just after
>> movies.persist(movie); in the same function, i.e. in
>> MoviesImpl::addMovie(Movie movie), the movie indeed DOES NOT get added to
>> the DB.
>>
>> The MoviesImpl.java and MoviesTest.java are attached.
>> Your help is greatly appreciated.
>>
>> Regards,
>> Debarshi
>>
>>
>>
>>  On Sun, Jan 4, 2009 at 10:48 AM, David Blevins 
>> > david.blev...@visi.com>> wrote:
>>
>>
>>On Jan 2, 2009, at 1:51 AM, Debarshi Sanyal wrote:
>>
>>Hi All,
>>
>>New Year greetings!
>>
>>
>>Happy new year to you too!
>>
>>
>>I was trying to execute the example on EJB entity-manager
>>provided at *
>>
>> http://openejb.apache.org/3.0/injection-of-entitymanager-example.html.*
>>
>>I have replaced the test case with a standalone client that,
>>however,
>>performs exactly the same steps. However, I observed that
>>while insertion to
>>the database is occurring perfectly, the deletion does not
>>take place.
>>
>>Movies movies = (Movies) context.lookup("MoviesLocal");
>>
>>  movies.addMovie(new Movie("Quentin Tarantino",
>>"Reservoir Dogs", 1992));
>>  movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
>>  movies.addMovie(new Movie("Joel Coen", "The Big
>>Lebowski", 1998));
>>
>>  List list = movies.getMovies();
>>  assertEquals("List.size()", 3, list.size());
>>
>>  *for (Movie movie : list) {
>>  movies.deleteMovie(movie);
>>  }*
>>
>>Even after the previous steps, *movies.getMovies().size()*
>>returns 3 which
>>is very strange.
>>
>>
>>That is very strange.  I'm not sure what could be happening.  When
>>in doubt add a entityManager.flush() statement and hope for an
>>error message :) (useful debug tactic with JPA -- just make sure
>>to remove it in production as it will affect performance)  In this
>>case I'd add it right after the entityManager.remove(movie); call.
>>
>>Hopefully that will dig up something.
>>
>>Also double check that one of your modifications did not involve
>>deleting "PersistenceContextType.EXTENDED" from the example.  That
>>could cause the issue as well -- entityManager.remove(foo) only
>>works if the object passed in is still "attached" [1].
>>
>>-David
>>
>>[1] Attached/Detached explained here
>>http://openejb.apache.org/3.0/jpa-concepts.html
>>
>>
>>
>


Re: Issue with using JPA in openejb

2009-01-07 Thread Jonathan Gallimore
It looks like this works if you're using the LocalInitialContextFactory 
and looking up a local session bean, but not if you're using 
RemoteInitialContextFactory (if you're running against a standalone 
server - your test refers to RemoteInitialContextFactory, so I assume 
that's what you're trying to do) - presumably because the movie object 
passed back to the Movies.deleteMovie() is no longer "attached" in the 
bean EntityManager context.


I managed to get your code to work on a standalone server by adding a 
private int id field to the Movie entity, along with a getId() and 
setId() method (and annotating the setId method with @Id), and changing 
the MoviesImpl.deleteMovie() method as follows:


   public void deleteMovie(Movie movie) throws Exception {
   Query query = entityManager.createQuery("SELECT m from Movie as 
m where m.id = ?1");

   query.setParameter(1, movie.getId());
   Movie retrievedMovie = (Movie) query.getSingleResult();
  
   if (retrievedMovie != null) {

   entityManager.remove(retrievedMovie);
   entityManager.flush();
   }
   }

Hope that helps.

Jon

Debarshi Sanyal wrote:

Hi,

I have tried both the suggestions but I couldn't get it right.
However, if I add the movies.remove(movie) call just after 
movies.persist(movie); in the same function, i.e. in 
MoviesImpl::addMovie(Movie movie), the movie indeed DOES NOT get added 
to the DB.


The MoviesImpl.java and MoviesTest.java are attached.
Your help is greatly appreciated.

Regards,
Debarshi



On Sun, Jan 4, 2009 at 10:48 AM, David Blevins > wrote:



On Jan 2, 2009, at 1:51 AM, Debarshi Sanyal wrote:

Hi All,

New Year greetings!


Happy new year to you too!


I was trying to execute the example on EJB entity-manager
provided at *
http://openejb.apache.org/3.0/injection-of-entitymanager-example.html.*

I have replaced the test case with a standalone client that,
however,
performs exactly the same steps. However, I observed that
while insertion to
the database is occurring perfectly, the deletion does not
take place.

Movies movies = (Movies) context.lookup("MoviesLocal");

  movies.addMovie(new Movie("Quentin Tarantino",
"Reservoir Dogs", 1992));
  movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
  movies.addMovie(new Movie("Joel Coen", "The Big
Lebowski", 1998));

  List list = movies.getMovies();
  assertEquals("List.size()", 3, list.size());

  *for (Movie movie : list) {
  movies.deleteMovie(movie);
  }*

Even after the previous steps, *movies.getMovies().size()*
returns 3 which
is very strange.


That is very strange.  I'm not sure what could be happening.  When
in doubt add a entityManager.flush() statement and hope for an
error message :) (useful debug tactic with JPA -- just make sure
to remove it in production as it will affect performance)  In this
case I'd add it right after the entityManager.remove(movie); call.

Hopefully that will dig up something.

Also double check that one of your modifications did not involve
deleting "PersistenceContextType.EXTENDED" from the example.  That
could cause the issue as well -- entityManager.remove(foo) only
works if the object passed in is still "attached" [1].

-David

[1] Attached/Detached explained here
http://openejb.apache.org/3.0/jpa-concepts.html






Re: Issue with using JPA in openejb

2009-01-07 Thread Debarshi Sanyal
Hi,

I have tried both the suggestions but I couldn't get it right.
However, if I add the movies.remove(movie) call just after
movies.persist(movie); in the same function, i.e. in
MoviesImpl::addMovie(Movie movie), the movie indeed DOES NOT get added to
the DB.

The MoviesImpl.java and MoviesTest.java are attached.
Your help is greatly appreciated.

Regards,
Debarshi



On Sun, Jan 4, 2009 at 10:48 AM, David Blevins wrote:

>
> On Jan 2, 2009, at 1:51 AM, Debarshi Sanyal wrote:
>
>  Hi All,
>>
>> New Year greetings!
>>
>
> Happy new year to you too!
>
>  I was trying to execute the example on EJB entity-manager provided at *
>> http://openejb.apache.org/3.0/injection-of-entitymanager-example.html.*
>>
>> I have replaced the test case with a standalone client that, however,
>> performs exactly the same steps. However, I observed that while insertion
>> to
>> the database is occurring perfectly, the deletion does not take place.
>>
>> Movies movies = (Movies) context.lookup("MoviesLocal");
>>
>>   movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs",
>> 1992));
>>   movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
>>   movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
>>
>>   List list = movies.getMovies();
>>   assertEquals("List.size()", 3, list.size());
>>
>>   *for (Movie movie : list) {
>>   movies.deleteMovie(movie);
>>   }*
>>
>> Even after the previous steps, *movies.getMovies().size()* returns 3 which
>> is very strange.
>>
>
> That is very strange.  I'm not sure what could be happening.  When in doubt
> add a entityManager.flush() statement and hope for an error message :)
> (useful debug tactic with JPA -- just make sure to remove it in production
> as it will affect performance)  In this case I'd add it right after the
> entityManager.remove(movie); call.
>
> Hopefully that will dig up something.
>
> Also double check that one of your modifications did not involve deleting
> "PersistenceContextType.EXTENDED" from the example.  That could cause the
> issue as well -- entityManager.remove(foo) only works if the object passed
> in is still "attached" [1].
>
> -David
>
> [1] Attached/Detached explained here
> http://openejb.apache.org/3.0/jpa-concepts.html
>
>
package ju.impact.ejb;

//START SNIPPET: code
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;
import java.util.List;

@Stateful(name = "Movies")
public class MoviesImpl implements Movies {

@PersistenceContext(unitName = "movie-unit", type = PersistenceContextType.EXTENDED)
private EntityManager entityManager;

public void addMovie(Movie movie) throws Exception {
entityManager.persist(movie);

}

public void deleteMovie(Movie movie) throws Exception {
entityManager.remove(movie);
entityManager.flush();
}

public List getMovies() throws Exception {
Query query = entityManager.createQuery("SELECT m from Movie as m");
return query.getResultList();
}

}
//END SNIPPET: code
package ju.impact.ejb;

import javax.naming.Context;
import javax.naming.InitialContext;
import java.util.List;
import java.util.Properties;

//START SNIPPET: code
public class MoviesTest  {

public static void main(String[] args) {
  try
  {
Properties p = new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "org.apache.openejb.client.RemoteInitialContextFactory");
p.put("movieDatabase", "new://Resource?type=DataSource");
p.put("movieDatabase.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabase.JdbcUrl", "jdbc:hsqldb:mem:moviedb");

p.put("movieDatabaseUnmanaged", "new://Resource?type=DataSource");
p.put("movieDatabaseUnmanaged.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("movieDatabaseUnmanaged.JdbcUrl", "jdbc:hsqldb:mem:moviedb");
p.put("movieDatabaseUnmanaged.JtaManaged", "false");

Context context = new InitialContext(p);

Movies movies = (Movies) context.lookup("MoviesRemote");

//movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
//movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
//movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));
movies.addMovie(new Movie("Sandip Ray", "Kailashe Kelenkari", 2007));
List list = movies.getMovies();
//assertEquals("List.size()", 3, list.size());

for (Movie movie : list) {
movies.deleteMovie(movie);
}

for (Movie movie : list) {
System.out.println("Director: " + movie.getDirector() + 
   ", title: " + movie.getTitle() + 
   ", year: " + movie.getYear() );
}
System.out.println("Size after deletion: " +  movies.getMovi

Re: Issue with using JPA in openejb

2009-01-03 Thread David Blevins


On Jan 2, 2009, at 1:51 AM, Debarshi Sanyal wrote:


Hi All,

New Year greetings!


Happy new year to you too!

I was trying to execute the example on EJB entity-manager provided  
at *
http://openejb.apache.org/3.0/injection-of-entitymanager- 
example.html.*


I have replaced the test case with a standalone client that, however,
performs exactly the same steps. However, I observed that while  
insertion to

the database is occurring perfectly, the deletion does not take place.

Movies movies = (Movies) context.lookup("MoviesLocal");

   movies.addMovie(new Movie("Quentin Tarantino", "Reservoir  
Dogs", 1992));

   movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
   movies.addMovie(new Movie("Joel Coen", "The Big Lebowski",  
1998));


   List list = movies.getMovies();
   assertEquals("List.size()", 3, list.size());

   *for (Movie movie : list) {
   movies.deleteMovie(movie);
   }*

Even after the previous steps, *movies.getMovies().size()* returns 3  
which

is very strange.


That is very strange.  I'm not sure what could be happening.  When in  
doubt add a entityManager.flush() statement and hope for an error  
message :) (useful debug tactic with JPA -- just make sure to remove  
it in production as it will affect performance)  In this case I'd add  
it right after the entityManager.remove(movie); call.


Hopefully that will dig up something.

Also double check that one of your modifications did not involve  
deleting "PersistenceContextType.EXTENDED" from the example.  That  
could cause the issue as well -- entityManager.remove(foo) only works  
if the object passed in is still "attached" [1].


-David

[1] Attached/Detached explained here 
http://openejb.apache.org/3.0/jpa-concepts.html



Issue with using JPA in openejb

2009-01-02 Thread Debarshi Sanyal
Hi All,

New Year greetings!

I was trying to execute the example on EJB entity-manager provided at *
http://openejb.apache.org/3.0/injection-of-entitymanager-example.html.*

I have replaced the test case with a standalone client that, however,
performs exactly the same steps. However, I observed that while insertion to
the database is occurring perfectly, the deletion does not take place.

Movies movies = (Movies) context.lookup("MoviesLocal");

movies.addMovie(new Movie("Quentin Tarantino", "Reservoir Dogs", 1992));
movies.addMovie(new Movie("Joel Coen", "Fargo", 1996));
movies.addMovie(new Movie("Joel Coen", "The Big Lebowski", 1998));

List list = movies.getMovies();
assertEquals("List.size()", 3, list.size());

*for (Movie movie : list) {
movies.deleteMovie(movie);
}*

Even after the previous steps, *movies.getMovies().size()* returns 3 which
is very strange.

I am surely missing something. Request you to please help me resolve this
issue.

Regards,
Debarshi