2013-11-20 4 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

Он работал после изменения заголовка в файле конфигурации для этого

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"> 
Смежные вопросы