- This topic has 1 reply, 2 voices, and was last updated 18 years, 8 months ago by Haris Peco.
-
AuthorPosts
-
nealkatzMemberI 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
Haris PecoMemberNeal,
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 DAOIn 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 DAOIt’s possible that we will add support for pure hibernate (without JNDI or spring) in future releases
Best regards
-
AuthorPosts