EntityManager.merge 不执行任何操作

2022-09-03 13:46:23

我有一个用户实体:

@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 是一个简单的,它具有 jpa 版本和 equals/hashcode 定义。@MappedSuperClass

我有一个基础道类,看起来像这样

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中定义了OpenEntityManagerInView过滤器.xml如下所示

<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 代理转换为 Load Time Weaving with aspectJ for Spring,但我没有为 eclipselink 配置 LTW。

问题在于此代码,它位于弹簧应用程序管理器中,请参阅注释。

        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只是设置一些字段并返回我确信它正在通过代码,因为我得到了围绕它的日志语句,我可以设置一个断点并遍历它。我的postgres日志没有显示任何流量进入postgres进行合并。this

我正在毫无问题地保存其他东西,包括更改密码时在另一个位置的用户,我知道这个代码曾经工作过。这里有什么明显不对劲的地方吗?我是否错误地使用了 OpenEntityManagerInViewFilter?我是否需要采用@Transactional方法才能将实体视为托管实体?任何帮助是值得赞赏的。

更新我按照prajeesh的建议尝试了冲洗。这是代码

@Transactional
public T merge( T entity )
{
    entity = em.merge( entity );
    em.flush();
    return entity;
}

在 的类中。我在我的弹簧应用程序配置文件中有这个com.bi.data

<context:component-scan base-package="com.bi.controller,com.bi.data,com.bi.web" />

在我的弹簧配置中,我有

<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的问题是什么...


答案 1

也请尝试在 .有时,只是保留更改以供以后更新。em.flush()em.merge()EntityManager


答案 2

EntityManager 自动提交为 false。请使用如下交易;

EntityManager em = null;
EntityTransaction t = null;
try {
    em = emf.createEntityManager();
    t = em.getTransaction();
    t.begin();
    em.merge(myTestObject);
    t.commit();
} catch (Exception e) {
    t.rollback();
    throw new RuntimeException(e.getMessage());
}finally{
    if(em != null)
        em.close();
}

推荐