I have a Jax RS server which is correctly called when a POST request is sent with a Movie in XML format.
@Resource(name = "movie")
@Path("/movie")
public class MovieResource
{
@PersistenceContext(unitName = "movieDS")
private EntityManager em;
public MovieResource()
{
em = PersistenceProvider.createEntityManager();
}
@POST
@Path("/post")
@Consumes(
{
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
public Response createMovie(Movie movie)
{
if (!em.contains(newMovie))
{
em.merge(newMovie);
}
String result = "Movie created : " + movie;
return Response.status(201).entity(movie).build();
}
}
debugging shows no errors whatsoever however nothing is persisted. The datasource is JTA over EclipseLink, here is persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.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_2_0.xsd">
<!-- The data source should be set up in Glassfish -->
<persistence-unit name="MovieManager" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/movieDS</jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="ALL"/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
The logs returned from EclipseLink show no error whatsoever while calling em.merge(), they mostly involve sequence creation:
FINEST: Execute query ValueReadQuery(name="SEQUENCE" sql="SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = #SEQ_NAME")
FINE: SELECT SEQ_COUNT FROM SEQUENCE WHERE SEQ_NAME = ?
bind => [1 parameter bound]
FINEST: local sequencing preallocation for SEQ_GEN: objects: 50 , first: 51, last: 100
INFO: RAR7115: Unable to set ClientInfo for connection
FINEST: Connection released to connection pool [default].
FINER: TX commitTransaction, status=STATUS_ACTIVE
FINER: TX Internally committing
FINEST: local sequencing preallocation is copied to preallocation after transaction commit
FINER: external transaction has committed internally
FINEST: assign sequence to the object (51 -> net.plarz.entities.LoginInformation@91229c)
Has anyone any idea what's missing? the Movie class is very simple and has no dependencies with other tables, I think its something really simple I'm missing.
EDIT :
If I add a flush() after merge() I get an error:
javax.persistence.TransactionRequiredException:
Exception Description: No externally managed transaction is currently active for this thread
You shouldn't call flush() but instead create an enterprise bean so:
@Stateless
public class MovieEJB
{
@PersistenceContext(unitName = "movieDS")
private EntityManager em;
@Override
public Movie create(Movie movie) throws Exception
{
em.persist(movie);
return movie;
}
@Override
public void delete(Movie movie)
{
em.remove(em.merge(movie));
}
@Override
public Movie update(Movie movie) throws Exception
{
return em.merge(movie);
}
}
then modify your MovieResource class so:
@ManagedBean(name = "restController")
@SessionScoped
@Resource(name = "movie")
@Path("/movie")
public class MovieResource
{
@EJB
private MovieEJB movieEJB;
public MovieResource()
{
}
public MovieEJBLocal getMovieEJB()
{
return movieEJB;
}
public void setMovieEJB(MovieEJBLocal movieEJB)
{
this.movieEJB = movieEJB;
}
@POST
@Path("/post")
@Consumes(
{
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
public Response createMovie(Movie movie)
{
getMovieEJB().create(movie);
String result = "Movie created : " + movie;
return Response.status(201).entity(movie).build();
}
}