gpt4 book ai didi

java - EntityManager 静默坚持失败

转载 作者:行者123 更新时间:2023-11-30 04:14:59 25 4
gpt4 key购买 nike

我有一个 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>

在调用 em.merge() 时,从 EclipseLink 返回的日志没有显示任何错误,它们主要涉及序列创建:

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() 之后添加lush(),则会收到错误:

javax.persistence.TransactionRequiredException: 
Exception Description: No externally managed transaction is currently active for this thread

最佳答案

您不应该调用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();
}
}

关于java - EntityManager 静默坚持失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18608671/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com