最近用STS搭建hibernate&spring,遇到Could not locate SessionFactory in JNDI.找到这个文章很快解决了
2012-11-10 10:36
507 查看
用HIBERNATE时出现如下错误:
ERROR - Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:27)
at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
at Test.main(Test.java:13)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:30)
at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
at Test.main(Test.java:13)
需要修改的两个地方:
protected SessionFactory getSessionFactory() {
try {
//return (SessionFactory) new InitialContext().lookup("SessionFactory");这句不能通过,会提示Could not locate SessionFactory in JNDIjavax.naming.NoInitialContextException:
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
修改为
return (SessionFactory) new Configuration().configure().buildSessionFactory();
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI"+e.toString());
}
}
public Tb1 findById( java.lang.String id) {
log.debug("getting Tb1 instance with id: " + id);
try {
System.out.println("test0....."+sessionFactory);
//Tb1 instance= (Tb1)sessionFactory.getCurrentSession().get(“com.sss.common”,id);//这句有问题。会提示No CurrentSessionContext configured!
修改为如下两句
Session session = sessionFactory.openSession();//先加入import org.hibernate.Session;
//Tb1 instance = (Tb1) session.load("com.sss.commmon.Tb1", id);这句也可以
Tb1 instance = (Tb1) session.get("com.sss.commmon.Tb1", id);
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
原网页链接http://gotosleep.blog.sohu.com/20756325.html
非常感谢作者!
ERROR - Could not locate SessionFactory in JNDI
javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)
at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)
at javax.naming.InitialContext.lookup(InitialContext.java:351)
at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:27)
at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
at Test.main(Test.java:13)
Exception in thread "main" java.lang.IllegalStateException: Could not locate SessionFactory in JNDI
at com.lmy.myblog.pojos.THostInfoHome.getSessionFactory(THostInfoHome.java:30)
at com.lmy.myblog.pojos.THostInfoHome.<init>(THostInfoHome.java:22)
at Test.main(Test.java:13)
需要修改的两个地方:
protected SessionFactory getSessionFactory() {
try {
//return (SessionFactory) new InitialContext().lookup("SessionFactory");这句不能通过,会提示Could not locate SessionFactory in JNDIjavax.naming.NoInitialContextException:
Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
修改为
return (SessionFactory) new Configuration().configure().buildSessionFactory();
}
catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException("Could not locate SessionFactory in JNDI"+e.toString());
}
}
public Tb1 findById( java.lang.String id) {
log.debug("getting Tb1 instance with id: " + id);
try {
System.out.println("test0....."+sessionFactory);
//Tb1 instance= (Tb1)sessionFactory.getCurrentSession().get(“com.sss.common”,id);//这句有问题。会提示No CurrentSessionContext configured!
修改为如下两句
Session session = sessionFactory.openSession();//先加入import org.hibernate.Session;
//Tb1 instance = (Tb1) session.load("com.sss.commmon.Tb1", id);这句也可以
Tb1 instance = (Tb1) session.get("com.sss.commmon.Tb1", id);
if (instance==null) {
log.debug("get successful, no instance found");
}
else {
log.debug("get successful, instance found");
}
return instance;
}
catch (RuntimeException re) {
log.error("get failed", re);
throw re;
}
原网页链接http://gotosleep.blog.sohu.com/20756325.html
非常感谢作者!
相关文章推荐
- Spring-hibernate开发时遇到 Could not obtain transaction-synchronized Session for current thread的问题解决
- Spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法 转载的
- spring注解空指针异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法
- Spring注解方式,异常 'sessionFactory' or 'hibernateTemplate' is required的解决方法 转载的
- 解决Hiberante延迟加载问题——could not initialize proxy - no Session & 简介Hibernate的持久化
- JTA多数据源 Hibernate3.9, Spring 3.1 Could not find UserTransaction in JNDI [java:comp/UserTransaction]
- Hibernate--------- Could not bind factory to JNDI
- 解决org.hibernate.LazyInitializationException: could not initialize proxy - no Session问题
- 常见Hibernate报错处理:出现“org.hibernate.QueryException: could not resolve property”和 is not mapped和could not locate named parameter错误的解决
- 解决Could not open Hibernate Session for transaction
- ssh2 整合 org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in Servl
- org.springframework.transaction.CannotCreateTransactionException: Could not open Hibernate Session
- 转:Could not commit Hibernate 问题,spring 事物 提交前session关闭问题
- 今天遇到了这样一个问题 Exception in thread "main" org.hibernate.LazyInitializationException: could not initiali
- 学Hibernate报:Exception in thread "main" org.hibernate.LazyInitializationException: could not initiali
- (xcode5.0 DEV target:iOS5.0) Could not find a storyboard named 'Main_iPhone.storyboard' in bundle,解决
- 出现org.hibernate.LazyInitializationException: could not initialize proxy - the owning Session was closed问题以及解决办法
- Spring使用OpenSessionInViewFilter解决Hibernate的lazy延时加载问题
- 配置Spring的OpenSessionInViewFilter过滤器,以解决Hibernate的懒加载异常(LazyInitializationException)
- dubbo序列化,hibernate.LazyInitializationException could not initialize proxy - no Session懒加载异常的解决 转