2

我一直在用头撞墙一段时间,现在试图让它发挥作用。我创建了以下数据访问对象:

public interface GenericDAO<T, ID extends Serializable> {
  T findById(ID id);
  List<T> findAll();
  T save(T entity);
  void update(T entity);
  void delete(T entity);
}

public class GenericHibernateDAO<T, ID extends Serializable> implements GenericDAO<T, ID> {

  private final Class<T> persistentClass;
  private final SessionFactory sessionFactory;

  public GenericHibernateDAO(final SessionFactory sessionFactory) {
    this.persistentClass = (Class<T>) ((ParameterizedType) getClass().getGenericSuperclass()).getActualTypeArguments()[0];
    this.sessionFactory = sessionFactory;
  }

  protected Session getSession() {
    return sessionFactory.getCurrentSession();
  }

  public Class<T> getPersistentClass() {
    return persistentClass;
  }

  @Override
  public T findById(final ID id) {
    return (T) getSession().load(getPersistentClass(), id);
  }

  @Override @SuppressWarnings("unchecked")
  public List<T> findAll() {
    return findByCriteria();
  }

  protected List<T> findByCriteria(final Criterion... criterion) {
    final Criteria crit = getSession().createCriteria(getPersistentClass());
    for (final Criterion c : criterion) {
      crit.add(c);
    }
    return crit.list();
  }

  @Override
  public T save(final T entity) {
    getSession().saveOrUpdate(entity);
    return entity;
  }

  @Override
  public void delete(final T entity) {
    getSession().delete(entity);
  }

  @Override
  public void update(final T entity) {
    getSession().saveOrUpdate(entity);
  }
}

@Repository
public class StockHibernateDAO extends GenericHibernateDAO<Stock, String> implements StockDAO {

  @Inject
  public StockHibernateDAO(final SessionFactory sessionFactory) {
    super(sessionFactory);
  }
}

我正在尝试使用 Java 配置进行设置,所以这是我设置服务层的配置:

@Configuration @Profile("hibernate")
@EnableCaching @EnableTransactionManagement
@ComponentScan("reference.dao.hibernate")
public class HibernateServiceConfig implements TransactionManagementConfigurer {

  @Inject private StockDAO stockDao; //No extra methods, just the base stuff for now

  @Bean(destroyMethod = "shutdown")
  public DataSource dataSource() {
    return new EmbeddedDatabaseBuilder().setType(EmbeddedDatabaseType.HSQL).addScript("classpath:schema.sql").build();
  }

  @Bean
  public SessionFactory sessionFactory() {
    return new LocalSessionFactoryBuilder(dataSource()).addAnnotatedClasses(Stock.class)
    .setProperty("hibernate.show_sql", "true")
    .setProperty("hibernate.cache.region.factory_class", "org.hibernate.cache.ehcache.EhCacheRegionFactory")
    .setProperty("hibernate.cache.use_query_cache", "true")
    .setProperty("hibernate.cache.use_second_level_cache", "true")
    .setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect").buildSessionFactory();
  }

  @Override @Bean
  public PlatformTransactionManager annotationDrivenTransactionManager() {
    return new HibernateTransactionManager(sessionFactory());
  }

}

这是交易服务:

@Service
public class TradingServiceImpl implements TradingService {    
  @Inject private StockDAO stockDAO;

  @Override @Transactional
  @CachePut(value = "stockCache", key = "#stock.name")
  public Stock addNewStock(final Stock stock) {
    stockDAO.save(stock);
    return stock;
  }

  @Override @Cacheable(value = "stockCache")
  public Stock getStock(final String stockName) {
    return stockDAO.findById(stockName);
  }

  @Override @CacheEvict(value = "stockCache", key = "#stock.name")
  public void removeStock(final Stock stock) {
    stockDAO.delete(stock);
  }

  @Override @CacheEvict(value = "stockCache", key = "#stock.name")
  public void updateStock(final Stock stock) {
    stockDAO.update(stock);
  }

  @Override
  public List<Stock> getAll() {
    return stockDAO.findAll();
  }
}

仅当我将 session.flush() 添加到 save 方法时,股票的保存似乎才完成。我理解事物的方式,在服务层方法周围使用 TransactionManager 和 @Transactional 实际上应该会导致为我进行调用。这个配置缺少什么?

4

1 回答 1

3

因为你正在注入一个Session

  @Bean
  public Session session() {
    return sessionFactory().openSession();
  }

Spring 不能在它周围添加它的事务行为。让 Spring 打开会话并完成它的业务。

与其注入 a Session,不如注入 a SessionFactory。在您的 DAO 中,保留一个用于SessionFactory获取sessionFactory.getCurrentSession()会话的属性。

当 Spring 看到 时@Transactional,它会得到SessionFactory, call openSession(),在上面开始一个事务,然后调用你的方法。当您的方法成功返回时,它将关闭该事务。

您还应该@Autowired在您的服务类中使用 dao。

于 2013-02-15T19:19:53.997 回答