Hi!
All my DAOImplementations are @Repository's and extend the help class DAOSupport:
I suppose this is not the right way to open/start a Session.Code:@Transactional public abstract class DAOSupport { @Autowired protected HibernateTransactionManager _manager; // // RIGHT WAY? // openSession() ??? some one have to close the opened.... // private Session getSession() { Session session = _manager.getSessionFactory().getCurrentSession(); // <-- return session; } protected void defaultUpdate(Object object) { getSession().update(object); } protected Serializable defaultSave(Object object) { return getSession().save(object); } protected void defaultDelete(Object object) { getSession().delete(object); } protected <T> T defaultGetByID(Class<T> clazz, Serializable id) { return (T) getSession().get(clazz, id); } protected <T> List<T> defaultGetList(Class<T> clazz, Criterion... criterions) { Session session = getSession(); Criteria criteria = session.createCriteria(clazz); if (criterions != null) { for (Criterion c : criterions) { criteria.add(c); } } return criteria.list(); } @SuppressWarnings("unchecked") protected <T> T defaultGetUniqueResult(Class<T> clazz, Criterion... criterions) { Session session = getSession(); Criteria criteria = session.createCriteria(clazz); if (criterions != null) { for (Criterion c : criterions) { criteria.add(c); } } return (T) criteria.uniqueResult(); } }
If I use getCurrentSession(), i get an exception (in different @Service's):
If I use openSession(), i get exception:org.hibernate.LazyInitializationException: could not initialize proxy - no Session
My Configuration:org.hibernate.HibernateException: Illegal attempt to associate a collection with two open sessions
Would you give me some helpful advices?Code:@Configuration @EnableWebMvc @EnableTransactionManagement @ComponentScan(basePackages="myproject.blabla.*") public class AppConfiguration extends WebMvcConfigurerAdapter { @Bean public InternalResourceViewResolver getInternalResourceViewResolver() { InternalResourceViewResolver resolver = new InternalResourceViewResolver(); resolver.setPrefix("/WEB-INF/views/"); resolver.setSuffix(".jsp"); return resolver; } @Bean public DataSource myDataSource() { DriverManagerDataSource dataSource = new DriverManagerDataSource(); dataSource.setDriverClassName(Driver.class.getName()); dataSource.setUrl("jdbc:mysql://localhost/myproject"); dataSource.setUsername("root"); dataSource.setPassword(""); return dataSource; } @Bean public AnnotationSessionFactoryBean sessionFactory() { Properties props = new Properties(); props.put("hibernate.dialect", CustomMysqlDialect.class.getName()); props.put("hibernate.format_sql", "true"); props.put("hibernate.connection.charSet", "UTF-8"); props.put("hibernate.connection.useUnicode", "true"); props.put("hibernate.show_sql", "false"); props.put("hibernate.connection.characterEncoding", "UTF-8"); AnnotationSessionFactoryBean sessionFactory = new AnnotationSessionFactoryBean(); sessionFactory.setPackagesToScan(new String[] { "myproject.blabla.db.*" }); sessionFactory.setHibernateProperties(props); sessionFactory.setDataSource(myDataSource()); sessionFactory.setSchemaUpdate(true); return sessionFactory; } @Bean public HibernateTransactionManager transactionManager() { return new HibernateTransactionManager(sessionFactory().getObject()); } @Bean public ReloadableResourceBundleMessageSource messageSource() { ReloadableResourceBundleMessageSource msrc = new ReloadableResourceBundleMessageSource(); msrc.setBasename("/WEB-INF/lang/lang"); msrc.setCacheSeconds(0);// TODO return msrc; } @Override public void addInterceptors(InterceptorRegistry registry) { OpenSessionInViewInterceptor interceptor = new OpenSessionInViewInterceptor(); interceptor.setSessionFactory(transactionManager().getSessionFactory()); registry.addWebRequestInterceptor(interceptor); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } }
Thanks


Reply With Quote
