Quantcast
Channel: Spring Community Forums - Data
Viewing all articles
Browse latest Browse all 297

Spring data- rest: " Cannot create inner bean '(inner bean)'"

$
0
0
Hello:

I have a problem with a piece of software which uses Spring data-rest. When I try to access at the main URL of the program I get the next error message:

Code:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'repositoryFilm': Cannot create inner bean '(inner bean)' of type [org.springframework.orm.jpa.SharedEntityManagerCreator] while setting bean property 'entityManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '(inner bean)#1': Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public static javax.persistence.EntityManager org.springframework.orm.jpa.SharedEntityManagerCreator.createSharedEntityManager(javax.persistence.EntityManagerFactory)] threw exception; nested exception is java.lang.NullPointerException
I think the problem is in the entity Film which is used by the repository RepositoryFilm but I can not see where is the problem.

The code of the entity is this:

Code:

package org.springframework.data.rest.example;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table (name="peliculas")
public class Film implements Serializable {

        /**
        *
        */
        private static final long serialVersionUID = 1L;
       
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        @Column (name="idPeliculas")
        private Long id;
       
        @Column(name="Titulo")
        private String titulo;
       
        @Column(name="Duracion")
        private Long duracion;
       
        @Column(name="Tipo")
        private String tipo;
       
        @Column(name="Tipo")
        private String Cartel;
       
        @Column(name="Copias")
        private Long Copias;
       
        @Column(name="Directores_idDirectores")
        private Long idDirectores;
       
        public Film(){
                super();
        }

        public Long getId() {
                return id;
        }

        public void setId(Long id) {
                this.id = id;
        }

        public String getTitulo() {
                return titulo;
        }

        public void setTitulo(String titulo) {
                this.titulo = titulo;
        }

        public Long getDuracion() {
                return duracion;
        }

        public void setDuracion(Long duracion) {
                this.duracion = duracion;
        }

        public String getTipo() {
                return tipo;
        }

        public void setTipo(String tipo) {
                this.tipo = tipo;
        }

        public String getCartel() {
                return Cartel;
        }

        public void setCartel(String cartel) {
                Cartel = cartel;
        }

        public Long getCopias() {
                return Copias;
        }

        public void setCopias(Long copias) {
                Copias = copias;
        }

        public Long getIdDirectores() {
                return idDirectores;
        }

        public void setIdDirectores(Long idDirectores) {
                this.idDirectores = idDirectores;
        }
}

And the code of RepositoryFilm is this:

Code:

package org.springframework.data.rest.example;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.repository.annotation.RestResource;

@RestResource(path="film")
public interface RepositoryFilm extends PagingAndSortingRepository<Film, Long> {
       
        @RestResource(path="titulo")
        public List<Film> findByTitulo(@Param("titulo") String titulo);

}

I give you the configuration and the web initializer class, too:

Configuration class:
Code:

package org.springframework.data.rest.example;

import java.sql.Driver;

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.orm.jpa.JpaDialect;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.Database;
import org.springframework.orm.jpa.vendor.HibernateJpaDialect;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@Configuration
@ComponentScan(basePackageClasses = ApplicationConfig.class)
@EnableJpaRepositories
@EnableTransactionManagement
public class ApplicationConfig {

        private static final Logger LOG = LoggerFactory.getLogger(ApplicationConfig.class);
       
  @Bean public DataSource dataSource() {
//    EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
//    return builder.setType(EmbeddedDatabaseType.HSQL).build();
         
          LOG.debug("NNN Iniciamos la configuracion del data source");
          final SimpleDriverDataSource ds = new SimpleDriverDataSource();
         
      try {
              LOG.debug("NNN Antes de dar de alta el data source");
                ds.setDriverClass((Class<? extends Driver>) Class.forName("com.mysql.jdbc.Driver"));
                LOG.debug("NNN Despues de dar de alta el data source");
        } catch (ClassNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
        }
      ds.setUrl("jdbc:mysql://localhost/dbhaetoas");
      ds.setUsername("root");
      ds.setPassword("12qwerty");
      return ds;
 
  }

  @Bean public EntityManagerFactory entityManagerFactory() {
        LOG.debug("NNN Iniciamos la configuracion del EntityManagerFactory");
        HibernateJpaVendorAdapter vendorAdapter = null;
        LocalContainerEntityManagerFactoryBean factory = null;
       
        try{
                LOG.debug("NNN Antes de generar el vendorAdapter");
                vendorAdapter = new HibernateJpaVendorAdapter();
                vendorAdapter.setDatabase(Database.MYSQL);
                LOG.debug("NNN Despues de generar el vendorAdapter");

                LOG.debug("NNN Generamos el factory");
                factory = new LocalContainerEntityManagerFactoryBean();
                LOG.debug("NNN Inicializamos el vendorAdapter del factory");
                factory.setJpaVendorAdapter(vendorAdapter);
                LOG.debug("NNN Configuramos el escaneo de paquetes");
                factory.setPackagesToScan(getClass().getPackage().getName());
                LOG.debug("NNN Inicializamos el data source");
                factory.setDataSource(dataSource());

                factory.afterPropertiesSet();
        }catch (Exception e){
                e.printStackTrace();
        }

    return factory.getObject();
  }

  @Bean public JpaDialect jpaDialect() {
        LOG.debug("NNN Configuramos el dialecto de JPA");
    return new HibernateJpaDialect();
  }

  @Bean public PlatformTransactionManager transactionManager() {
        LOG.debug("NNN Configuramos el transaction manager");
    JpaTransactionManager txManager = new JpaTransactionManager();
    LOG.debug("NNN Configuramos el EntityManagerFactory del transaction manager");
    txManager.setEntityManagerFactory(entityManagerFactory());
    LOG.debug("NNN Devolvemos el transaction manager");
    return txManager;
  }

}

Web initializer:
Code:

package org.springframework.data.rest.example;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

import org.springframework.data.rest.webmvc.RepositoryRestExporterServlet;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;

public class RestExporterWebInitializer implements WebApplicationInitializer {

  @Override public void onStartup(ServletContext ctx) throws ServletException {

    AnnotationConfigWebApplicationContext rootCtx = new AnnotationConfigWebApplicationContext();
    rootCtx.register(ApplicationConfig.class);

    ctx.addListener(new ContextLoaderListener(rootCtx));

    RepositoryRestExporterServlet exporter = new RepositoryRestExporterServlet();

    ServletRegistration.Dynamic reg = ctx.addServlet("rest-exporter", exporter);
    reg.setLoadOnStartup(1);
    reg.addMapping("/*");

  }

}

I am rookie with Spring data-rest so I really need your help. Do you see where is the mistake?

Thank you in advance for you help

Viewing all articles
Browse latest Browse all 297

Trending Articles