Friday, November 16, 2007

How to use TopLink along with JBoss

JBoss is bundled with Hibernate as the default JPA/EJB3 persistence implementation, like GlassFish which is bundled with TopLink essentials. Developers who are trying to use TopLink in an application which is to be deployed on JBoss may face with some difficulties.

Persistence implementation class is referenced in persistence.xml. Since Hibernate JPA implementation is referenced somewhere in persistence.xml in a META-INF folder visible in JBoss classpath, javax.persistence.Persistence finds at least two implementations: TopLink (which is deployed with the application, and you put persistence.xml in your classes/META-INF) and Hibernate. A call to EntityManagerFactory implementation constructor is done through reflection. If it returns null, Persistence tries with the next implementation found. The problem here is that a call to Hibernate's EntityManagerFactory cannot be done, since Hibernate classes (org.hibernate.*) are not visible in the current classloader's classpath, so your application fails to deploy with a ClassCastException for Hibernate's EntityManagerFactory implementation. To overcome this problem you need to do a small trick. Just copy hibernate-entitymanager.jar inside WEB-INF/lib folder, so that org.hibernate.ejb.HibernateEntityManagerFactory is visible in class path. Since all hibernate libraries are not available and also you specified another implementation (TopLink) in your META-INF/persistence.xml, Hibernate is bypassed and TopLink is used instead.

5 comments:

Saad said...

salam.

baz dar morede barname Zekr ye nazar dashtam. emkane jadide in barname pakhshe saoti ayat hast. vali bayad be internet vasl bood. ke baz be darde sor@e 28kb/s namihkore. man too computeram mp3 soorehaye quran ro az 2 qari daram. namishe inaro be barname khoroond ta baramoon pakhsh kone?

Mohsen Saboorian said...

Salaam,
rastesh emkanesh hast. dar soorati ke be soraye aye-be-aye darid kheili sade ast. vali age sure be sure bashe, ye zarre moshkele. fekr konam inja jaye monasebi baraye bahs dar moredesh nabashe. bizahmat ya be grooh mail bezanid ya be bande ta dar moredesh sohbat konim. tarjihan be grooh + englisi ta javabesh baraye baghie ham ghabele estefade bashe :)
http://siahe.com/zekr/contact.html

mamnoon.

Charlie said...

Thank you for this post. It helped me with that exact issue.

Sajid said...

Thank a million :-)

This problem was driving me crazy

Sajid

Youssef said...

hi, is this works for jboss 5.0.1 cause i cant get this damn thing to work.

thanks.