我有一个Jax RS服务器,当使用XML格式的电影发送POST请求时,该服务器被正确调用。
@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();
}
}
调试显示没有任何错误,但没有任何持久性。 数据源是EclipseLink上的JTA,这里是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>
从EclipseLink返回的日志在调用em.merge()时没有显示任何错误,它们主要涉及序列创建:
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)
有没有人知道缺少什么? Movie类非常简单,并且与其他表没有依赖关系,我认为它非常简单,我很遗憾。
编辑:
如果我在merge()之后添加flush(),我会收到错误:
javax.persistence.TransactionRequiredException:
Exception Description: No externally managed transaction is currently active for this thread
答案 0 :(得分:2)
你不应该调用flush(),而是创建一个企业bean,所以:
@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);
}
}
然后修改你的MovieResource类:
@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();
}
}
答案 1 :(得分:0)
您正在使用JTA持久性单元,但未启动JTA事务。
切换到RESOURCE_LOCAL和非JTA DataSource,或者使用JTA事务,例如使用EJB(参见其他答案)。