2013-11-20 73 views
0

我剛開始使用Hibernate,但遇到了第一個程序本身的錯誤。請幫我找出錯誤。這是代碼。我使用Hibernate 4.2.7和PostgreSQL 9.3postgresql - 休眠程序中的錯誤

的hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?> 

<hibernate-configuration 
    xmlns="http://www.hibernate.org/xsd/hibernate-configuration" 
    xsi:schemaLocation="http://www.hibernate.org/xsd/hibernate-configuration hibernate-   configuration-4.0.xsd" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
<session-factory> 
<!-- Database connection settings --> 
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property> 
<property name="hibernate.connection.username">postgres</property> 
<property name="hibernate.connection.password">password</property> 

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

<!-- SQL dialect --> 
<property name="hibernate.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 class="org.hibernate.tutorial.dto.UserDetails"/> 

UserDetails.java

package org.hibernate.tutorial.dto; 

import javax.persistence.Entity; 
import javax.persistence.Id; 

@Entity 
public class UserDetails { 
@Id 
private int userId; 
private String userName; 

public int getUserId() { 
    return userId; 
} 
public void setUserId(int userId) { 
    this.userId = userId; 
} 
public String getUserName() { 
    return userName; 
} 
public void setUserName(String userName) { 
    this.userName = userName; 
} 

} 

HibernateTest.java

package org.hibernate.test; 

import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.hibernate.tutorial.dto.UserDetails; 

public class HibernateTest { 

/** 
* @param args 
*/ 
public static void main(String[] args) { 
    UserDetails mUser = new UserDetails(); 
    mUser.setUserId(1); 
    mUser.setUserName("freak"); 

    try { 
     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(mUser); 
     session.getTransaction().commit(); 
    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

} 

而且我面對錯誤是....

org.hibernate.MappingException: invalid configuration at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:2095) at org.hibernate.cfg.Configuration.configure(Configuration.java:2012) at org.hibernate.cfg.Configuration.configure(Configuration.java:1991) at org.hibernate.test.HibernateTest.main(HibernateTest.java:19) Caused by: org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 25; Document is invalid: no grammar found.

UPDATE: 這使得一些修改後的工作。我將xml內容更改爲此。

的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"> 

<session-factory> 

    <!-- Database connection settings --> 
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property> 
<property name="hibernate.connection.username">postgres</property> 
<property name="hibernate.connection.password">password</property> 

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

<!-- SQL dialect --> 
<property name="hibernate.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 class="org.hibernate.tutorial.dto.UserDetails"/> 

+0

您是否嘗試過在'hibernate-配置,4.0.xsd' –

+0

移除所有這些多餘的空格這只是在拷貝這裏引起了錯字。無論如何感謝您的檢查。它現在得到解決 – killboi6174

+0

然後刪除你的問題,或自己回答。 –

回答

0

它改變配置文件中的標題本

後工作0

的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">