2012-09-11 178 views
0

我想插入到我的postgresql數據庫使用休眠,我挺或還沒有弄清楚爲什麼我不斷收到這個錯誤,我想要做的是讓我的休眠創建我的數據庫表和列並插入到創建的表中。所以我的第一個錯誤是關於失蹤的jar文件,這是我已經加入到我的Hibernate庫的SLF4J,但是經過我再次運行應用程序新的錯誤彈出所以這是我的錯誤:Java休眠錯誤

90 [main] INFO org.hibernate.annotations.common.Version - Hibernate Commons 

Annotations 3.2.0.Final 
100 [main] INFO org.hibernate.cfg.Environment - Hibernate 3.6.10.Final 
100 [main] INFO org.hibernate.cfg.Environment - hibernate.properties not found 
100 [main] INFO org.hibernate.cfg.Environment - Bytecode provider name : javassist 
100 [main] INFO org.hibernate.cfg.Environment - using JDK 1.4 java.sql.Timestamp handling 
150 [main] INFO org.hibernate.cfg.Configuration - configuring from resource: /hibernate.cfg.xml 
150 [main] INFO org.hibernate.cfg.Configuration - Configuration resource: /hibernate.cfg.xml 
Exception in thread "main" org.hibernate.HibernateException: /hibernate.cfg.xml not found 
    at org.hibernate.util.ConfigHelper.getResourceAsStream(ConfigHelper.java:170) 
    at org.hibernate.cfg.Configuration.getConfigurationInputStream(Configuration.java:2176) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2157) 
    at org.hibernate.cfg.Configuration.configure(Configuration.java:2137) 
    at org.hibernate_main.HibernateMain.main(HibernateMain.java:13) 

這是我的JAR文件(INT Hibernate庫):

hibernate3.jar 
antlr-2.7.6.jar 
commons-collections-3.1.jar 
dom4j-1.6.1.jar 
javassist-3.12.0.GA.jar 
jta-1.1.jar 
slf4j-api-1.6.1.jar 
hibernate-jpa-2.0-api-1.0.1.Final.jar 
postgresql-9.1-902.jdbc4 

而且我的代碼: 配置(XML)

<?xml version="1.0" encoding="UTF-8"?> 

<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright (c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the @author tags or express copyright attribution ~ statements applied by the authors. All third-party contributions are ~ distributed under license by Red Hat Inc. ~ ~ This copyrighted material is made available to anyone wishing to use, modify, ~ copy, or redistribute it subject to the terms and conditions of the GNU ~ Lesser General Public License, as published by the Free Software Foundation. ~ ~ This program is distributed in the hope that it will be useful, ~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY ~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License ~ for more details. ~ ~ You should have received a copy of the GNU Lesser General Public License ~ along with this distribution; if not, write to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~ Boston, MA 02110-1301 USA --> 

<!DOCTYPE hibernate-configuration SYSTEM "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd" PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"> 
-<hibernate-configuration> -<session-factory> 
<!-- Database connection settings --> 
<property name="connection.driver_class">org.postgresql.Driver</property> 
<property name="connection.url">jdbc:postgresql://localhost:5432:MYPOS</property> 
<property name="connection.username">postgres</property> 
<property name="connection.password"/>perbert101 
<!-- JDBC connection pool (use the built-in) --> 
<property name="connection.pool_size">1</property> 
<!-- SQL dialect --> 
<property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
<!-- Disable the second-level cache --> 
<property name="cache.provider_class">org.hibernate.cache.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> 
<!-- Names the annotated entity class --> 
<mapping class="org.user_detail.dto.UserDetail"/> </session-factory> </hibernate-configuration> 

UserDetail類:

package org.user_detail.dto; 
import javax.persistence.Entity; 
import javax.persistence.Id; 

@Entity 
public class UserDetail { 
    @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; 
    } 

} 

我的主類:

package org.hibernate_main; 
import org.hibernate.Session; 
import org.hibernate.SessionFactory; 
import org.hibernate.cfg.Configuration; 
import org.user_detail.dto.UserDetail; 

public class HibernateMain { 
    public static void main(String[] args){ 
     UserDetail user = new UserDetail(); 
     user.setUserId(1); 
     user.setUserName("Jerome"); 

     SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory(); 
     Session session = sessionFactory.openSession(); 
     session.beginTransaction(); 
     session.save(user); 
     session.getTransaction().commit(); 
    } 
} 

傢伙你能告訴我這是什麼錯誤是,如何解決這些問題,以及如何測試,如果在XML數據庫連接是成功還是失敗。多謝你們。

+1

哪裏是你的配置文件?你能編輯出非必要的信息嗎? –

回答

2

我覺得

/hibernate.cfg.xml not found 

是非常明顯的。

確保它在CLASSPATH中。

您還可以指定它:

return new Configuration().configure("/path/to/hibernate.cfg.xml").buildSessionFactory(); 

在你的代碼上述

+0

耶謝謝。我的錯誤:我是名稱配置應該是休眠。 – Zyrax

+0

此錯誤意味着什麼無法解析配置:/hibernate.cfg.xml – Zyrax

+0

這意味着它無法讀取XML文件,因爲它包含錯誤。這裏有一個破折號「 - 」 - '和這裏' - '和' perbert101'沒有結束標記 –