2015-05-14 86 views
0

我得到異常下面冬眠:獲取異常而連接彈簧使用H2數據庫

log4j:WARN No appenders could be found for logger (org.springframework.context.support.ClassPathXmlApplicationContext). 
log4j:WARN Please initialize the log4j system properly. 
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mysessionFactory' defined in class path resource [applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.InvalidMappingException: Could not parse mapping document from input stream 
    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.naveen.java.InsertTest.main(InsertTest.java:12) 
Caused by: org.hibernate.InvalidMappingException: Could not parse mapping document from input stream 
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:508) 
    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.dom4j.DocumentException: Connection timed out: connect Nested exception: Connection timed out: connect 
    at org.dom4j.io.SAXReader.read(SAXReader.java:484) 
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499) 
    ... 16 more 

下面是員工的.java

package com.naveen.java; 

import javax.persistence.Id; 

public class Employee { 

private int id; 
private String name; 
private int salary; 
private String LASTNAME ; 

public String getLASTNAME() { 
    return LASTNAME; 
} 
public void setLASTNAME(String lASTNAME) { 
    this.LASTNAME = lASTNAME; 
} 
public int getId() { 
    return id; 
} 
public void setId(int id) { 
    this.id = id; 
} 
public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public float getSalary() { 
    return salary; 
} 
public void setSalary(int salary) { 
    this.salary = salary; 
} 

}

下面

是employeedao.java

package com.naveen.java; 
import org.springframework.orm.hibernate3.HibernateTemplate; 

public class EmployeeDao { 
HibernateTemplate template; 
public void setTemplate(HibernateTemplate template) { 
    this.template = template; 
} 

public void saveEmployee(Employee e){ 
    template.save(e); 
} 

public void updateEmployee(Employee e){ 
    template.update(e); 
} 

public void deleteEmployee(Employee e){ 
    template.delete(e); 
} 
} 

下面是inserttest.java低於

package com.naveen.java; 

import org.springframework.beans.factory.BeanFactory; 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import org.springframework.core.io.ClassPathResource; 
import org.springframework.core.io.Resource; 

public class InsertTest { 
public static void main(String[] args) { 

    ApplicationContext con=new ClassPathXmlApplicationContext("applicationContext.xml"); 
    //Resource r=new ClassPathResource("applicationContext.xml"); 
    EmployeeDao dao=(EmployeeDao)con.getBean("d"); 

    Employee e=new Employee(); 
    e.setId(147); 
    e.setName("kumar"); 
    e.setSalary(70000); 

    //dao.saveEmployee(e); 
    dao.updateEmployee(e); 
} 
} 

都將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:p="http://www.springframework.org/schema/p" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> 


    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
      destroy-method="close"> 
     <property name="driverClassName" value="org.h2.Driver"/> 
     <property name="url" value="jdbc:h2:~/test"/> 
     <property name="username" value="sa"/> 
     <property name="password" value="123"/> 
    </bean> 


<bean id="mysessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="dataSource"></property> 

     <property name="mappingResources"> 
     <list> 
     <value>employee-hbm.xml</value> 
     </list> 
     </property> 

     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.H2Dialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 

      </props> 
     </property> 
    </bean> 

    <bean id="template" class="org.springframework.orm.hibernate3.HibernateTemplate"> 
    <property name="sessionFactory" ref="mysessionFactory"></property> 
    </bean> 

    <bean id="d" class="com.naveen.EmployeeDao"> 
    <property name="template" ref="template"></property> 
    </bean> </beans> 

第二屆XML映射

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
     "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 


<hibernate-mapping> 
    <class name="com.naveen.java.Employee" table="EMP558"> 

     <id name="id" type="int" column="id"> 
     <generator class="native"/> 
     </id> 
     <property name="firstName" column="NAME" type="string"/> 
     <property name="lastName" column="LASTNAME" type="string"/> 
     <property name="salary" column="salary" type="double"/> 
    </class> 
</hibernate-mapping> 

我試圖通過JDBC鏈接和它的正常工作。

+0

你在哪裏保留「employee-hbm.xml」?檢查employee-hbm.xml的這個路徑是否沒有錯。 – Bikku

+0

@arin兩者都存在於相同的包/文件夾中。 – Adward

+0

你可以驗證你的applicationContext.xml是否正確?我得到錯誤http://www.w3schools.com/xml/xml_validator.asp。看來結束標記丟失 – Reddy

回答

0

工資字段的int類型,但在Hibernate映射

+0

仍然沒有進展。 感謝您的幫助 – Adward

+0

請更新這個問題,以反映您所做的更改 – Reddy

0

因爲休眠試圖獲取DTD文件,這可能會導致雙。所以,解決方法可能是:

  1. 變化不同於DTD來"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"
  2. 值創建您自己的函數來解析配置然後傳遞到休眠這樣

    public static Document parseConfiguration(String resourcePath) throws Exception { 
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 
        factory.setValidating(false); <-- the magic is here 
        DocumentBuilder builder = factory.newDocumentBuilder(); 
        return builder.parse(builder.getClass().getResourceAsStream(resourcePath)); 
    } 
    
+0

對不起兄弟不工作 – Adward

0

雖然我在你的實體中定義了很多錯誤(在映射文件中定義的屬性不存在於實體中),但我沒有收到你所得到的錯誤。我已經達到了試圖插入記錄的那一步​​,所以我很好地過去了發生錯誤的階段。

通過查看拋出錯誤的hibernate源文件(org.hibernate.cfg.Configuration.addInputStream(Configuration.java:499)),hibernate無法訪問映射xml文件。

請仔細檢查映射文件是否在正確的位置並且可讀。 還要確保在類路徑中有正確版本的hibernate jar。

+0

我知道有一些錯誤,但我仍然在我的情況下嘗試單列仍然一次又一次得到相同的異常.. 可以分享你的哪個hibernate jar和dtd你使用...這將是一個很好的幫助.. – Adward

+0

我只複製了你的代碼(包括你的包結構)。我正在使用休眠3.6.1。你能否檢查你是否能夠連接到映射DTD http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd? – Reddy

+0

做任何機構知道如何解決它...請幫助我的傢伙.... – Adward