Spring Data - 数据已加载但未保留

时间:2017-04-21 15:54:55

标签: java spring hibernate spring-data

我第一次尝试使用Spring Data。我已经为Spring创建了我的存储库接口来实现。我使用findAll()方法,效果很好。但是,save()delete()方法无法正常工作:没有数据被保留,但不会抛出任何异常。

我尝试通过Hibernate代码进行调试,并且我发现事件EntityDeleteAction[com.my.test.City#2]已添加到ActionQueue中,但未调用其execute()方法。我没有找到原因。

你能帮帮我吗?下面是数据库创建代码,java配置,我的实体和存储库代码:

CREATE TABLE cities(
  id SERIAL PRIMARY KEY,
  name VARCHAR(45) NOT NULL UNIQUE
);
INSERT INTO cities(id, name) VALUES (1, 'Moscow');
INSERT INTO cities(id, name) VALUES (2, 'St.Petersburg');

City.java

@Entity
@Table(name = "cities")
public class City {
    @Id @GeneratedValue(strategy = GenerationType.IDENTITY)
    public Long id;
    public String name;
}

CityRepository.java:

@Repository
public interface CityRepository extends CrudRepository<City, Long> {}

AppConfig.java(部分)

@EnableWebMvc
@Configuration
@ComponentScan(value = ...)
@EnableJpaRepositories("com.my.test.repository")
@Import(value = {
        SecurityConfig.class,
        WebMvcConfig.class
})
@PropertySource("classpath:db.properties")
@EnableTransactionManagement
public class AppConfig extends WebSecurityConfigurerAdapter

@Bean
DataSource dataSource() {
    DriverManagerDataSource dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName(env.getProperty("db.driver"));
    dataSource.setUrl(env.getProperty("db.url"));
    dataSource.setUsername(env.getProperty("db.username"));
    dataSource.setPassword(env.getProperty("db.password"));
    return dataSource;
}

@Bean
SessionFactory sessionFactory() {
    LocalSessionFactoryBuilder sessionBuilder = new LocalSessionFactoryBuilder(dataSource());
    sessionBuilder.scanPackages("com.my.test");
    sessionBuilder.addProperties(getHibernateProperties());
    return sessionBuilder.buildSessionFactory();
}

@Bean
PlatformTransactionManager transactionManager() {
    return new HibernateTransactionManager(sessionFactory());
}

@Bean
LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPackagesToScan(
            "com.my.test.repository",
            "com.my.test.entity"
    );
    em.setJpaVendorAdapter(new HibernateJpaVendorAdapter());
    return em;
}

private Properties getHibernateProperties() {
    Properties properties = new Properties();
    properties.put("hibernate.show_sql", env.getProperty("hibernate.show_sql"));
    properties.put("hibernate.dialect", env.getProperty("hibernate.dialect"));
    return properties;
}

方法I执行:

cityRepository.delete(2);

谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

正如M. Deinum所写,问题出在不正确的交易经理身上。这是解决方案:

删除bean sessionFactory()

替换豆子:

@Bean
public JpaTransactionManager transactionManager() {
    return new JpaTransactionManager(entityManagerFactory().getObject());
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
    em.setDataSource(dataSource());
    em.setPersistenceProviderClass(HibernatePersistenceProvider.class);
    em.setPackagesToScan(
            "com.my.test.repository",
            "com.my.test.entity"
    );
    em.setJpaProperties(getHibernateProperties());
    return em;
}