错误信息:
Exception in thread "main" java.lang.IllegalArgumentException: No SessionFactory specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:283)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:470)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:405)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
at com.ourchr.channy.daoImp.UserDAO.findById(UserDAO.java:151)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy0.findById(Unknown Source)
at com.ourchr.channy.daoImp.UserDAO.main(UserDAO.java:163)applicationContext.xml
<?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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/study_h" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="15" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="find*" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoPointcut"
expression="execution(* com.ourchr.channy.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoPointcut" />
</aop:config> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">
</property>
<property name="mappingResources">
<list>
<value>com/ourchr/channy/entity/Directory.hbm.xml</value>
<value>com/ourchr/channy/entity/MyFile.hbm.xml</value>
<value>com/ourchr/channy/entity/User.hbm.xml</value>
<value>com/ourchr/channy/entity/Privilege.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userBO" class="com.ourchr.channy.bo.UserBO">
<property name="userDao" ref="userDAO"></property>
</bean>
<bean id="fileBO" class="com.ourchr.channy.bo.MyFileBO"></bean>
<bean id="dirBO" class="com.ourchr.channy.bo.MyDirectoryBO"></bean>
<bean id="privilegeBO" class="com.ourchr.channy.bo.MyPrivilegeBO"></bean>
<bean id="userDAO" class="com.ourchr.channy.daoImp.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="fileDAO" class="com.ourchr.channy.daoImp.MyFileDAO"></bean>
<bean id="dirDAO" class="com.ourchr.channy.daoImp.MyDirectoryDAO"></bean>
<bean id="privilegeDAO"
class="com.ourchr.channy.daoImp.PrivilegeDAO">
</bean>
</beans>UserDAO
package com.ourchr.channy.daoImp;import java.sql.SQLException;
import java.util.Date;
import java.util.List;import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;import com.ourchr.channy.dao.IUserDAO;
import com.ourchr.channy.entity.User;@SuppressWarnings("unchecked")
public class UserDAO extends BaseDAO<User> implements IUserDAO{
private static HibernateTemplate template;
public void setSessionFactory(SessionFactory sessionFactory) {
template = new HibernateTemplate();
} public UserDAO() {
super(User.class);
}
/*
* 将指定用户的最大存储空间修改为newSize
*/
public void updateStoreSize(User user,int newSize) {
Object[] values = new Object[2];
values[0] = newSize;
values[1] = user.getId();
template.find("update Privilege p set p.maxScoresize=? where p.id=?",values);
}
/*
* 删除用户,同时删除用户创建的所有文件夹及所有文件
*/
public void deleteById(Long id) {
template.find("delete from User u WHERE u.id=?",id); }
/*
* 根据用户id查询用户信息,同时查询出用户创建的文件夹
*/
public User findUserWithDirectories(Long id) {
User user = (User) template.find("FROM User u left join fetch u.directories WHERE u.id=?",id);
return user;
}
/*
* --查询账号已过期的用户
*/
public List<User> findExpiredUsers() {
List<User> users = template.find("FROM User u left join u.privilege up WHERE up.expiration<?",new Date());
return users;
}
/*
* --查询存储空间最大的用户信息
*/
public List<User> findHasMaxStoreSizeUser() {
List<User> users = template.find("FROM User u left join fetch u.privilege up WHERE up.maxScoresize=(SELECT max(p.maxScoresize) FROM Privilege p)");
return users;
}
public User findById(Long id){
User user=null;
List<User> users=template.find("from User u where u.id = ?",id);
if(users.size()!=0){
user=(User)(users.toArray()[0]);
}
return user;
}
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDAO dao = (IUserDAO) ac.getBean("userDAO");
dao.findById(1L);
}
}麻烦csdn的各位大侠们了!!!
Exception in thread "main" java.lang.IllegalArgumentException: No SessionFactory specified
at org.springframework.util.Assert.notNull(Assert.java:112)
at org.springframework.orm.hibernate3.SessionFactoryUtils.doGetSession(SessionFactoryUtils.java:283)
at org.springframework.orm.hibernate3.SessionFactoryUtils.getSession(SessionFactoryUtils.java:235)
at org.springframework.orm.hibernate3.HibernateTemplate.getSession(HibernateTemplate.java:470)
at org.springframework.orm.hibernate3.HibernateTemplate.doExecute(HibernateTemplate.java:405)
at org.springframework.orm.hibernate3.HibernateTemplate.executeWithNativeSession(HibernateTemplate.java:374)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:917)
at org.springframework.orm.hibernate3.HibernateTemplate.find(HibernateTemplate.java:913)
at com.ourchr.channy.daoImp.UserDAO.findById(UserDAO.java:151)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:310)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)
at $Proxy0.findById(Unknown Source)
at com.ourchr.channy.daoImp.UserDAO.main(UserDAO.java:163)applicationContext.xml
<?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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/study_h" />
<property name="user" value="root" />
<property name="password" value="root" />
<property name="minPoolSize" value="10" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="1800" />
<property name="acquireIncrement" value="2" />
<property name="maxStatements" value="0" />
<property name="initialPoolSize" value="15" />
<property name="idleConnectionTestPeriod" value="1800" />
<property name="acquireRetryAttempts" value="30" />
<property name="breakAfterAcquireFailure" value="true" />
<property name="testConnectionOnCheckout" value="false" />
</bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="delete*" propagation="REQUIRED"></tx:method>
<tx:method name="find*" read-only="true" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="save*" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="daoPointcut"
expression="execution(* com.ourchr.channy.dao.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="daoPointcut" />
</aop:config> <bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource">
</property>
<property name="mappingResources">
<list>
<value>com/ourchr/channy/entity/Directory.hbm.xml</value>
<value>com/ourchr/channy/entity/MyFile.hbm.xml</value>
<value>com/ourchr/channy/entity/User.hbm.xml</value>
<value>com/ourchr/channy/entity/Privilege.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userBO" class="com.ourchr.channy.bo.UserBO">
<property name="userDao" ref="userDAO"></property>
</bean>
<bean id="fileBO" class="com.ourchr.channy.bo.MyFileBO"></bean>
<bean id="dirBO" class="com.ourchr.channy.bo.MyDirectoryBO"></bean>
<bean id="privilegeBO" class="com.ourchr.channy.bo.MyPrivilegeBO"></bean>
<bean id="userDAO" class="com.ourchr.channy.daoImp.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="fileDAO" class="com.ourchr.channy.daoImp.MyFileDAO"></bean>
<bean id="dirDAO" class="com.ourchr.channy.daoImp.MyDirectoryDAO"></bean>
<bean id="privilegeDAO"
class="com.ourchr.channy.daoImp.PrivilegeDAO">
</bean>
</beans>UserDAO
package com.ourchr.channy.daoImp;import java.sql.SQLException;
import java.util.Date;
import java.util.List;import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.HibernateTemplate;import com.ourchr.channy.dao.IUserDAO;
import com.ourchr.channy.entity.User;@SuppressWarnings("unchecked")
public class UserDAO extends BaseDAO<User> implements IUserDAO{
private static HibernateTemplate template;
public void setSessionFactory(SessionFactory sessionFactory) {
template = new HibernateTemplate();
} public UserDAO() {
super(User.class);
}
/*
* 将指定用户的最大存储空间修改为newSize
*/
public void updateStoreSize(User user,int newSize) {
Object[] values = new Object[2];
values[0] = newSize;
values[1] = user.getId();
template.find("update Privilege p set p.maxScoresize=? where p.id=?",values);
}
/*
* 删除用户,同时删除用户创建的所有文件夹及所有文件
*/
public void deleteById(Long id) {
template.find("delete from User u WHERE u.id=?",id); }
/*
* 根据用户id查询用户信息,同时查询出用户创建的文件夹
*/
public User findUserWithDirectories(Long id) {
User user = (User) template.find("FROM User u left join fetch u.directories WHERE u.id=?",id);
return user;
}
/*
* --查询账号已过期的用户
*/
public List<User> findExpiredUsers() {
List<User> users = template.find("FROM User u left join u.privilege up WHERE up.expiration<?",new Date());
return users;
}
/*
* --查询存储空间最大的用户信息
*/
public List<User> findHasMaxStoreSizeUser() {
List<User> users = template.find("FROM User u left join fetch u.privilege up WHERE up.maxScoresize=(SELECT max(p.maxScoresize) FROM Privilege p)");
return users;
}
public User findById(Long id){
User user=null;
List<User> users=template.find("from User u where u.id = ?",id);
if(users.size()!=0){
user=(User)(users.toArray()[0]);
}
return user;
}
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
IUserDAO dao = (IUserDAO) ac.getBean("userDAO");
dao.findById(1L);
}
}麻烦csdn的各位大侠们了!!!
解决方案 »
- 用swing或awt实现查询智能提示的组件
- 请教一个 Regex 的写法
- 图像交点算法问题求助
- 制作bat文件运行java程序,怎么去掉后面的黑框啊?
- Reader/Writer 和input/output stream哪个更好?
- 极度简单,怎么在一个二维数组内插入一条记录
- JAVA里有没有像VB中那样的DoEvents语句来实现不让系统占用太多CPU资源。
- 所有的JFC对象都是线程不安全的,大家遇到过这方面的问题吗?
- 关于java访问MS Word的问题(请慎重发言,拒绝不负责任的评论)
- 各位大虾,为什么我的Tomcat启动不起来了?
- 非数据库操作,如果想做成类似事物,该如何做?
- HTMLunit 的一个警告提示引出的问题
public class UserDAO extends BaseDAO<User> implements IUserDAO{
private static HibernateTemplate template;
private SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
template = new HibernateTemplate();
}
还有就是,我加了default-autowire="byName"还是出了一样的错误
ps:我初学框架,不懂的很多,谢谢帮助!
template = new HibernateTemplate();这样 new 出来的 HibernateTemplate() 和你注入的 sessionFactory 没有一点关系啊
最起码要加上 template.setSessionFactory(sessionFactory);吧??