Hi,
I had been working to setup Hibernate 2.1.4 with Weblogic 8.1 and I have some doubts.
Hibernate In Action Section 2.4.2 says :
Code:
The SessionFactory will automatically bind itself to JNDI if the property hibernate.session_factory_name is set to the name of the directory node.
efender also seem to suggest something like that, but I could not make it work automatically.
I had a glance at the code and got the feel that the JNDI registration happens in
SessionFactoryObjectFactory.addInstance,which is called from the constructor of
SessionFactoryImpl. This constructor gets called from
Configuration.buildSessionFactory method.
Hence I ended up doing the following :
Code:
public class HibernatePOCEJB implements SessionBean
{
public static final String WLS_SESSION_FACTORY = "hibernate/SessionFactory";
private static Logger log = Logger.getLogger(HibernatePOCEJB.class);
public void fetchLoan(String loanId) throws RemoteException
{
Session session = openSession();
try
{
Loan loan = (Loan) session.load(Loan.class, Long.valueOf(loanId));
}
catch (HibernateException e)
{
throw new RemoteException("", e);
}
catch (NumberFormatException e)
{
throw new RemoteException("", e);
}
finally
{
try
{
if (session != null)
{
session.close();
}
}
catch (HibernateException e)
{
throw new RuntimeException("", e);
}
}
}
private Session openSession() throws RemoteException
{
SessionFactory sessions = null;
try
{
log.debug("Looking up SessionFactory in JNDI");
Context ctx = new InitialContext();
sessions = (SessionFactory) ctx.lookup(WLS_SESSION_FACTORY);
log.debug("Looked up SessionFactory in JNDI");
}
catch (NameNotFoundException e)
{
try
{
sessions = new Configuration().configure().buildSessionFactory();
log.debug("Could not find a SessionFactory in JNDI, hence created one");
}
catch (HibernateException e1)
{
throw new RemoteException("", e1);
}
}
catch (NamingException e)
{
throw new RemoteException("", e);
}
try {
log.debug("Opening new Hibernate Session");
return sessions.openSession();
}
catch (HibernateException e)
{
throw new RemoteException("", e);
}
}
}
Code:
<hibernate-configuration>
<session-factory name="hibernate.session-factory.Oracle">
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="jndi.url">t3://localhost:7001</property>
<!-- .. -->
</session-factory>
</hibernate-configuration>
This seems to work for me, although I havent tested this throughly. Kindly let me know if this is the recommended way (if there is one :) to use Hibernate with Weblogic.