【问题标题】:I am not able to store entity using em.merge in broadleaf我无法在阔叶中使用 em.merge 存储实体
【发布时间】:2015-09-15 08:09:01
【问题描述】:

我是 broadleaf 应用程序的新手。我能够很好地使用 tomcat + mysql 集成运行应用程序。现在我想继续开发以根据我的要求自定义站点项目。

我在阔叶网站模块中坚持不懈。我尝试使用em.merge 返回我的实体,但不将其保存在数据库中,也尝试使用@Transactional(value="blTransactionManager"),但问题仍然存在。我在 applicationContext-servlet.xml

中尝试了以下代码
<aop:config>
    <aop:pointcut id="blMyStoreDao" expression="execution(* com.mycompany.dao.StoreDaoImpl.save*(..))"/>
    <aop:advisor advice-ref="blTxAdvice" pointcut-ref="blMyStoreDao"/>
    </aop:config>

这是我的控制器代码

newStore.setCustomer(customer);
    newStore.setProductList(new ArrayList<ProductImpl>());
    Store getStore=store.save(em, newStore);
    System.out.println(getStore.getCustomer().getUsername());
    System.out.println("customer fetched: "+customer.getEmailAddress());

这是我的 daoimpl 代码

 @Repository("blMyStoreDao")
    @Transactional(value="blTransactionManager")
    public class StoreDaoImpl implements StoreDao {

        @PersistenceContext(unitName="blPU")
        protected EntityManager em;



        @Transactional(value="blTransactionManager")
        public Store save(EntityManager em, Store store) {

            System.out.println(em);

            System.out.println(store.getCustomer().getUsername());
        Store s=  em.merge(store);
            return s;


        }


    }

但这也没有解决我的问题。

代码按应有的方式完美运行,但它没有将我的实体保存在数据库中。

任何人都可以帮助。提前致谢

【问题讨论】:

  • 你可以在 StoreDaoImpl 中的 em.merge(store) 之后尝试 em.flush() 并发布异常
  • 你到底为什么要传入EntityManager?您应该使用 dao 中的 EntityManager 实例,而不是您传入的实例。接下来您有 @Transactional,因此请确保您的配置中有 &lt;tx:annotation-driven /&gt;
  • 我也做了同样的事情,但它没有在 daoimpl 中注入 em。另一件事是关于@transsection。其中

标签: mysql spring hibernate jpa broadleaf-commerce


【解决方案1】:
  1. 没有任何理由使用&lt;aop:config&gt;,尤其是在applicationContext-servlet.xml 中(如果它应该在根应用程序上下文中)
  2. 您应该使用@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER 来注释您的方法
  3. Spring 可能没有扫描您的类。在 Broadleaf 中,applicationContext.xml 中设置了默认组件扫描以扫描 com.mycompany.core

我建议验证您的 dao 是否真的被 Spring 扫描并被初始化为 Spring bean。实体管理器没有被注入的事实表明它没有被 Spring 正确加载。验证这一点的一种方法是添加 @PostConstruct 方法并打印一些内容或设置断点以验证它是否被命中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多