2013-09-25 52 views
0

我正在用hibernate-core jar-4.2.5.Final開發hibernate應用程序。使用hibernate 4.x版本的hbm.xml文件中的錯誤

這是實體:

public class Contact { 
    Long id; 
    String firstName; 
    String lastName; 
    String email; 

     //setters and getters 
} 

Contact.hbm.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 package="org.sample.entity"> 
  <class name="com.sample.entity.Contact" table="CONTACT">   
     <id name="id" type="java.lang.Long"> 
      <column name="ID" /> 
      <generator class="increment" /> 
     </id> 
      <property name="firstName"><column name="FIRSTNAME" /></property> 
      <property name="lastName"><column name="LASTNAME"/></property> 
      <property name="email"><column name="EMAIL"/></property> 
 </class> 
</hibernate-mapping> 

RunContact.java

public class RunContact { 

    public static void main(String[] args) { 

     Session session = null; 

     try{ 
     //sessionfactory 
     Configuration configuration = new Configuration().configure(); 
     //SessionFactory factory = configuration.buildSessionFactory(); 
     ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 
     SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 

     //session open 
     session = sessionFactory.openSession(); 

     Contact contact = new Contact(); 
     contact.setFirstName("Venu Kumar"); 
     contact.setLastName("Indukuri"); 
     contact.setEmail("[email protected]"); 

     //save 
     session.save(contact); 

     }catch(Exception e){ 
      e.printStackTrace(); 
     }finally{ 
      //session close 
      session.flush(); 
      session.close(); 
     }   
    }  
} 

當我運行這個程序,我收到以下異常。

Caused by: org.xml.sax.SAXParseException: The content of element type "class" must match "(meta*,subselect?,cache?,synchronize*,comment?,tuplizer*,(id|composite-id),discriminator?,natural-id?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|properties|any|map|set|list|bag|idbag|array|primitive-array)*,((join*,subclass*)|joined-subclass*|union-subclass*),loader?,sql-insert?,sql-update?,sql-delete?,filter*,fetch-profile*,resultset*,(query|sql-query)*)". 

任何幫助解決問題的讚賞。

回答

1

看起來像DTD驗證失敗,但由於http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd(使用Notepad ++進行檢查),您的XML文件有效。
所以,我認爲這個問題是下一:

<hibernate-mapping package="org.sample.entity"> 
    <class name="Contact" table="CONTACT"> 

類名應該只包含簡單的類名稱,沒有包裝

+0

仍然給了同樣的錯誤 – ikvenu2000

+0

@ ikvenu2000它是全'hbm.xml'文件在你的例子或它只是它的一部分?可能你有更多'hbm.xml'文件? – Ilya

相關問題