2013-05-15 73 views

回答

101

這是一個posgresql的hibernate.cfg.xml,它可以幫助您使用posgresql的基本hibernate配置。

<!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> 
     <property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property> 
     <property name="hibernate.connection.driver_class">org.postgresql.Driver</property> 
     <property name="hibernate.connection.username">postgres</property> 
     <property name="hibernate.connection.password">password</property> 
     <property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/hibernatedb</property> 



     <property name="connection_pool_size">1</property> 

     <property name="hbm2ddl.auto">create</property> 

     <property name="show_sql">true</property> 



     <mapping class="org.javabrains.sanjaya.dto.UserDetails"/> 

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

這很好,但是我在哪裏放置這個文件?在WEB-INF中? –

+1

@HrishikeshChoudhari它應該在你的構建路徑中的某處。我認爲WEB-INF會很好。我對Web項目沒有太多的瞭解,但是我覺得WEB-INF正在構建路徑中。 –

+9

org.hibernate.dialect.PostgreSQLDialect已棄用。 您應該使用 org.hibernate.dialect.PostgreSQL82Dialect 而不是 – long

5

如果項目是行家把它夾在src/main/resources,在封裝階段,將它複製../WEB-INF/classes/hibernate.cfg.xml

1

這是hibernate.cfg.xml文件來連接PostgreSQL的9.5,這是幫助你基本的配置。

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

<!-- 
    ~ Hibernate, Relational Persistence for Idiomatic Java 
    ~ 
    ~ License: GNU Lesser General Public License (LGPL), version 2.1 or later. 
    ~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. 
    --> 
<!DOCTYPE hibernate-configuration SYSTEM 
     "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">postgres</property> 
     <property name="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="com.waseem.UserDetails"/> 
    </session-factory> 
</hibernate-configuration> 

確保文件的位置應該是在的src /主/資源/ hibernate.cfg.xml的

0

是利用彈簧的引導與Hibernate的配置文件,我們可以堅持數據到數據庫。 在您的src/main/resources文件夾中保留休眠.cfg.xml以讀取與數據庫相關的配置。

enter image description here

+1

請** **您的文章,並顯示實際的代碼/ XML文本,而不是截圖。其他人不能從你的圖像複製和粘貼。詳情請參閱(https://meta.stackoverflow.com/a/285557/1402846)。謝謝。 – Pang