下面那个代码是什么意思。SessionFactory是指哪个配置文件里的。
private final SessionFactory sessionFactory = getSessionFactory(); protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
我用的是SPRING 3.2 ,hibernate 4.1
(SessionFactory) //强转
new InitialContext()//工厂类
.lookup("SessionFactory");//返回的方法
按着ctrl键盘,然后左击.lookup("SessionFactory");这个方法。
看看是不是跳到另一个java文件里面,那么应该会标注着配置文件的地址,如果跳到的是一个class,那么就是默认方法,配置文件就是默认的。
工程里面配置文件应该不会太多吧。
.lookup("SessionFactory");
你看下spring配置文件里面有没有名字为“sessionfactory”的bean,这个好像是通过命名名称查找数据源的
这个好像读的是hibernate.cfg.xml这个配置文件的
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="SessionFactory">
<property name="hibernate.connection.autocommit">true</property>
<property name="hibernate.connection.driver_class">net.sourceforge.jtds.jdbc.Driver</property>
<property name="hibernate.connection.password">test</property>
<property name="hibernate.connection.url">jdbc:jtds:sqlserver://172.16.0.140:1433/soiii_soiii_db/td</property>
<property name="hibernate.connection.username">test</property>
<property name="hibernate.current_session_context_class">org.springframework.orm.hibernate4.SpringSessionContext</property>
<property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.search.autoregister_listeners">false</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<mapping resource="com/td/model/Bug.hbm.xml" />
<mapping resource="com/td/model/History.hbm.xml" />
</session-factory>
</hibernate-configuration>
现在把
private final SessionFactory sessionFactory = getSessionFactory();protected SessionFactory getSessionFactory() {
try {
return (SessionFactory) new InitialContext()
.lookup("SessionFactory");
} catch (Exception e) {
log.error("Could not locate SessionFactory in JNDI", e);
throw new IllegalStateException(
"Could not locate SessionFactory in JNDI");
}
}
这个换成
public SessionFactory getSessionFactory() {
return sessionFactory;
} public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}TOMACT 可以启动了,可是执行数据库操作时出现下面错误
2012-12-31 16:52:01,296 get failed
org.hibernate.HibernateException: No Session found for current thread
at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97)
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="current_session_context_class">thread</prop> </props> </property>
<property name="packagesToScan" value="com.bolo.examples.entity.*" />
</bean>
红色的
http://stackoverflow.com/questions/2475950/hibernate-sessionfactory-how-to-configure-jndi-in-tomcathttps://community.jboss.org/wiki/UsingJNDI-boundSessionFactorywithTomcat41