java - EntityManager persist silently fails -
i have jax rs server correctly called when post request sent 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 nothing persisted. datasource jta on eclipselink, here 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"> <!-- data source should set 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 eclipselink show no error whatsoever while calling em.merge(), involve sequence creation:
finest: execute query valuereadquery(name="sequence" sql="select seq_count sequence seq_name = #seq_name") fine: select seq_count sequence seq_name = ? bind => [1 parameter bound] finest: local sequencing preallocation seq_gen: objects: 50 , first: 51, last: 100 info: rar7115: unable set clientinfo connection finest: connection released connection pool [default]. finer: tx committransaction, status=status_active finer: tx internally committing finest: local sequencing preallocation copied preallocation after transaction commit finer: external transaction has committed internally finest: assign sequence object (51 -> net.plarz.entities.logininformation@91229c)
has idea what's missing? movie class simple , has no dependencies other tables, think simple i'm missing.
edit :
if add flush() after merge() error:
javax.persistence.transactionrequiredexception: exception description: no externally managed transaction active thread
you shouldn't call flush() instead create 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 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(); } }
Comments
Post a Comment