2014-10-17 95 views
0

我是Spring的新手,以至於Hibernate框架。任務是創建一個包含前端和後端的項目,但現在我想專注於數據庫連接。Spring + Hibernate + Maven org.hibernate.MappingException:需要AnnotationConfiguration實例

我在我的MySQL服務器的localhost上有MySQL數據庫。有一個簡單的應用程序需要創建客戶端,命令並在數據庫中搜索它們,因此任務很簡單。然而,我遇到以下ecxeption:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'clientDAOImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory ee.st.running.dao.ClientDAOImpl.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [config.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="ee.st.running.model.Client"/>  

我嘗試了一些操作,這裏看過不少類似的問題,害得我等異常喜歡這個。

我的config.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" 
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:task="http://www.springframework.org/schema/task" 
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd"> 
    <context:component-scan base-package=".*" /> 
<tx:annotation-driven/> 
    <context:annotation-config /> 
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 
    <property name="driverClassName" value="com.mysql.jdbc.Driver" /> 
    <property name="url" value="jdbc:mysql://localhost:3306/sqlconnection" /> 
    <property name="username" value="admin" /> 
    <property name="password" value="root" /> 
    </bean> 


<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource"></property> 
    <property name="annotatedClasses"> 
      <list> 
       <value>ee.st.running.model.Client</value> 
       <value>ee.st.running.dao.ClientDAOImpl</value> 
       <value>ee.st.running.model.Order</value> 
       <value>ee.st.running.dao.OrderDAOImpl</value> 
      </list> 
     </property> 
     <property name="configLocation"> 
    <value>classpath:hibernate.cfg.xml</value> 
</property> 
    <property name="hibernateProperties"> 
     <props> 
     <prop 
     key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop> 
     <prop key="hibernate.show_sql">true</prop> 
     </props> 

    </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager" 
    p:sessionFactory-ref="sessionFactory"> 
    </bean> 

    <bean id = "client" class = "ee.st.running.model.Client"> 
    <property name="clientDAO" ref = "ClientDAO"></property> 
    </bean> 

    <bean id = "clientDAO" class = "ee.st.running.dao.ClientDAOImpl"> 
    <property name="sessionFactory" ref = "sessionFactory"></property> 
    </bean> 


    <bean id = "order" class = "ee.st.running.model.Order"> 
    <property name="orderDAO" ref = "OrderDAO"></property> 
    </bean> 

     <bean id = "orderDAO" class = "ee.st.running.dao.OrderDAOImpl"> 
    <property name="sessionFactory" ref = "sessionFactory"></property> 
    </bean> 

    </beans> 

這裏是項目結構:

enter image description here enter image description here enter image description here

這裏是我的pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>ee.st.running.aktorstask</groupId> 
    <artifactId>aktorstask</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-core</artifactId> 
     <version>3.3.2.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-annotations</artifactId> 
     <version>3.3.1.GA</version> 
    </dependency> 
    <dependency> 
     <groupId>org.hibernate</groupId> 
     <artifactId>hibernate-commons-annotations</artifactId> 
     <version>3.3.0.ga</version> 
    </dependency> 


    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-tx</artifactId> 
     <version>4.0.6.RELEASE</version> 
    </dependency> 

    <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-orm</artifactId> 
      <version>4.0.6.RELEASE</version> 
    </dependency> 

    <dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-context</artifactId> 
     <version>4.0.6.RELEASE</version> 
    </dependency> 

    <dependency> 
<groupId>commons-dbcp</groupId> 
<artifactId>commons-dbcp</artifactId> 
<version>1.4</version> 
</dependency> 

    <dependency> 
<groupId>org.slf4j</groupId> 
<artifactId>slf4j-log4j12</artifactId> 
<version>1.5.6</version> 
</dependency> 


    <dependency> 
     <groupId>mysql</groupId> 
     <artifactId>mysql-connector-java</artifactId> 
     <version>5.1.6</version> 
    </dependency> 
</dependencies> 
<properties> 
    <spring.version>3.2.3.RELEASE</spring.version> 
</properties> 
</project> 

及其類別: 客戶

package ee.st.running.model; 

//import java.util.List; 
//import java.util.Set; 
    import javax.persistence.Column; 
    import javax.persistence.Entity; 
    import javax.persistence.GeneratedValue; 
    import javax.persistence.Id; 
//import javax.persistence.OneToMany; 
    import javax.persistence.Table; 
    import ee.st.running.dao.ClientDAOInterface; 
    import ee.st.running.dao.ClientDAOImpl; 

    @Entity 
    @Table(name = "client") 
    public class Client implements ClientInterface { 


private Order o; 
ClientDAOInterface ClientDAOInterface; 

@Id 
@GeneratedValue 
private int id; 


@Column (name = "name") 
private String name; 
@Column (name = "surename") 
private String surename; 
@Column (name = "address") 
private String address; 
@Column (name = "phone_number") 
private long phoneNumber; 
@Column (name = "id_code") 
private long idCode; 

//@OneToMany(mappedBy = «client», fetch = FetchType.LAZY) 


public void makeorder() { 
    o.newOrder(); 
} 

// getters nd setters 

public Order getO() { 
    return o; 
} 


public void setO(Order o) { 
    this.o = o; 
} 


public String getName() { 
    return name; 
} 


public void setName(String name) { 
    this.name = name; 
} 


public String getSurename() { 
    return surename; 
} 


public void setSurename(String surename) { 
    this.surename = surename; 
} 


public String getAddress() { 
    return address; 
} 


public void setAddress(String address) { 
    this.address = address; 
} 


public long getPhoneNumber() { 
    return phoneNumber; 
} 


public void setPhoneNumber(int phoneNumber) { 
    this.phoneNumber = phoneNumber; 
} 


public long getIdCode() { 
    return idCode; 
} 


public void setIdCode(int idCode) { 
    this.idCode = idCode; 
} 


// ctor 

public Client() 
{ 

} 



public void setClientInfDAO (ClientDAOInterface ClientDAOInterface) 
{ 
    this.ClientDAOInterface = ClientDAOInterface; 
} 


public void save(Client client) { 

    ClientDAOInterface.save(client); 
} 


public void update(Client client) { 

    ClientDAOInterface.update(client); 
} 


public void remove(Client client) { 

    ClientDAOInterface.remove(client); 
} 
} 

ClientInterface:

package ee.st.running.model; 

public interface ClientInterface { 
public void makeorder(); 
public void save(Client client); 
public void update (Client client); 
public void remove (Client client); 

} 

ClientDAOInterface:

package ee.st.running.dao; 

import java.util.List; 
import ee.st.running.model.Client; 
public interface ClientDAOInterface { 

public void removeClient (long id); 
public List<Client> listClient(); 
public void save (Client client); 
public void update (Client client); 
public void remove (Client client); 
} 

ClientDAOImpl:

package ee.st.running.dao; 

import java.util.List; 
import org.hibernate.SessionFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; 
import org.springframework.stereotype.Repository; 
import ee.st.running.model.Client; 

@Repository 
public class ClientDAOImpl extends HibernateDaoSupport implements ClientDAOInterface { 


@Autowired 
private SessionFactory sessionFactory; 


public void AddClient(Client client) { 
    sessionFactory.getCurrentSession().save(client); 

} 

public void removeClient(long id) { 


    Client client = (Client) sessionFactory.getCurrentSession().load(Client.class, id); 
    if (null != client) { 
     sessionFactory.getCurrentSession().delete(client); 
    } 

} 

@SuppressWarnings("unchecked") 
public List<Client> listClient() { 

    return sessionFactory.getCurrentSession().createQuery("from Client").list(); 
} 

public void save(Client client) { 
    sessionFactory.getCurrentSession().save(client); 

} 

public void update(Client client) { 
    // TODO Auto-generated method stub 

} 

public void remove(Client client) { 
    // TODO Auto-generated method stub 

} 
} 

最後我的hibernate.cfg:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
"-//Hibernate/Hibernate Configuration DTD//EN" 
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
<session-factory> 
    <mapping class="ee.st.running.model.Client" /> 
    <mapping class="ee.st.running.model.Order" /> 
</session-factory> 
</hibernate-configuration> 

我修改了幾次,添加了一些額外的信息。我現在要的是檢查,是否它的工作原理,所以在主類,我寫的原始代碼信息存儲到數據庫,看看它是否真的保存,這樣我就可以繼續前進:

package ee.st.running.aktorstask; 

import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
import ee.st.running.dao.*; 
import ee.st.running.model.*; 
public class Main { 

public static void main(String[] args) { 

    ApplicationContext appContext = new ClassPathXmlApplicationContext ("./config.xml"); 

    Client clientx = (Client) appContext.getBean("client"); 
    Client client = new Client(); 
    client.setName("Vasilij"); 
    client.setIdCode(389844455); 
    client.setAddress("Pae 485 54 Tartu"); 
    client.setSurename("B"); 
    //client.makeorder(); 

    clientx.save(client); 
    } 
    } 

現在給我mappingexception 任何想法,爲什麼?順便說一下,「AnnotationConfiguration實例」是誰,它應該是什麼?

回答

0

普通hibernate.cfg.xml中使用配置類爲構建SessionFactory對象

return new Configuration().configure().buildSessionFactory(); 

您需要使用AnnotationConfiguration的一樣。

在你上面的例子中,你已經在指定的sessionFactory bean類爲

<property name="annotatedClasses"> 
      <list> 
       <value>ee.st.running.model.Client</value> 
       <value>ee.st.running.dao.ClientDAOImpl</value> 
       <value>ee.st.running.model.Order</value> 
       <value>ee.st.running.dao.OrderDAOImpl</value> 
      </list> 
     </property> 

你在這裏做的是你添加的DAO類也做錯了一件事。 AnnotatedClasses應該是域類。 您可以刪除它們並僅保留客戶端和訂單。

其次,你還提供了hibernate.cfg.xml文件。因此,您可以在使用註釋類時刪除它。

因此最終的代碼可能是這樣的:

<property name="annotatedClasses"> 
      <list> 
       <value>ee.st.running.model.Client</value> 
       <value>ee.st.running.model.Order</value> 
      </list> 
     </property> 

和刪除的hibernate.cfg.xml

+0

謝謝你的回答!然而,似乎還有另一個例外:「...嵌套的異常是org.springframework.beans.factory.BeanCreationException:在類路徑資源[config.xml]中定義的名爲'sessionFactory'的bean創建時出錯:init方法的調用失敗;嵌套的異常是java.io.FileNotFoundException:類路徑資源[hibernate.cfg.xml]無法解析爲URL,因爲它不存在「 和removig」「從我的配置中,I另一個: – 2014-10-17 14:08:09

+0

「java.lang.NoSuchMethodError:org.hibernate.cfg.Configuration.addAnnotatedClass(Ljava/lang/Class;)Lorg/hibernate/cfg/Configuration;」像這樣.. – 2014-10-17 14:08:32

+0

我更新了hibernate版本到3.6.0.Final,根據http://stackoverflow.com/questions/14958708/java-lang-nosuchmethoderror-org-hibernate-cfg-configuration-addannotatedclass,但它又返回我錯誤 「嵌套的異常是org.springframework.beans.factory.BeanCreationException:在類路徑資源[config.xml]中定義名稱爲'sessionFactory'的bean時出錯:init方法的調用失敗;嵌套的異常是java.lang .NoClassDefFoundError:org/hibernate/annotations/common/reflection/MetadataProvider「 – 2014-10-17 14:55:56

相關問題