我无法在broadleaf中使用em.merge存储实体

时间:2015-09-14 03:30:06

标签: mysql spring hibernate jpa broadleaf-commerce

我是 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;


        }


    }

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

代码运行完美,但它不会将我的实体保存在数据库中。

任何人的帮助。在此先感谢

1 个答案:

答案 0 :(得分:0)

  1. 没有任何理由使用<aop:config>,尤其是在applicationContext-servlet.xml中(如果它应该位于根应用程序上下文中的任何地方)
  2. 您应该使用@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER注释您的方法
  3. 您的班级很可能没有被Spring扫描。在Broadleaf中,在applicationContext.xml中设置了默认组件扫描,以扫描com.mycompany.core
  4. 我建议验证您的dao实际上是由Spring扫描并初始化为Spring bean。实体管理器没有被注入的事实表明它没有被Spring正确加载。验证这种方法的一种方法是添加@PostConstruct方法并打印一些内容或设置断点以验证它是否被击中。

相关问题