Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9301907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T23:08:10+00:00 2026-06-18T23:08:10+00:00

I’ve been beating my head against a wall for awhile now trying to get

  • 0

I’ve been beating my head against a wall for awhile now trying to get this to work. I have created the following data access object:

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);
  }
}

I’m attempting to set this up with Java Configuration, so here is my configuration to setup my service layer:

@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());
  }

}

Here is the TradingService:

@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();
  }
}

The saving of a stock only seems to be completed if I add a session.flush() to the save method. The way I understand things, having the TransactionManager and the @Transactional around the service layer method should in fact cause that call to be made for me. What is this configuration missing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T23:08:11+00:00Added an answer on June 18, 2026 at 11:08 pm

    Because you are injecting a Session

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

    Spring cannot add it’s transactional behavior around it. Let Spring open the session and do it’s business.

    Instead of injecting a Session, inject a SessionFactory. In your DAO, keep a attribute for SessionFactory and use sessionFactory.getCurrentSession() to acquire a session.

    When Spring sees the @Transactional, it will get the SessionFactory, call openSession(), begin a transaction on it, then call your method. When your method returns successfully, it will close that transaction.

    You should also probably @Autowired the dao in your service class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have been unable to fix a problem with Java Unicode and encoding. The
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.