gpt4 book ai didi

java - EntityManager.merge 没有做任何事情

转载 作者:IT老高 更新时间:2023-10-28 13:59:00 27 4
gpt4 key购买 nike

我有一个用户实体:

@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/

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