Hi Guys,
Got a solution to the above problem, so posting it.
Some modules in my application will use Entity bean and some will use Hibernate to communicate to peristant store. I am using CMT(Weblogic) and the task was to get this both working in a single transaction. Approach we used is :
1> Created a war and put it in our Application EAR which contains entities and other jar files. 2> Used ServletContextListener to initiate a class MySessionfactory. 3> Use this session factory in my CMT to get current session. 4> Now in a single transaction I can save entity beans as well as hibernate objects.
My CFG :
<!-- ****************************Datasource******************************** --> <property name="connection.username">weblogic</property> <property name="connection.password">weblogic</property> <!— weblogic console username and password -- > <property name="dialect">org.hibernate.dialect.OracleDialect</property> <property name="connection.datasource">myPool</property> <property name="hibernate.use_sql_comments">true</property>
<!-- Transaction settings --> <property name="transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property> <property name="transaction.factory.class">org.hibernate.transaction.CMTTransactionFactory</property > <property name ="jndi.class">weblogic.jndi.WLInitialContextFactory</property> <property name="current_session_context_class">jta</property> <property name="show_sql">true</property>
<mapping resource="Emp.hbm.xml"/>
------------------------------------------------------------------------------------------- MyServletContextListener code :
private static final SessionFactory sessionFactory; protected Session session = null; static { try { // Create the SessionFactory from hibernate.cfg.xml sessionFactory = new Configuration().configure().buildSessionFactory(); System.out.println("sessionFactory--->>>"+sessionFactory); } catch (Throwable ex) { System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } --------------------------------------------------------------------------------------------------------------- In a CMT :
Method: //Some other entity beans fetch and save code . . . SessionFactory sessionFactory = MyServletContextListener.getSessionFactory(); Session session = null; try { session = sessionFactory.getCurrentSession(); } catch (Exception he) { he.printStackTrace(); session = sessionFactory.openSession(); } Emp emp = new Emp ("DSS","Sach",new Date(),77.77); session.save(cd); session.flush(); -------------------------------------------------------------------------------------------- Here important thing is to use session.flush(), only then ur hibernate code will get committed. Don’t use session.close() here else the hibernate objects will not get persisted.
The above setting are working fine with : EJB2 and weblogic 10.3 and hibernate 4. Have a good time
Regards, Dacs_sach(Kolte S)
|