我是阔叶申请的新手。 我能够使用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.
更多推荐
发布评论