facebook

Where does JNDI SessionFactory come from ? hibernate DAO

  1. MyEclipse Archived
  2.  > 
  3. Database Tools (DB Explorer, Hibernate, etc.)
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #251736 Reply

    nealkatz
    Member

    I am starting to use hibernate for the first time in my project. But I do not know how to ‘find’ the SessionFactory with JNDI

    pre-hibernate:
    I am currently using Tomcat, and use JNDI to find a DataSource,
    <Resource name=”jdbc/HotelDB” auth=”Container” type=”javax.sql.DataSource”
    maxActive=”100″ maxIdle=”30″ maxWait=”10000″
    username=”bob” password=”xxxx” driverClassName=”com.mysql.jdbc.Driver”
    url=”jdbc:mysql://zoomer.xxxx.com/hotel?autoReconnect=true&requireSSL=false”/>

    post-hibernate:
    The myeclipse wizard generated this code for the DAO object
    protected SessionFactory getSessionFactory() {
    try {
    return (SessionFactory) new InitialContext().lookup(“jdbc/HotelDB”);
    } catch (Exception e) {
    log.error(“Could not locate SessionFactory in JNDI”, e);
    throw new IllegalStateException(“Could not locate SessionFactory in JNDI”);
    }
    }

    The key “jdbc/HotelDB” is obviously wrong since this is a DataSource, Not a SessionFactory – but how do I create/define the session factory ?

    Is there documentation or a sample project showing Dao use within an appserver ?

    Thanks,
    Neal

    #251741 Reply

    Haris Peco
    Member

    Neal,

    You can’t make JNDI DAO with tomcat.Tomcat’s jndi support is constrainted (read-only) and it is very hard make session factory JNDI in tomcat (maybe impossible).
    You have 2 choices :

    – choose real JNDI implementation and make JNDI Dao (for example choose JBoss instead tomcat)
    – make spring DAO

    In first choice you have to make session factory JNDI on JBoss

    Second choice can work on tomcat and it’s probably simpler for you.You have to make hibernate.cfg.xml with your datasource (choose JNDI datasource) and set name java:comp/env/jdbc/HotelDB
    After this you add spring capabilities in project and choose this hibernate.cfg.xml for your session factory bean
    – make hibernate mappings with Spring DAO

    It’s possible that we will add support for pure hibernate (without JNDI or spring) in future releases

    Best regards

Viewing 2 posts - 1 through 2 (of 2 total)
Reply To: Where does JNDI SessionFactory come from ? hibernate DAO

You must be logged in to post in the forum log in