2013-05-15 40 views
56

Tôi có một dự án Spring MVC trống và tôi đã cài đặt trình điều khiển Hibernate và PostgreSQL bằng Maven.Kết nối PostgreSQL 9.2.1 với Hibernate

Tôi đang chạy ngắn trên hướng dẫn đầy đủ cho biết cách kết nối PostgreSQL với Hibernate.

Mọi trợ giúp tại đây?

Trả lời

101

Đây là tệp hibernate.cfg.xml cho posgresql và nó sẽ giúp bạn với các cấu hình ngủ đông cơ bản cho posgresql.

<!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

Điều này thật tuyệt, nhưng tôi nên đặt tệp này ở đâu? Trong WEB-INF? –

+1

@ HrishikeshChoudhari nó phải ở đâu đó trong đường dẫn xây dựng của bạn. Tôi nghĩ WEB-INF sẽ ổn. Tôi không hiểu nhiều về các dự án web, nhưng tôi cảm thấy rằng WEB-INF nằm trong đường dẫn xây dựng. –

+9

org.hibernate.dialect.PostgreSQLDialect không còn được dùng nữa. bạn nên sử dụng org.hibernate.dialect.PostgreSQL82Dialect thay vì – long

5

Nếu dự án là maven đặt nó trong src/main/resources, trong giai đoạn gói nó sẽ sao chép nó trong ../WEB-INF/classes/hibernate.cfg.xml

1

Đây là file hibernate.cfg.xml để kết nối postgresql 9,5 và đây là sự giúp đỡ để bạn cấu hình cơ bản.

<?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> 

Hãy chắc chắn rằng vị trí tập tin cần được theo src/main/nguồn/hibernate.cfg.xml

0

Có bằng cách sử dụng lò xo khởi động với các tập tin cấu hình hibernate chúng ta có thể kiên trì dữ liệu vào cơ sở dữ liệu. giữ ngủ đông .cfg.xml trong thư mục src/main/resources của bạn để đọc các cấu hình liên quan đến cơ sở dữ liệu.

enter image description here

+1

Vui lòng ** chỉnh sửa ** bài đăng của bạn và hiển thị mã/XML thực tế dưới dạng văn bản thay vì ảnh chụp màn hình. Những người khác không thể sao chép và dán từ hình ảnh của bạn. [Xem tại đây] (https://meta.stackoverflow.com/a/285557/1402846) để biết chi tiết. Cảm ơn bạn. – Pang

Các vấn đề liên quan