Page 1 of 2
How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 2:46 am
by gowthamgutha
I am writing a utils class in the core module of the broadleaf commerce. Like,
com.mycompany.core.util.ProductUtils
to perform various operations that I need. I will be calling these methods in the thymeleaf templates. Now, I would like to get the entity manager instance into this ProductUtils class. How could I get that?
Code: Select all
// import statements omitted
public class ProductUtils
{
@PersistenceContext(unitName="blPU")
EntityManager em;
// ... methods
}
Do I need to do anything else, beside adding this class in the
persistence.xml in the
blPU section and adding it to the
application-entity.xml in the core module.
Thanks in advance. Hope you will reply as soon as possible.
Re: How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 10:27 am
by phillipuniverse
I would recommend against using the entity manager directly from Thymeleaf; it is usually better from a design perspective to wrap that in something like a dao (e.g. ProductDao).
That said, as of Thymeleaf 2.1 you can access Spring beans with the @<beanname> syntax. So within an expression you should be able to do "${@em.
Also, you do not add the ProductUtils class to persistence.xml. The only things that go inside of persistence.xml are entities (things that are mapped with @Entity). You probably want ProductUtils as a bean so that it can inject the EntityManager. For this, you can either add it to something like applicationContext.xml or annotate the class with @Service or @Component and ensure that the package is component scanned.
Re: How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 5:46 pm
by gowthamgutha
I annotated it with
@Repository() annotation and included the following in the
applicationContext-entity.xml file in the core module.
Code: Select all
<jpa:repositories base-package="com.mycompany.core.util" />
But, I don't know why, I am getting the following error..
the matching wildcard is strict but no declaration can be found for element sec:httpI have not touched
sec:http files at all. I don't know why am I getting this error suddenly? Also, the error is shown only while running the application but not when I open those xml files in the site directory.
No, particular xml file is stated in the log where the problem is, instead the directory containing all of the xml files in the site module is logged.
Thanks in advance. Hope you will reply as soon as possible.
Re: How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 7:21 pm
by phillipuniverse
Try not adding it to applicationContext-entity.xml and instead adding it to the applicationContext.xml file in core.
Re: How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 7:24 pm
by phillipuniverse
You also would have had to add the jpa XML namespace and schema location in order to get jpa:repositories to work. What did you add to do that?
That said, you should still add beans like that to applicationContext.xml and not applicationContext-entity.xml. ApplicationContext-entity.xml is designed to only be used for entity overrides. I am not 100% sure if it matters or not (as in, not sure if that specifically will give you problems) but your project should be structured like that nevertheless.
Re: How to get entity manager in whatever class I want?
Posted: Tue Jun 24, 2014 7:27 pm
by phillipuniverse
One final thing: you do not need to use the specific jpa-repositories. Since you annotated the class with @Repository, you just need '<context:component-scan base-package="com.mycompany.core.util" />'. You should not need to add any custom XML namespaces to get that to work.
Re: How to get entity manager in whatever class I want?
Posted: Wed Jun 25, 2014 2:24 am
by gowthamgutha
No, I am not getting that injected when I added context:component scan base-package in applicationContext.xml.
I also tried adding..
Code: Select all
<jpa:repositories base-package="com.mycompany.core.util"/>
and adding the namespaces
Code: Select all
http://www.springframework.org/schema/data/jpa
http://www.springframework.org/schema/data/jpa/spring-jpa-1.0.xsd
in applicationContext.xml file in the core module. Then, I am getting
Code: Select all
Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/data/jpa] : offending resource : byte array
Thanks in advance. Hope you will reply as soon as possible.
Re: How to get entity manager in whatever class I want?
Posted: Wed Jun 25, 2014 11:07 am
by phillipuniverse
You do not need to have a jpa:repositories. That was necessary in earlier versions of Spring but not anymore. This should be resolved with <context:component-scan>, assuming that you have annotated it with @Repository. This is exactly how things like ProductDaoImpl and CategoryDaoImpl work right now with no issue:
http://cl.ly/image/1Q1o3c2M261r.
If you are doing this in the 'core' project, it is possible that you did not do a 'mvn install' of the core project when restarting your site. You have to rebuild the core dependency jar in order for the 'site' project to include it in the built war.
Re: How to get entity manager in whatever class I want?
Posted: Thu Jun 26, 2014 8:10 am
by gowthamgutha
But I am using Jrebel, the classes should be re-loaded automatically including the spring context files.
Re: How to get entity manager in whatever class I want?
Posted: Thu Jun 26, 2014 11:11 am
by phillipuniverse
Those Spring context files will actually not be reloaded because of how Broadleaf works with merging application context files together. All of your Java class files, property files and servlet xml files (applicationContext-servlet.xml and applicationContext-admin-servlet.xml) but applicationContext files in the root application context (patchConfigLocations in web.xml) will not be reloaded by Jrebel.