i have deployed one hibernate project in Jboss as and .har file
in the deploy folder i have two archive deploy |-Track.har | | | |-META-INF | | |-jboss-service.xml | | |-jboss-app.xml | | | |-Source | |-*.hbm.xml and corresponding .class file(Track.hbm.xml,Track.class) |-Track.war | |-WEB-INF | | |-web.xml,jboss-web.xml | | |-classes | | |-java files (*.class)that access sessionFactory |-JSP |-all jsp files
i have deployed my project as a war and har files i have written certain utility classes that lookup for the sessionFactory and create session and in that session execute the HQL. public static SessionFactory getHibernateSessionFactory(String JNDI_sessionFactory) throws NamingException { Context context=new InitialContext(); SessionFactory sessionFactory=(SessionFactory)context.lookup(JNDI_sessionFactory); return sessionFactory; } public static Session getSessionFromSessionFactory(String JNDI_sessionFactory) throws HibernateException, NamingException { Session session=(Session)ServiceLocator.getHibernateSessionFactory(JNDI_sessionFactory).openSession(); return session; } public List getTracks() { List trackList=null; try { Session session=ServiceLocator.getSessionFromSessionFactory("java:/hibernate/TrackSessionFactory"); Query query=session.createQuery("from source.Track"); trackList=query.list(); /* if (ServiceLocator.getSessionFromSessionFactory("java:/hibernate/TrackSessionFactory").createCriteria(source.Track.class) instanceof org.hibernate.impl.CriteriaImpl) { trackList=(org.hibernate.impl.CriteriaImpl)ServiceLocator.getSessionFromSessionFactory("java:/hibernate/TrackSessionFactory").createCriteria(source.Track.class); } */ } catch (HibernateException e) { e.printStackTrace(); } catch (NamingException e) { e.printStackTrace(); } return trackList; }
and these utility classes are in my Track.war folder i am using Eclipse as an IDE now when i debug above method getTracks() by inserting break points i get proper data in trackList that i am returning at the end.
i have called the above getTracks() method from a JSP but while casting the object to Track i get a classCast exception
EditTracks tracks=new EditTracks(); List listTracks=tracks.getTracks(); for(Iterator itr=listTracks.iterator();itr.hasNext();) { Track aTrack=new Track(); aTrack=(Track)itr.next();//Error over here System.out.println(aTrack.getTitle()); } now while accessing above code in a JSP file i get classCast exception. but when i restart the server things work fine as expected.(no exception) so i have 2 main questions over here (1)the way i have orgainsed my war and Har files, is it the best way to do it , or there are other ways to do it-actually i just read over net how to deploy hibernate in jboss and things worked for me so i did not think much about where to keep all hibernate *.hbm.xml file and there POJO's where to keep files for my web archive like JSP,css,HTML and java (2)Why do i get these class cast exception, and y it vanishes when i restart the server.
Thnaks in advanced.
|