0 Replies Latest reply on May 25, 2005 12:43 PM by fmaredia

    JBoss 4.0.2, EJB Preview 5, and Hibernate 3 : Making it all

    fmaredia

      This is to inform anyone that was having difficulty getting Hibernate 3 to work with JBoss 4.02 with EJB Preview 5.

      The following needs to be done at the EJB Layer:

      @Unchecked
       @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
       public Employee login() {
       EmployeeLogonDAO eldao = new EmployeeLogonDAO();
       Employee emp = getEmployee();
       try {
       if (emp != null) {
       //Log the employee
       EmployeeLogon elogon = new EmployeeLogon();
       elogon.setEmployeeID(emp);
       elogon.setLogontime(new Date());
       eldao.save(elogon);
       }
       }
       catch (Exception e) {
       log.error(e.getMessage());
       }
       return emp;
       }


      At the DAO level there is an inner class that has the following:
      
       public static final ThreadLocal session = new ThreadLocal();
      
       public static Session currentSession()
       throws HibernateException {
      
       Session s = (Session) session.get();
       if (s == null) {
       try {
       SessionFactory sf = (SessionFactory) new InitialContext().lookup("java:hibernate/HibernateFactory");
       s = sf.openSession();
       s.setFlushMode(FlushMode.AUTO);
       session.set(s);
       }
       catch (Exception e) {
       e.printStackTrace();
       }
       }
      
       return s;
       }
      
       public static void closeSession() throws HibernateException {
       Session s = (Session) session.get();
       s.flush();
       session.set(null);
       if (s != null) s.close();
       }


      This finally made it work where the updates and deletes were commited. I had to explicitly call s.setFlushMode(FlushMode.AUTO); and s.flush() to ensure that everything works.