Announcement Announcement Module
No announcement yet.
SpringData vs EntityManager injection [NullPointerException] Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • SpringData vs EntityManager injection [NullPointerException]

    Im using spring data, witch is very good and all. But it fails me because I cant create a dinamic query
    (for filtering, in an datatable, por e.g). Soo, i need to get a reference of EntityManager to build my query.
    But i get nullPointerException!! Check out my config;

    	<!-- Autoscan for annotations -->
    	<context:component-scan base-package="" />
    	<jpa:repositories base-package="" />
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="emf" />
    	<bean id="dataSource"
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost/myfinancesweb" />
    		<property name="username" value="root" />
    		<property name="password" value="root" />
    	<bean id="emf"
    		<property name="dataSource" ref="dataSource" />
    		<property name="packagesToScan" value="" />
    		<property name="persistenceProvider">
    			<bean class="org.hibernate.ejb.HibernatePersistence" />
    		<property name="jpaProperties">
    				<entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
    				<entry key="" value="update" />

    Now here's how im tryin to inject it:

        private EntityManager entityManager;
        public void setEntityManager(EntityManager entityManager) {
            this. entityManager = entityManager;
        public GrupoController(GrupoService grupoService, MessageUtil messageUtil) throws Exception {
    	super(grupoService, Grupo.class,messageUtil);
    	lazyModel = new BaseLazyModelJPA<Grupo, Integer>(grupoService,entityManager, Grupo.class, messageUtil);

    and when I try to use it, in my BaseLazyModelJPA class:

        private EntityManager entityManager;
        public BaseLazyModelJPA(BaseService<T, PK> baseService, EntityManager entityManager, Class<T> entityClass, MessageUtil messageUtil) {
    	this.entityManager = entityManager;
    //this method is called by primefaces, in p:datatable.... but when it reaches the last line of the code block,
     // null pointer ;(
        public List<T> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) {
    	List<T> data = new ArrayList<T>();
    	String noneSelected = messageUtil.getMessageBundle("global.selecione");
    	// Criteria
    	CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();

    Can anybody help?