Boa tarde, acabei de perceber que o erro está no manager sim, porém não entendo o porque ele insere os dados e não busca.
Classe de Configuração do JPA
package br.com.cdc.loja.dao;
import java.util.Properties;
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 JPAConfig {
@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
LocalContainerEntityManagerFactoryBean factoryBean = new LocalContainerEntityManagerFactoryBean();
JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
factoryBean.setJpaVendorAdapter(vendorAdapter);
DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setUsername("root");
ds.setPassword("");
ds.setUrl("jdbc:mysql://localhost:3306/casadocodigo");
ds.setDriverClassName("com.mysql.jdbc.Driver");
factoryBean.setDataSource(ds);
Properties prop = new Properties();
prop.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
prop.setProperty("hibernate.show_sql", "true");
prop.setProperty("hibernate.hbm2dll.auto", "update");
factoryBean.setJpaProperties(prop);
factoryBean.setPackagesToScan("br.com.cdc.loja.models");
return factoryBean;
}
@Bean
public JpaTransactionManager jpaTransactionManager(EntityManagerFactory emf) {
return new JpaTransactionManager(emf);
}
}
Classe do ProdutoDAO
package br.com.cdc.loja.dao;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import br.com.cdc.loja.models.Produto;
@Repository
@Transactional
public class ProdutoDAO {
@PersistenceContext
private EntityManager em;
public void gravar(Produto p) {
em.persist(p);
}
public List<Produto> listar() {
try {
Produto find = em.find(Produto.class, 1);
List<Produto> lista = new ArrayList<>();
lista.add(find);
em.getTransaction().commit();
return lista;
} catch (NullPointerException e) {
System.out.println(e.getMessage());
return new ArrayList<>();
}
}
}