spring整合hibernate时报错

bingfengfzl 2012-03-07 06:13:47
<?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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

<context:component-scan base-package="com.BeNnovo.EmployeesManagement"/>
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:conf/jdbc.properties</value>
</property>
</bean>

<bean id="dataSource" destroy-method="close"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>

<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan">
<list>
<value>com.BeNnovo.EmployeesManagement.coreCode.entity</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.SQLServerDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>

<aop:config>
<aop:pointcut id="bussinessService"
expression="execution(public * com.BeNnovo.EmployeesManagement.coreCode.service..*.*(..))" />
<aop:advisor pointcut-ref="bussinessService"
advice-ref="txAdvice" />
</aop:config>

<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="exists" read-only="true" />
<tx:method name="add*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>



<bean id="employeesDAO" class="com.BeNnovo.EmployeesManagement.coreCode.dao.employeesManagerment.impl.EmployeesDAO"></bean>
<bean id="employeesService" class="com.BeNnovo.EmployeesManagement.coreCode.service.employeesManagerment.impl.EmployeesService">
<property name="employeesDAO">
<ref local="employeesDAO"/>
</property>
</bean>
<bean id="emloyeesLeaveDAO" class="com.BeNnovo.EmployeesManagement.coreCode.dao.employeesManagerment.impl.EmloyeesLeaveDAO"></bean>
<bean id="emloyeesLeaveService" class="com.BeNnovo.EmployeesManagement.coreCode.service.employeesManagerment.impl.EmloyeesLeaveService">
<property name="emloyeesLeaveDAO">
<ref local="emloyeesLeaveDAO"/>
</property>
</bean>


</beans>




public class TestEmployeesService
{
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
private EmployeesService employeesService = (EmployeesService) context.getBean("employeesService");

private int page = 1;
private int rows = 10;
private long total;
private long records;


@Test
public void createEmployees()
{
EmployeesDTO employeesDTO = new EmployeesDTO();
employeesDTO.setEmployeesName("admin");
employeesDTO.setEmployeesPassword("admin");
employeesDTO.setEmployeesIdentityCard("123434");
employeesDTO.setEmpoyeesMaritalStatus(EmployeesMaritalStatus.MARRIED);
employeesDTO.setEmployeesSex(EmployeesSex.MAN);
for (int i = 0; i < 50; i++)
{
employeesDTO.setEmployeesCode("B"+i);
employeesService.createEmployees(employeesDTO);
}

}

public EmployeesService getEmployeesService()
{
return employeesService;
}
@Resource
public void setEmployeesService(EmployeesService employeesService)
{
this.employeesService = employeesService;
}

}





public class TemplateDAOImpl<T> implements TemplateDAO<T>{

private HibernateTemplate hibernateTemplate;

public void save(T entity) throws DataAccessException {
hibernateTemplate.save(entity);
}


public HibernateTemplate getHibernateTemplate()
{
return hibernateTemplate;
}
@Resource
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
this.hibernateTemplate = hibernateTemplate;
}
}



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'employeesDAO' defined in class path resource [applicationContext.xml]: Instantiation of bean failed; nested exception is java.lang.ExceptionInInitializerError
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:883)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:839)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:440)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ExceptionInInitializerError
at com.BeNnovo.EmployeesManagement.coreCode.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:21)
at com.BeNnovo.EmployeesManagement.coreCode.util.HibernateUtil.<clinit>(HibernateUtil.java:11)
at com.BeNnovo.EmployeesManagement.coreCode.dao.employeesManagerment.impl.EmployeesDAO.<clinit>(EmployeesDAO.java:21)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:525)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:100)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:61)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:877)
... 39 more
Caused by: org.hibernate.HibernateException: /hibernate.cfg.xml not found
at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170)
at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:1453)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1475)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1017)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:64)
at org.hibernate.cfg.Configuration.configure(Configuration.java:1462)
at org.hibernate.cfg.AnnotationConfiguration.configure(AnnotationConfiguration.java:1011)
at com.BeNnovo.EmployeesManagement.coreCode.util.HibernateUtil.buildSessionFactory(HibernateUtil.java:16)
... 48 more

整合后为什么还要去找那个/hibernate.cfg.xml
我使用的是xml方式,原来用的是annotation方式
...全文
240 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
bingfengfzl 2012-03-10
  • 打赏
  • 举报
回复
不懂,基本都是我们这些新人来管理所有技术,所以各位前辈帮帮忙啊。。。。
昨日凡阳 2012-03-10
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 bingfengfzl 的回复:]

不懂,基本都是我们这些新人来管理所有技术,所以各位前辈帮帮忙啊。。。。
[/Quote]

唉,那你跟老板说这些都是对牛弹琴。

只要你老板不吝惜代价,那么你可以整最贵最安全最稳定的框架。
bingfengfzl 2012-03-09
  • 打赏
  • 举报
回复
可是老板是个怪人,老是改方案,改来改去的,如果用注释的方式太不灵活了。。。。。。。
昨日凡阳 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 bingfengfzl 的回复:]

可是老板是个怪人,老是改方案,改来改去的,如果用注释的方式太不灵活了。。。。。。。
[/Quote]

你老板懂技术吗?
昨日凡阳 2012-03-08
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 bingfengfzl 的回复:]

我以前也是用annotation的方式,可是后来觉得他不好用了就换了xml的,可是换了就报错了。。。。。。。。我现在全用的都是xml的方式,没有用annnotation的方式。。。。为什么它老找hibernate.cfg.xml
[/Quote]

你既然全用xml方式配置,那麽你得entity為什麽又說是用annotation?

用xml配置,entity是需要hibernate.cfg.xml這個文件得。
bingfengfzl 2012-03-08
  • 打赏
  • 举报
回复
我以前也是用annotation的方式,可是后来觉得他不好用了就换了xml的,可是换了就报错了。。。。。。。。我现在全用的都是xml的方式,没有用annnotation的方式。。。。为什么它老找hibernate.cfg.xml
昨日凡阳 2012-03-08
  • 打赏
  • 举报
回复
你得配置怪怪。很亂。
給你一個,你參考一下。
最好是把dao,service,controller全用spring annotation配置。
要麽全部用xml,要麽全部用annotation。 特別是跟spring結合時候,將很多東西都交由spring來管理。


<context:annotation-config/>
<context:component-scan base-package="com.cybersoft4u.framework.domain"/>
<context:property-placeholder location="classpath:jdbc.properties"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</bean>


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="annotatedClasses">
<list>
<!-- BASE -->
<value>com.cybersoft4u.framework.domain.SequenceNumberControl</value>
<value>com.cybersoft4u.framework.domain.BaseParameterItemDef</value>
<!-- EMP -->
<value>com.cybersoft4u.framework.domain.emp.Department</value>
<value>com.cybersoft4u.framework.domain.org.Department</value>
<value>com.cybersoft4u.framework.domain.emp.Employee</value>
<value>com.cybersoft4u.framework.domain.org.Employee</value>
<!-- PAM -->
<value>com.cybersoft4u.framework.domain.lfs.pam.CalendarLevDay</value>
<value>com.cybersoft4u.framework.domain.lfs.pam.CalendarWeekEnd</value>
<value>com.cybersoft4u.framework.domain.lfs.pam.LeaveType</value>
<value>com.cybersoft4u.framework.domain.lfs.pam.ExtraLeave</value>
<value>com.cybersoft4u.framework.domain.lfs.pam.ManageAgent</value>
<!-- LEA -->
<value>com.cybersoft4u.framework.domain.lfs.lev.LeaveAppForm</value>
<value>com.cybersoft4u.framework.domain.lfs.lev.LeaveAppFormLog</value>
<!-- STA -->
<value>com.cybersoft4u.framework.domain.lfs.sta.DailyLog</value>
<!-- UMP -->
<value>com.cybersoft4u.framework.domain.upm.Function</value>
<value>com.cybersoft4u.framework.domain.upm.Role</value>
<value>com.cybersoft4u.framework.domain.upm.RoleRights</value>
<value>com.cybersoft4u.framework.domain.upm.UserRights</value>
<!-- PFM -->
<value>com.cybersoft4u.framework.domain.pfm.CRInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.FileUploadInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.LogDetailInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.LogInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.ProjectInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.ProjectUserInfo</value>
<value>com.cybersoft4u.framework.domain.pfm.WorksInfo</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="javax.persistence.validation.mode">none</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.OracleDialect</prop>
<prop key="hibernate.show_sql">false</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
<prop key="hibernate.connection.release_mode">auto</prop>
</props>
</property>
</bean>
<!-- 事務管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<tx:annotation-driven transaction-manager="transactionManager" />
<aop:config>
<aop:pointcut expression="execution(* com.cybersoft4u.framework.persistence..*.*(..)) and
execution(* com.cybersoft4u.framework.service..*.*(..))" id="allManagerMethod"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="allManagerMethod"/>
</aop:config>
bingfengfzl 2012-03-08
  • 打赏
  • 举报
回复
entity层我用的是annnotation方式,一起我用的spring 的annotation方式注入的,现在改成xml的就报错了。。。
昨日凡阳 2012-03-08
  • 打赏
  • 举报
回复
整合后为什么还要去找那个/hibernate.cfg.xml
我使用的是xml方式,原来用的是annotation方式

你現在用得xml方式,你得entity是如何配置得?你沒有用annotation,用得xml,那你得xml呢?
京郊-金手指 2012-03-08
  • 打赏
  • 举报
回复
这些个框架结构老是出现这种问题,解决方案:

一般配置问题,仔细检查所有配置是否正确;

重新发布运行,发布前清空所有以前发布的项目;

查看包是否正确,多了?少了?版本不符?
bingfengfzl 2012-03-08
  • 打赏
  • 举报
回复
没写错整合前还能测试通过整合后就不行了
wubin_feng 2012-03-08
  • 打赏
  • 举报
回复
其实还是使用注解的方式好一点,配置文件里面东西也少了,代码看起来也干净了。
bingfengfzl 2012-03-08
  • 打赏
  • 举报
回复
因为我以前在学校做过这样一个项目,spring用xml的,而hibernate是用annotation的,不过那是是老师帮我们把框架全部都搭好了的不用我们自己动手搭建。现在觉得spring还是用xml的方式比较好吧。。。,所以把spring改成了xml方式的了
彬彬13 2012-03-07
  • 打赏
  • 举报
回复
从测试方法那里,你是不是写错了employeeDAO,写成了employeeDTO

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧