- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我有一个用户实体:
@Entity
@Table( name = "bi_user" )
@SequenceGenerator( name = "USER_SEQ_GEN", sequenceName = "USER_SEQUENCE" )
public class User
extends DataObjectAbstract<Long>
{
private static final long serialVersionUID = -7870157016168718980L;
/**
* key for this instance. Should be managed by JPA provider.
*/
@Id
@GeneratedValue( strategy = GenerationType.SEQUENCE, generator = "USER_SEQ_GEN" )
private Long key;
/**
* Username the user will use to login. This should be an email address
*/
@Column( nullable=false, unique=true)
private String username;
// etc. other columns and getters/setters
}
DataObjectAbstract 是一个简单的 @MappedSuperClass
,它有一个 jpa 版本和 equals/hashcode 定义。
我有一个看起来像这样的基础 dao 类
public abstract class BaseDaoAbstract<T extends DataObject<K>, K extends Serializable>
implements BaseDao<T, K>
{
@PersistenceContext
private EntityManager em;
/**
* Save a new entity. If the entity has already been persisted, then merge
* should be called instead.
*
* @param entity The transient entity to be saved.
* @return The persisted transient entity.
*/
@Transactional
public T persist( T entity )
{
em.persist( entity );
return entity;
}
/**
* merge the changes in this detached object into the current persistent
* context and write through to the database. This should be called to save
* entities that already exist in the database.
*
* @param entity The entity to be merged
* @return The merged entity.
*/
@Transactional
public T merge( T entity )
{
return em.merge( entity );
}
// other methods like persist, delete, refresh, findByKey that all delegate to em.
}
我在 web.xml 中定义了 OpenEntityManagerInView 过滤器如下
<filter>
<filter-name>openEntityManagerInViewFilter</filter-name>
<filter-class>
org.springframework.orm.jpa.support.OpenEntityManagerInViewFilter
</filter-class>
<init-param>
<param-name>entityManagerFactoryBeanName</param-name>
<param-value>biEmf</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>openEntityManagerInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
我最近升级到 eclipselink 2.3.2 和 Spring 3.1 并从 CGLIB 代理转换为 Spring 使用 aspectJ 的 Load Time Weaving,但我没有为 eclipselink 配置 LTW。
问题出在这段代码中,它位于 spring ApplicationListener 中,请参阅注释。
User user = userService.findByKey(userDetails.getKey());
// THIS MERGE NEVER WRITES THROUGH TO THE DATABASE.
// THIS DOESN'T WORK AS PERSIST EITHER
user = userService.merge( user.loginSuccess() );
user.loginSuccess 只是设置一些字段并返回 this
我确定它正在通过代码,因为我得到了围绕它的日志语句,我可以设置一个断点并遍历它。我的 postgres 日志没有显示任何流量到达 postgres 以进行合并。
我在所有地方都保存了其他内容,包括更改密码时位于其他位置的用户,而且我知道这段代码曾经可以工作。这里有什么明显的不对吗?我是否错误地使用了 OpenEntityManagerInViewFilter?我是否需要使用@Transactional 方法才能将实体视为托管?任何帮助表示赞赏。
更新我按照prajeesh的建议尝试了冲洗。这是代码
@Transactional
public T merge( T entity )
{
entity = em.merge( entity );
em.flush();
return entity;
}
在 com.bi.data
的类中。我的 spring 应用程序配置文件中有这个
<context:component-scan base-package="com.bi.controller,com.bi.data,com.bi.web" />
在我的 spring 配置中,我有
<context:load-time-weaver/>
<tx:annotation-driven mode="aspectj"/>
使用如下所示的 aop.xml:
<aspectj>
<weaver>
<!-- only weave classes in our application-specific packages -->
<include within="com.bi..*"/>
</weaver>
</aspectj>
我得到了一个
javax.persistence.TransactionRequiredException:
Exception Description: No transaction is currently active
那么显然有些地方配置错误,但是什么?
更新 2:我恢复了我的更改以启用加载时间编织,现在合并在有或没有刷新的情况下进行,但我仍然不明白 LTW 的问题是什么......
最佳答案
在 em.merge()
之后也可以尝试 em.flush()
。有时 EntityManager
只是保留更改以供以后更新。
关于java - EntityManager.merge 没有做任何事情,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9073885/
有一些使用多个数据源的示例: @Inject @DataSource("users") AgroalDataSource dataSource1; @Inject @DataSource("inven
我试图使用持久性和 servlet guice 扩展,让简单的 webapp 在 Jetty 上与 Guice 和 JPA 一起工作。 我写了这个服务实现类: public class PersonS
我使用 Hibernate 5.1.0.Final、Guice、Jersey。我有创建 EntityManagerFactory 的 HibernateModule并管理EntityManager实例
我正在使用 ThreadLocal 和请求/实体模式来获取实体。这种情况发生时,我关闭了一个实体管理器,并且在后台有一些实体可以编辑、复制和修改,然后我想将其保留或与新的实体管理器合并。我不知道这是一
好的,我正在使用 EJB 3.0 和 hibernate,我们将 .ear 文件放入嵌入 Apache Tomcat 6.0.18 的 Easy-Beans 1.0.1(带有 Hibernate)部署
我正在使用 Spring + JPA + Hibernate + EntityManager 与数据库对话。我收到“A JTA EntityManager cannot use getTransact
更新数据库时我应该更喜欢什么?这两种方法的优缺点是什么?我什么时候应该使用其中一种? public void disemployEmployee(Integer employeeId, Date en
我正在尝试在存储库中注入(inject) EntityManager。 编译成功,但是当我运行应用程序并发送一个发布请求时,我收到了这个错误: Unexpected error occurred: F
我正在尝试在 Spring Tools Suite 和 Pivotal tc Server Developer Edition 上开发 Spring+Hibernate+EntityManager+S
我有一个使用 spring boot + spring data JPA 的示例项目。在日志中,我观察到 EntityManagers 在我进行第一次 rest 调用之前被创建了几次。请澄清为什么会发
给定网络应用程序中的以下情况: // EntityManager em, one per Request with Spring's OpenEntityManagerInViewFilter //
当我们在 JAVA EE 环境中的 EntityManager 上使用 @PersistenceContext 注释时,容器将创建 entityManagerFactory(我猜是整个 session
我遇到了一种情况(我认为这很奇怪,但可能很正常),我使用 EntityManager.getReference(LObj.getClass(), LObj.getId()) 来获取数据库实体,然后通过
我有以下服务... @Stateless @LocalBean public class RandomService { @EJB RandomString stringTokenizer;
我在实体类中有这个函数,但 getDoctrine 不喜欢...... public function getObject() { $em = $this->getDoctrine()->ge
我正在尝试以级联方式保存某个对象并检索它。 我有 3 个对象超过 3 个实体。 实体: class Order { /** * @var integer $id *
我正在开发一个 JPA 应用程序(使用 hibernate ),我正在与 作斗争。自动冲洗 特征。 默认情况下,每当我们处理对任何实体的查询时,完整的 实体管理器 被冲洗。在大多数情况下这是可以的:我
我刚刚建立了一个到目前为止仍然非常小的项目 maven/jpa/hibernate 项目,我试图在其中持久化一个对象。 我的类(class)是一个非常简单的类(class): @Entity publ
我当前的项目使用 HSQLDB2.0 和 JPA2.0 。 场景是:我查询数据库以获取 contactDetails 的列表的 person .我删单contactInfo在 UI 中,但不保存该数据
我是 jpa 和 spring 世界的新手,我目前正在对一个简单的方法进行一些单元测试,但只有当我在单元测试模式下运行我的测试类时才会继续收到此错误消息: java.lang.IllegalState
我是一名优秀的程序员,十分优秀!