org.springframework.beans.factory.BeanCreationException:创建类路径资源[jlcindia.xml]中定义的名为'sessionFactory'的bean时出错

时间:2014-05-30 10:03:44

标签: java spring hibernate

当我运行我的应用程序时,我得到以下例外:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException:
  Error creating bean with name 'sessionFactory' defined in class path resource [jlcindia.xml]:
  Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException:
  Could not parse mapping document from invalid mapping
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1420)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
  at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:291)
  at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:288)
  at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:190)
  at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:563)
  at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:895)
  at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:425)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
  at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
  at com.jlcindia.spring.hibernate.LAB27.main(LAB27.java:13)
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from invalid mapping
  at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:502)
  at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:677)
  at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
  at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)
  ... 12 more
Caused by: org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 19; Document is invalid: no grammar found.
  at org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(Unknown Source)
  at org.apache.xerces.util.ErrorHandlerWrapper.error(Unknown Source) at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
  at org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
  at org.apache.xerces.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
  at org.apache.xerces.impl.XMLNSDocumentScannerImpl$NSContentDispatcher.scanRootElementHook(Unknown Source)
  at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
  at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
  at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
  at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
  at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
  at org.dom4j.io.SAXReader.read(SAXReader.java:465)
  at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)
  ... 16 more

这是jlcindia.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
 xmlns:context="http://www.springframework.org/schema/context"
 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-3.0.xsd
 http://www.springframework.org/schema/context
 http://www.springframework.org/schema/context/spring-context-3.0.xsd">

  <context:annotation-config/>
  <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/jlcindiadb"/> 
    <property name="username" value="root"/>
    <property name="password" value="santhosh"/>
  </bean> 

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean" p:dataSource-ref="dataSource"> 
    <property name="mappingResources">
      <list> 
        <value>com/jlcindia/spring/hibernate/Customer.hbm.xml</value>
      </list>
    </property> 
    <property name="hibernateProperties">
      <map> 
        <entry key="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <entry key="hibernate.show_sql" value="true"/> 
        <entry key="hibernate.hbm2ddl.auto" value="create"/> 
      </map> 
    </property> 
  </bean> 

  <bean id="hibernateTemp" class="org.springframework.orm.hibernate3.HibernateTemplate" autowire="constructor"/> 
  <bean id="cdao" class="com.jlcindia.spring.hibernate.HibernateCustomerDAO"/> 
  <bean id="cs" class="com.jlcindia.spring.hibernate.CustomerServiceImpl"/>
</beans>

主要课程:

public class LAB27 { 
  public static void main(String[] args) {
    ApplicationContext ctx=new ClassPathXmlApplicationContext("jlcindia.xml"); 
    CustomerService cs=(CustomerService)ctx.getBean("cs"); 
    //1. addCustomer
    CustomerTO cto=new CustomerTO("san","s@jlc.com",98989,"bangalore");
    cs.addCustomer(cto);
  } 
}

Customer.hbm.xml

//Customer.hbm.xml
<hibernate-mapping package="com.jlcindia.spring.hibernate">
  <class name="Customer" table="Customer" lazy="false">
    <id name="cid" column="cid" type="int">
      <generator class="increment"/>
    </id>
    <property name="cname"/>
    <property name="email" />
    <property name="phone" />
    <property name="city"/>
  </class>
</hibernate-mapping>

非常感谢任何帮助

1 个答案:

答案 0 :(得分:3)

我认为问题在于您的映射文件作为异常状态:

InvalidMappingException: Could not parse mapping document from invalid

首先,我看到非XML评论//Customer.hbm.xml,如果您在撰写问题时不是拼写错误,那么这就是问题了 其次,我认为你应该为你的映射文件添加语法声明,如下所示:

<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">