org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [bean.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingNotFoundException: resource: com.newsDemo.po.Admin not foundAdmin.java@SuppressWarnings("serial")
@Entity
@Table(name="admin_table")
public class Admin {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Integer id;
@Column(name="user_name", length=30)
private String userName;
@Column(name="password", length=30)
private String password;
public Admin(){}

public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}

}
Bean.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: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-3.0.xsd
           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
           http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
           http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"
value="classpath:/hibernate.cfg.xml">
</property>
</bean> <bean id="txManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<tx:annotation-driven transaction-manager="txManager" /> <bean id="userService" class="com.newsDemo.service.impl.UsersServiceImpl"/>
<bean id="adminService" class="com.newsDemo.service.impl.AdminServiceImpl"/>
<bean id="newsService" class="com.newsDemo.service.impl.NewsServiceImpl"/>

<bean id="userDao" class="com.newsDemo.dao.impl.UsersDaoImpl"/>
<bean id="adminDapo" class="com.newsDemo.dao.impl.AdminDaoImpl"/>
<bean id="newsDao" class="com.newsDemo.dao.impl.NewsDaoImpl"/>

<bean id="beginningAction" class="com.newsDemo.action.BeginningAction"/>
<bean id="newsAction" class="com.newsDemo.action.NewsAction"/>
<bean id="adminAction" class="com.newsDemo.action.AdminAction"/></beans>
hibernate.cfg.xml
<?xml version="1.0" encoding="GBK"?>
<!-- 指定Hibernate配置文件的DTD信息 -->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- hibernate- configuration是连接配置文件的根元素 -->
<hibernate-configuration>
<session-factory>
<!-- 指定连接数据库所用的驱动 -->
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<!-- 指定连接数据库的url,hibernate连接的数据库名 -->
<property name="connection.url">jdbc:mysql://localhost:3306/News</property>
<!-- 指定连接数据库的用户名 -->
<property name="connection.username">root</property>
<!-- 指定连接数据库的密码 -->
<property name="connection.password">root</property>
<!-- 指定连接池里最大连接数 -->
<property name="hibernate.c3p0.max_size">20</property>
<!-- 指定连接池里最小连接数 -->
<property name="hibernate.c3p0.min_size">1</property>
<!-- 指定连接池里连接的超时时长 -->
<property name="hibernate.c3p0.timeout">5000</property>
<!-- 指定连接池里最大缓存多少个Statement对象 -->
<property name="hibernate.c3p0.max_statements">100</property>
<property name="hibernate.c3p0.idle_test_period">3000</property>
<property name="hibernate.c3p0.acquire_increment">2</property>
<property name="hibernate.c3p0.validate">true</property>
<!-- 指定数据库方言 -->
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 二级缓存
<property name="cache.use_second_level_cache">true</property>
<property name="cache.provider_class">org.hibernate.cache.OSCacheProvider</property>
 -->
<!-- 根据需要自动创建数据表 -->
<property name="hbm2ddl.auto">create</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="hibernate.format_sql">true</property>
<!-- 罗列所有的映射文件 -->

<mapping resource="com.newsDemo.po.Admin"/>
<mapping resource="com.newsDemo.po.News"/>
<mapping resource="com.newsDemo.po.NewClass"/>
<mapping resource="com.newsDemo.po.Users"/> </session-factory>
</hibernate-configuration>
问题的出现应该是
<mapping resource="com.newsDemo.po.Admin"/>
<mapping resource="com.newsDemo.po.News"/>
<mapping resource="com.newsDemo.po.NewClass"/>
<mapping resource="com.newsDemo.po.Users"/>但就是找不到哪里出错??求大侠!!!!

解决方案 »

  1.   

    用JPA得写成<mapping class="com.newsDemo.po.Admin"/> 亲哥
      

  2.   

    第一次导入hibernate功能时有个hibernate specipicatoin选项选中Enable hibernate annotations support ..
      

  3.   

    第一個xml中,<property name="configLocation"
                value="classpath:/hibernate.cfg.xml">
            </property>把 / 去掉。第二個xml中,把<mapping resource="com.newsDemo.po.Admin"/>
    <mapping resource="com.newsDemo.po.News"/>
    <mapping resource="com.newsDemo.po.NewClass"/>
    <mapping resource="com.newsDemo.po.Users"/>這種配置比較奇怪。既然你使用jpa基于注解的entity mapping,直接掃描package就可以了。
    而且,hibernate.cfg.xml可以拿掉。 datasource可以放到第一個xml中配置。
    給你推薦兩中方式:1、spring + jpa,用spring托管
    <context:property-placeholder location="classpath:jdbc.properties" />

    <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <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 bean="dataSource" />
    </property>

    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
    <prop key="hibernate.show_sql">true</prop>
    <prop key="hibernate.hbm2ddl.auto">validate</prop>
    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
    </props>
    </property>


    <property name="packagesToScan"> 
      <list>
        <value>com.pru.ekernel.example.domain.Visitor</value>
      <value>com.pru.ekernel.example.domain.Comment</value>
      <value>com.pru.ekernel.example.domain.Forum</value>
      <value>com.pru.ekernel.example.domain.Post</value>
      <value>com.pru.ekernel.example.domain.Topic</value>
      </list>
     </property>
     
    </bean>2、<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql://192.168.1.111:3306/comment_new"/>
            <property name="password" value="root" />
            <property name="username" value="root" />
        </bean>
    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingDirectoryLocations">
                <list>
                    <value>classpath:com/cocosily/comment/po</value>
                </list>
            </property>        <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.cache.use_query_cache">true</prop>
                    <prop key="hibernate.cache.use_second_level_cache">true</prop>
                </props>
            </property>
        </bean>
    還有很多中配置方式,你都可以試試。