我无法在broadleaf中使用em.merge存储实体(I am not able to store entity using em.merge in broadleaf)

编程入门 行业动态 更新时间:2024-10-23 02:03:49
我无法在broadleaf中使用em.merge存储实体(I am not able to store entity using em.merge in broadleaf)

我是阔叶申请的新手。 我能够使用tomcat + mysql集成运行应用程序。 现在我想继续开发以根据我的要求定制站点项目。

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

<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; } }

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

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

任何人的帮助。 提前致谢

I am new to broadleaf application. I am able to run application using tomcat + mysql integration well. Now I want to move on with the development to customize the site project as per my requirement.

I am stuck on the point of persistant in broadleaf site module. I have tried using em.merge that returns my entity but do not save it in database and also tried @Transactional(value="blTransactionManager") but It still problem persists. I have tried bellow code in 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>

Here is my controller code

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());

Here is my daoimpl code

@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; } }

But it also didn't resolve my issue.

Code runs perfectly as it should be, but it doesn't save my entity in database.

Anybody Help. Thanks In advance

最满意答案

没有任何理由使用<aop:config>特别是在applicationContext-servlet.xml (如果它应该在根应用程序上下文中的任何地方) 你应该使用@Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER来注释你的方法) 很可能你的班级没有被Spring扫描。 在Broadleaf中,在applicationContext.xml设置了默认组件扫描以扫描com.mycompany.core 。

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

There isn't any reason to use <aop:config> especially in applicationContext-servlet.xml (if anywhere it should be in the root application context) You should use @Transactional(TransactionUtils.DEFAULT_TRANSACTION_MANAGER to annotate your method It is likely that your class was not being scanned by Spring. In Broadleaf, there is a default component scan set up in applicationContext.xml to scan com.mycompany.core.

I would recommend verifying that your dao is actually scanned by Spring and is initialized as a Spring bean. The fact that the entity manager did not get injected indicates that it did not get loaded by Spring correctly. One way to verify this would be to add an @PostConstruct method and print something or set a breakpoint to verify that it gets hit.

更多推荐

本文发布于:2023-07-15 18:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1117531.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:实体   em   broadleaf   merge   entity

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!