2013-09-24 179 views
2

我得到這個錯誤: Caused by: org.xml.sax.SAXParseException; lineNumber: 35; columnNumber: 44; Attribute "name" must be declared for element type "mapping". 問題與該行<mapping name="org.one.dto.UserDetails" />一些,爲什麼它沒有找到name屬性屬性「名」必須爲聲明元素類型「映射」

下面是我的hibernate.cfg.xml文件內容:

<?xml version='1.0' encoding='utf-8'?> 
<!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD 3.0//EN" 
     "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd"> 

<hibernate-configuration> 
    <session-factory> 
     <!-- Database connection settings --> 
     <property name="connection.driver_class">org.postgresql.Driver</property> 
     <property name="connection.url">jdbc:postgresql://localhost:5433/hibernatedb 
     </property> 
     <property name="connection.username"></property> 
     <property name="connection.password">tere123</property> 

     <!-- JDBC connection pool (use the built-in) --> 
     <property name="connection.pool_size">1</property> 

     <!-- SQL dialect --> 
     <property name="dialect">org.hibernate.dialect.PostgreSQLDialect 
     </property> 

     <!-- Enable Hibernate's automatic session context management --> 
     <property name="current_session_context_class">thread</property> 

     <!-- Disable the second-level cache --> 
     <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider 
     </property> 

     <!-- Echo all executed SQL to stdout --> 
     <property name="show_sql">true</property> 

     <!-- Drop and re-create the database schema on startup --> 
     <property name="hbm2ddl.auto">create</property> 

     <mapping name="org.one.dto.UserDetails" /> 

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

這裏是我的主類:

package org.one; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.service.ServiceRegistry; 
import org.hibernate.service.ServiceRegistryBuilder; 
import org.one.dto.UserDetails; 

public class HibernateTest { 
    private static ServiceRegistry serviceRegistry; 
    public static void main(String[] args){ 

     UserDetails user = new UserDetails(); 
     user.setId(1); 
     user.setUserName("Laura"); 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry(); 

     SessionFactory sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(user); 
     session.getTransaction().commit(); 

    } 
} 

你能告訴WH在錯誤?

回答

1

映射標籤取相應的屬性

<!ELEMENT mapping EMPTY> <!-- reference to a mapping file --> 
<!ATTLIST mapping resource CDATA #IMPLIED> 
<!ATTLIST mapping file CDATA #IMPLIED> 
<!ATTLIST mapping jar CDATA #IMPLIED> 
<!ATTLIST mapping package CDATA #IMPLIED> 
<!ATTLIST mapping class CDATA #IMPLIED> 

它沒有名字屬性中列出。

0

如果您使用的是Hibernate映射XML文件,則必須將其指定爲如下:

<mapping resource="org.one.dto.UserDetails.hbm.xml"/> <!-- or your path to the mapping XML file --> 

Hibernate映射XML標籤沒有屬性「名」。

相關問題