Spring MVC I Error in JPA EntityManager

0

Hello, the code below is giving error:

import javax.persistence.EntityManagerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.jpa.JpaTransactionManager;
import org.springframework.orm.jpa.JpaVendorAdapter;
import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@EnableTransactionManagement
public class JPAConfiguration {

    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactory()
    {
        LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();

        JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
        factoryBean.setJpaVendorAdapter(vendorAdapter);

        DriverManagerDataSource dataSource = new DriverManagerDataSource();
        dataSource.setUsername("root");
        dataSource.setPassword("");
        dataSource.setUrl("jdbc:mysql://localhost:3306/musify");
        dataSource.setDriverClassName("com.mysql.jdbc.Driver");
        factoryBean.setDataSource(dataSource);
    JPA Configuration:    
        Properties properties = new Properties();
        properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
        properties.setProperty("hibernate.show_sql", "true");
        properties.setProperty("hibernate.hbm2ddl.auto", "update");

        factoryBean.setJpaProperties(properties);
        factoryBean.setPackagesToScan("org.musify.models");

        return factoryBean;
    }

    @Bean
    public JpaTransactionManager transactionManager(EntityManagerFactory emf) {
        return new JpaTransactionManager(emf);
    }


}

*** DAO class

package org.musify.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.musify.models.Artista;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

@Repository
public class ArtistaDAO {

    @PersistenceContext
    private EntityManager manager;
    @Transactional
    public void gravar(Artista artista)
    { 
        manager.persist(artista);
    }
}

**** Controller

package org.musify.controllers;

import org.musify.dao.ArtistaDAO;
import org.musify.models.Artista;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
@Transactional
@Controller
public class ArtistasController {

    @Autowired
    private ArtistaDAO dao;
    @RequestMapping("/artistas/form")
    public String form() {
        return "artistas/form";
    }

    @RequestMapping("/artistas")
    public String gravar( Artista artista)
    {
        System.out.println(artista);
        dao.gravar(artista); 
        return "artistas/ok";
    }
}
  

Error creating bean with name 'internalResourceViewResolver' defined in class org.musify.conf.AppWebConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public org.springframework.web.servlet.view.InternalResourceViewResolver org.musify.conf.AppWebConfiguration.internalResourceViewResolver ()] threw exception; nested exception is org.springframework.transaction.CannotCreateTransactionException: Could not open JPA EntityManager for transaction; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: Could not open connection

    
asked by anonymous 18.11.2018 / 03:02

0 answers