1

我有一个 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();
    }
}

调试显示没有任何错误,但没有任何内容持续存在。数据源是 JTA over EclipseLink,这里是 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
4

2 回答 2

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();
    }
}
于 2013-09-04T10:14:25.800 回答
0

您正在使用 JTA 持久性单元,但未启动 JTA 事务。

要么切换到 RESOURCE_LOCAL 和非 JTA 数据源,要么使用 JTA 事务,例如使用 EJB(参见其他答案)。

于 2013-09-04T14:53:25.013 回答