java - Spring JPA和Hibernate在调用save方法时不将实体保存到数据库

标签 java spring hibernate spring-data-jpa

我在使用 Spring Data JPA 和 Hibernate 创建 Spring 项目时遇到问题。当我在存储库上调用 save 方法时,它没有将我的对象插入数据库。

Hibernate 配置类:

@Configuration
@EnableTransactionManagement
@ComponentScan({ "com.app.config" })
@PropertySource(value = { "classpath:application.properties" })
@EnableJpaRepositories(basePackages = { "com.app.repository" })
public class HibernateConfiguration {

  @Autowired
  private Environment environment;

  @Bean
  public LocalSessionFactoryBean sessionFactory() {
      LocalSessionFactoryBean sessionFactory = new   LocalSessionFactoryBean();
      sessionFactory.setDataSource(dataSource());
      sessionFactory.setPackagesToScan(new String[] { "com.app.model", "com.app.repository" });
      sessionFactory.setHibernateProperties(hibernateProperties());
      return sessionFactory;
  }

  @Bean
  public EntityManagerFactory entityManagerFactory() {
      HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
      vendorAdapter.setGenerateDdl(true);
      LocalContainerEntityManagerFactoryBean factory = new LocalContainerEntityManagerFactoryBean();
      factory.setJpaVendorAdapter(vendorAdapter);
      factory.setPackagesToScan(new String[] { "com.app.model", "com.app.repository" });
      factory.setDataSource(dataSource());
      factory.setJpaProperties(hibernateProperties());
      factory.afterPropertiesSet();
      return factory.getObject();
  }

  @Bean
  public DataSource dataSource() {
      DriverManagerDataSource dataSource = new DriverManagerDataSource();
      dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClassName"));
      dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
      dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
      dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
      return dataSource;
  }

  @Bean
  @Autowired
  public HibernateTemplate getHibernateTemplate(SessionFactory sessionFactory) {
      HibernateTemplate hibernateTemplate = new HibernateTemplate(sessionFactory);
      return hibernateTemplate;
  }

  private Properties hibernateProperties() {
      Properties properties = new Properties();
      properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
      properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
      properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
      properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
      return properties;
  }

  @Bean
  @Autowired
  public HibernateTransactionManager transactionManager(SessionFactory s) {
      HibernateTransactionManager txManager = new HibernateTransactionManager();
      txManager.setSessionFactory(s);
      return txManager;
  }

  @Bean
  @Autowired
  public EntityManager entityManager(EntityManagerFactory entityManagerFactory) {
      return entityManagerFactory.createEntityManager();
  }
}

存储库类:

@Repository
public interface CityRepository extends BaseRepository<City, Integer> {
  City findByName(String name);
  List<City> findAll();

  @Query("SELECT c.name FROM City c")
  List<String> findAllCityName();
}

服务等级:

@Service
public class CityService {

   @Autowired
   private CityRepository cityRepository;

   public void saveCity(City city) {
      return cityRepository.save(city);
  }
}

BaseRepository扩展为 CrudRepository在 Spring JPA 中。 上面的代码运行没有任何错误,但实体未保存到数据库中。有谁知道我的代码有什么问题吗?

最佳答案

我已经按照上面 JB Nizet 的回答解决了我的问题。

Drop the sessionFactory, drop the hibernateTemplate, replace the HibernateTransactionManager by a JpaTransactionManager. – JB Nizet

关于java - Spring JPA和Hibernate在调用save方法时不将实体保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47218279/

相关文章:

java - 使用 XML 的 Spring Integration 与使用 Java DSL 的 Spring Integration 有什么区别?

Hibernate 5.0.11 - 多次注册 AttributeConverter 类

java - Spring数据jpa deleteBy查询不起作用

java - 当我尝试从 java 程序导出可运行的 jar 时,SWT 库出现错误

java - 使用 JNDI 和 Hibernate 配置 Spring

java - Java SE/EE/ME 之间的区别?

java - 如何在 Spring Boot 应用程序中正确使用 ThreadPoolExecutor

java - 如何构建一个动态查询,添加迄今为止的天数并使用条件 API 将该日期与另一个日期进行比较?

java - Spring @Profile 和 Autowiring 匹配

java - BigDecimal除法问题