Prefijo Sustantivo
Prefijo Sustantivo

Reputation: 525

simple spring jpa servlet example, crudrepository instance is null

H i, i am new to spring and what i am trying to do is create a simple application that does the following:

1- a simple crud on a simple oracle table, called Dummy, that has two fields: id, numeric and descripcion, a string

2 - the app only has a servlet. By invoking it the list of all Dummy rows are printed.

That's it. The problem is that in the servlet, my repository instance is always null.

Thanks

This is my code:

PersistenceContext

package jpa_spring.configuracion;

import java.util.Properties;

import javax.persistence.EntityManagerFactory;
import javax.sql.DataSource;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import com.zaxxer.hikari.HikariConfig;
import com.zaxxer.hikari.HikariDataSource;

@Configuration
@EnableJpaRepositories(basePackages = {"jpa_spring.dominio"})
@EnableTransactionManagement
public class PersistenceContext {
    @Bean(destroyMethod="close")
    DataSource dataSource(Environment env) {
        HikariConfig dataSourceConfig = new HikariConfig();
        dataSourceConfig.setDriverClassName(env.getRequiredProperty("db.driver"));
        dataSourceConfig.setJdbcUrl(env.getRequiredProperty("db.url"));
        dataSourceConfig.setUsername(env.getRequiredProperty("db.username"));
        dataSourceConfig.setPassword(env.getRequiredProperty("db.password"));

        return new HikariDataSource(dataSourceConfig);
    }

    @Bean
    LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource dataSource, Environment env) {
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
        entityManagerFactoryBean.setPackagesToScan("jpa_spring.dominio");

        Properties jpaProperties = new Properties();
        jpaProperties.put("hibernate.dialect", env.getRequiredProperty("hibernate.dialect"));    
        jpaProperties.put("hibernate.hbm2ddl.auto", env.getRequiredProperty("hibernate.hbm2ddl.auto")); 
        jpaProperties.put("hibernate.ejb.naming_strategy", env.getRequiredProperty("hibernate.ejb.naming_strategy") ); 
        jpaProperties.put("hibernate.show_sql", env.getRequiredProperty("hibernate.show_sql")); 
        jpaProperties.put("hibernate.format_sql", env.getRequiredProperty("hibernate.format_sql") );

        entityManagerFactoryBean.setJpaProperties(jpaProperties);
        return entityManagerFactoryBean;
    }

    @Bean
    JpaTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
        JpaTransactionManager transactionManager = new JpaTransactionManager();
        transactionManager.setEntityManagerFactory(entityManagerFactory);
        return transactionManager;

    }
}

Dummy

package jpa_spring.dominio;

import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="DUMMY")
public class Dummy {    
    private Long id;
    private String descripcion;

    public String getDescripcion() {
        return descripcion;
    }
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    @Id
    public Long getId() {
        return id;
    }
    public void setId(Long id) {
        this.id = id;
    }
}

RepositorioDummy

package jpa_spring.dominio;

import org.springframework.data.repository.CrudRepository;

public interface RepositorioDummy extends CrudRepository<Dummy, Long> {}

JPASpringServlet

package jpa_spring.servlet;

import java.io.IOException;
import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import jpa_spring.dominio.Dummy;
import jpa_spring.dominio.RepositorioDummy;

@Component
public class JPASpringServlet extends HttpServlet {
    @Autowired
    private  RepositorioDummy repositorio;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Iterator<Dummy> it = repositorio.findAll().iterator();
        while (it.hasNext()) {
            Dummy d = it.next();
            System.out.println(d.getDescripcion());
        }

    }
}

Upvotes: 0

Views: 1073

Answers (1)

Apokralipsa
Apokralipsa

Reputation: 2724

If you are using Spring MVC (and most likely you are), Spring already exposes a single servlet that will handle all http requests for you. As its role is to dispatch requests, it's called the DispatcherServlet. You can read more about it in the reference documentation. That servlet will then use a HandlerMapping to map incoming requests to methods that should be called.

What you should do instead of creating a servlet is to create a controller. You can then use @RequestMapping annotation that will be picked up by the RequestMappingHandlerMapping, an implementation of HandlerMapping.

Upvotes: 2

Related Questions