3

我想使用Configuration()methot。但我得到這個錯誤:是否需要使用AnnotationConfiguration進行Hibernate配置?

Initial SessionFactory creation failed.org.hibernate.MappingException: An AnnotationConfiguration instance is required to use <mapping class="com.example.domain.Lesson"/> 

如果我使用AnnotationConfiguratin()方法,Hibernate可以讀取我的類。我不明白這一點:

爲什麼當我用Configuration配置的時候,Hibernate看不到<mapping class="com.example.Lesson"/>的定義?

hibernate.cfg.xml文件:

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 
<hibernate-configuration> 
    <session-factory> 

     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
     <property name="hibernate.connection.password">****</property> 
     <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/DB</property> 
     <property name="hibernate.connection.username">postgres</property> 
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 

     <mapping resource="com.example/Lesson.hbm.xml" /> 

     <mapping class="com.example.Lesson"/> 


    </session-factory> 
</hibernate-configuration> 

我的課類:

public class Lesson implements java.io.Serializable { 

     private int id; 
     private String lesson; 
     private Set lessons = new HashSet(0); 

     public Lesson() { 
     } 


     public Lesson(int id, String lesson, Set lessons) { 
      this.id = id; 
      this.id = id; 
      this.lessons = lessons; 
     } 


     continue.. 



} 

我從reverse engineering.有bhm.xml文件,所以,我不想使用註釋。

有什麼想法?

+0

向我們顯示'com.example.domain.Lesson'類源。它似乎包含一些hibernate註釋。 – Andremoniy

+0

我加了。 Lesson類的源不包含hibernate註釋。 –

+0

好的。我沒有仔細閱讀你的問題。在我看來,問題在於'hibernate.cfg.xml'中的'語句。 – Andremoniy

回答

1

只需從hibernate.cfg.xml刪除<mapping class="com.example.Lesson"/>。這不是必需的,因爲Lesson.hbm.xml存在。

+0

這是解決方案。非常感謝。 –

+0

@BreedHansen不用客氣 – Andremoniy

相關問題