Здравствуйте, помогите разобраться в ошибке. Версия hibernate 4.3.11.Final. Glassfish 4.1.0
Вот лог ошибки:
com.sun.faces.mgbean.ManagedBeanCreationException: Cant instantiate class: com.alisher.controllers.AuthorController.
.....
Caused by: org.hibernate.HibernateException: createCriteria is not valid without active transaction
at org.hibernate.context.internal.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:352)
at com.sun.proxy.$Proxy213.createCriteria(Unknown Source)
at com.alisher.db.DataHelper.getAllAuthors(DataHelper.java:43)
at com.alisher.controllers.AuthorController.<init>(AuthorController.java:31)
HibernateUtil.java
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
private static SessionFactory confSessionFactory() throws HibernateException {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
public static SessionFactory getSessionFactory() {
return confSessionFactory();
}
}
DataHelper.java
public class DataHelper {
private SessionFactory sessionFactory = null;
private static DataHelper dataHelper;
public DataHelper() {
sessionFactory = HibernateUtil.getSessionFactory();
}
public static DataHelper getInstance(){
return dataHelper == null ? new DataHelper() : dataHelper;
}
private Session getSession(){
return sessionFactory.getCurrentSession();
}
public List<Author> getAllAuthors() {
return getSession().createCriteria(Author.class).list();
}
}
AuthorContoller.java
@ManagedBean(eager = false)
@ApplicationScoped
public class AuthorController implements Serializable, Converter {
private List<SelectItem> selectItems = new ArrayList<SelectItem>();
private Map<Long,Author> authorMap;
public AuthorController() {
authorMap = new HashMap<Long, Author>();
for (Author author : DataHelper.getInstance().getAllAuthors()) {
authorMap.put(author.getId(), author);
selectItems.add(new SelectItem(author, author.getFio()));
}
}
public List<SelectItem> getSelectItems() {
return selectItems;
}
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
return authorMap.get(Long.valueOf(value));
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object o) {
Long id = ((Author)o).getId();
return String.valueOf(id);
}
}
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.url"></property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<!-- Mysql Dialect-->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.datasource">jdbc/Test</property>
<property name="hibernate.current_session_context_class">thread</property>
<!-- Mapping files -->
....
</session-factory>
</hibernate-configuration>
HibernateSession.java
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain)
throws IOException, ServletException {
RequestWrapper wrappedRequest = new RequestWrapper((HttpServletRequest) request);
ResponseWrapper wrappedResponse = new ResponseWrapper((HttpServletResponse) response);
System.out.println("open session for " + wrappedRequest.getRequestURI());
sessionFactory.getCurrentSession().beginTransaction();
try {
if (debug) {
log("CheckSessionFilter:doFilter()");
}
doBeforeProcessing(wrappedRequest, wrappedResponse);
chain.doFilter(wrappedRequest, wrappedResponse);
Throwable problem = null;
doAfterProcessing(wrappedRequest, wrappedResponse);
sessionFactory.getCurrentSession().getTransaction().commit();
System.out.println("close session for " + wrappedRequest.getRequestURI());
// If there was a problem, we want to rethrow it if it is
// a known type, otherwise log it.
if (problem != null) {
if (problem instanceof ServletException) {
throw (ServletException) problem;
}
if (problem instanceof IOException) {
throw (IOException) problem;
}
sendProcessingError(problem, response);
}
} catch (Exception e) {
e.printStackTrace();
if (sessionFactory.getCurrentSession().getTransaction().isActive()) {
sessionFactory.getCurrentSession().getTransaction().rollback();
}
}
}