错误信息:
Name hibernate is not bound in this ContextContext导入的是javax.naming.Context;
在网上查了资料,说是没有绑定JNDI,可我就是按孙卫琴的<精通hibernate>一步一步来的,而且也在网上查了大部分资料,大体就是这个步骤,为什么呢?如果tomcat不配置数据源,用hibernate自己的链接机制,也可以正常运行
如果配置tomcat数据源,而不用hibernate技术,也没问题
只要tomcat数据源与hiberate连用就了这问题,天哪我该怎么办?
有人说如果要用tomcat数据源,hibernate必须搭配spring技术取得数据源
我晕,我想hibernate不至于这么差吧?
Name hibernate is not bound in this ContextContext导入的是javax.naming.Context;
在网上查了资料,说是没有绑定JNDI,可我就是按孙卫琴的<精通hibernate>一步一步来的,而且也在网上查了大部分资料,大体就是这个步骤,为什么呢?如果tomcat不配置数据源,用hibernate自己的链接机制,也可以正常运行
如果配置tomcat数据源,而不用hibernate技术,也没问题
只要tomcat数据源与hiberate连用就了这问题,天哪我该怎么办?
有人说如果要用tomcat数据源,hibernate必须搭配spring技术取得数据源
我晕,我想hibernate不至于这么差吧?
<resource-ref>
<description>connectDB test</description>
<res-ref-name>jdbc/hibernate</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
难道就没有人用过hibernate?都是与spring联合使用??????????????????
String jndiName = "java:/hibernate/HibernateFactory";
sf = (SessionFactory)ctx.lookup(jndiName);
session = sf.openSession();Hiberbate里面session不是这样取的,Session s=new Configuration().configure("hibernate.cfg.xml 为止").buildSessionFactory().openSession();
hibernate里面不用象上面这么麻烦
首先是初始化类,通过读取配置文件实例化hibernate.cfg.xml文件,并绑定到JNDI中ctx.bind("HibernateSessionFactory", sessionFactory);:public class HibernateInit implements PlugIn{ private Context ctx;
private SessionFactory sessionFactory;
private String hbnConfigFile; public void init(ActionServlet servlet, ModuleConfig config) throws ServletException{
try{
Configuration cfg = new Configuration();
cfg.configure(this.getHbnConfigFile());
sessionFactory = cfg.buildSessionFactory();
}catch(HibernateException ex){
throw new RuntimeException("Exception building SessionFactory: " + ex.getMessage(), ex);
}
try{
ctx = new InitialContext();
ctx.bind("HibernateSessionFactory", sessionFactory);
}catch(NamingException ex){
throw new RuntimeException("Exception building SessionFactory22: " + ex.getMessage(), ex);
}
} public void destroy(){
if(ctx!=null){
try{
ctx.unbind("HibernateSessionFactory");
}catch(NamingException e){
e.printStackTrace();
}
}
if(sessionFactory!=null){
try{
sessionFactory.close();
}catch(HibernateException e){
e.printStackTrace();
}
sessionFactory = null;
}
} public void setHbnConfigFile(String hbnConfigFile){
this.hbnConfigFile = hbnConfigFile;
}
public String getHbnConfigFile(){
return hbnConfigFile;
}}接着就在工具类中使用该JNDI。public class DButil{ private static SessionFactory sessionFactory = null;
public static final ThreadLocal session = new ThreadLocal(); public static Session currentSession() throws HibernateException{
if(sessionFactory == null){
if(getSystemSessionFactory() == false){
throw new HibernateException("Exception geting SessionFactory from JNDI");
}
} Session s = (Session)session.get();
if(s == null){
s = sessionFactory.openSession();
session.set(s);
} return s;
} public static void closeSession() throws HibernateException{
Session s = (Session)session.get();
session.set(null);
if(s!=null){
s.close();
}
} public static boolean getSystemSessionFactory(){
try{
Context ctx = new InitialContext();
sessionFactory = (SessionFactory)ctx.lookup("HibernateSessionFactory");
}catch(NamingException e){
return false;
}
return true;
}}
这里就通过语句sessionFactory = (SessionFactory)ctx.lookup("HibernateSessionFactory");获得sessionfactory。
Session session = DButil.currentSession();
Transaction tx = session.beginTransaction(); Criteria criteria = session.createCriteria(User.class);
criteria.add(Expression.like("name", userName));
List result = criteria.list(); tx.commit();
DButil.closeSession();
还是不行,报这个错误:Could not find datasource
User.class
可我的配制文件为.xml格式的
2.web.xml
这个你可以在jsp页面里试一下
Context ctx = new InitialContext();
String jndiName = "java:comp/env/jdbc/UserDB";
DataSource ds = (DataSource)ctx.lookup(jndiName);
Connection conn = ds.getConnection();
ResultSet rs = conn.executeQuery("select sysdate()");
if(rs.next()){
out.println(rs.getString(1));
}这一步通过了再试下一步
我在jsp页调试成功
不用hibernate调试成功
用hibernate自带链接池机制也调试成功
就是用tomcat链接池时,再用hibernate读tomcat数据源时就报错
框架struts+hibernate
3.hibernate.cfg.xml
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="connection.datasource">java:comp/env/jdbc/UserDB</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
<property name="transaction.manager_lookup_class">org.hibernate.transaction.JbossTransactionManageLookup</property>
<property name="show_sql">true</property><mapping resource="user/UserBean.hbm.xml"/></session-factory>4.bean:
SessionFactory sf = new Configuration().configure().buildSessionFactory();
session = sf.openSession();试一下
试着调试好像不对
你用tomcat admin 那个图形化的东东里面配置一下datasource,看看和你手动写的配置
放置的位置是不是一样就知道了
如果不用hibernate可以取到数据源,且能正常运行