如何使用java配置使用hibernate和Jpa配置spring mvc 4

时间:2016-09-23 09:06:21

标签: hibernate spring-mvc spring-data-jpa

我试图使用java配置将spring-mvc 4和hibernate与jpa连接但是它无法正常工作可以告诉我错误或指导我使用新的配置设置

第一个配置类是 -

package com.codeoxy.web.config.servlet3;

   import   org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

 import com.codeoxy.web.config.SpringWebConfig;

 public class MyWebInitializer extends
    AbstractAnnotationConfigDispatcherServletInitializer {

  @Override
  protected Class<?>[] getServletConfigClasses() {
      return new Class[] { SpringWebConfig.class };
  }

   @Override
  protected String[] getServletMappings() {
      return new String[] { "/" };
 }

  @Override
  protected Class<?>[] getRootConfigClasses() {
      return null;
  }

  }

春天的第二课是

 package com.codeoxy.web.config;

 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.ComponentScan;
 import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

  @EnableWebMvc
  @Configuration
 @ComponentScan({ "com.codeoxy.web" })
 public class SpringWebConfig extends WebMvcConfigurerAdapter {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
}

@Bean
public InternalResourceViewResolver viewResolver() {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
  }

  }

和jpa配置文件是

  package com.codeoxy.web.config;

  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.dao.annotation.PersistenceExceptionTranslationPostProcessor;
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.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class PersistenceJPAConfig{

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
  LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
  em.setDataSource(dataSource());
  em.setPackagesToScan(new String[] { "org.baeldung.persistence.model" });

  JpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
  em.setJpaVendorAdapter(vendorAdapter);
  em.setJpaProperties(additionalProperties());

  return em;
  }

  @Bean
  public DataSource dataSource(){
  DriverManagerDataSource dataSource = new DriverManagerDataSource();
  dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  dataSource.setUrl("jdbc:mysql://localhost:3306/fitnestracker");
  dataSource.setUsername( "root" );
  dataSource.setPassword( "" );
  return dataSource;
  }

  @Bean
  public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
  JpaTransactionManager transactionManager = new JpaTransactionManager();
  transactionManager.setEntityManagerFactory(emf);

  return transactionManager;
  }

  @Bean
  public PersistenceExceptionTranslationPostProcessor exceptionTranslation(){
  return new PersistenceExceptionTranslationPostProcessor();
  }

  Properties additionalProperties() {
  Properties properties = new Properties();
  properties.setProperty("hibernate.hbm2ddl.auto", "create-drop");
  properties.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5Dialect");
  return properties;
   }
    }

任何人都可以帮助我,谢谢。

1 个答案:

答案 0 :(得分:0)

您的setPackagesToScan错误。请更新正确的pacakage。此外,您还没有提供错误日志,没有它就很难回答。

em.setPackagesToScan(new String [] {&#34; org.baeldung.persistence.model&#34;});