使用hibernate 4.x版本的hbm.xml文件出错

时间:2013-09-25 08:09:30

标签: hibernate hibernate-mapping

我正在使用maven和hibernate-core jar开发hibernate应用程序 - 4.2.5.Final。

这是实体:

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("venu@prokarma.com");

        //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 个答案:

答案 0 :(得分: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">

类名应该只包含简单的类名,不包含