Java InjectSessionFactory获取null
当我试图调用getSession()时,它返回null。我还在“application service.xml”中定义了sessionFactory的bean。我对冬眠一无所知,所以我想得到一些帮助。任何答案都非常感谢;) hibernate.cfg.xmlJava InjectSessionFactory获取null,java,hibernate,Java,Hibernate,当我试图调用getSession()时,它返回null。我还在“application service.xml”中定义了sessionFactory的bean。我对冬眠一无所知,所以我想得到一些帮助。任何答案都非常感谢;) hibernate.cfg.xml com.gtis.model ${hibernate.hbm2ddl.auto} ${hibernate.dial} ${hibernate.show_sql} application-context.xml 0 UTF-8 0.##
com.gtis.model
${hibernate.hbm2ddl.auto}
${hibernate.dial}
${hibernate.show_sql}
application-context.xml
0
UTF-8
0.##########
yyyy MM dd HH:MM:ss
真的
忽视
UserDao.java
公共类UserDao{
@自动连线
会话工厂会话工厂;
公共会话getSession(){
Session Session=sessionFactory.getCurrentSession();
返回会议;
}
公共用户getUser(字符串用户ID){
return(User)getSession().get(User.class,userId);
}
公共列表getAllUser(){
字符串hql=“from com.gtis.model.User”;
Query Query=getSession().createQuery(hql);
返回query.list();
}
公共作废保存(用户){
getSession().saveOrUpdate(用户);
}
公共作废删除(用户){
getSession().delete(用户);
}
}
application-service.xml
您的hibernate.cfg.xml不正确。请检查此链接:,它可能会帮助您非常感谢您的回答,失败的原因是我忘记在web.xml中使用OpenSessionInViewFilter,它会导致空指针,谢谢
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- Hibernate4 -->
<context:property-placeholder location="classpath:application.properties"/>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan">
<list>
<value>com.gtis.model</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<!-- <prop key="hibernate.current_session_context_class">thread</prop> -->
</props>
</property>
</bean>
<!-- class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" -->
<!-- class="org.springframework.jdbc.datasource.DriverManagerDataSource" -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pass}"/>
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- <bean id="persistenceExceptionTranslationPostProcessor"
class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" />-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="*" read-only="true"/>
</tx:attributes>
</tx:advice>
<aop:config expose-proxy="true">
<aop:pointcut id="txPointcut" expression="execution(* com.gtis.service.*.*(..))"/>
<aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
</aop:config>
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.gtis.*"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="contentType" value="text/html; charset=utf-8"/>
<property name="cache" value="true" />
<property name="suffix" value=".ftl" />
<property name="order" value="2"/>
</bean>
<bean id="freemarkerConfig"
class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/view/" />
<property name="freemarkerSettings">
<props>
<prop key="template_update_delay">0</prop>
<prop key="default_encoding">UTF-8</prop>
<prop key="number_format">0.##########</prop>
<prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
<prop key="classic_compatible">true</prop>
<prop key="template_exception_handler">ignore</prop>
</props>
</property>
</bean>
<import resource="app-service.xml"/>
<import resource="hibernate.cfg.xml"/>
</beans>
public class UserDao{
@Autowired
SessionFactory sessionFactory;
public Session getSession() {
Session session = sessionFactory.getCurrentSession();
return session;
}
public User getUser(String userId){
return (User)getSession().get(User.class,userId);
}
public List<User> getAllUser(){
String hql = "from com.gtis.model.User";
Query query = getSession().createQuery(hql);
return query.list();
}
public void save(User user){
getSession().saveOrUpdate(user);
}
public void delete(User user){
getSession().delete(user);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="userDao" class="com.gtis.Dao.UserDao" scope="prototype"/>
</beans>