2013-05-15 12 views

Odpowiedz

101

To jest plik hibernate.cfg.xml dla posgresql i pomoże ci w podstawowych konfiguracjach hibernacji dla 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

To jest świetne, ale gdzie mogę umieścić ten plik? W WEB-INF? –

+1

@HrishikeshChoudhari powinno być gdzieś w twojej ścieżce budowania. Myślę, że WEB-INF będzie dobrze. Nie mam zbyt wiele zrozumienia na temat projektów internetowych, ale czuję, że WEB-INF jest na ścieżce budowania. –

+9

org.hibernate.dialect.PostgreSQLDialect jest przestarzałe. powinieneś użyć org.hibernate.dialect.PostgreSQL82Dialect zamiast tego – long

5

Jeśli projekt jest Maven umieścił go w src/main/resources w fazie pakietu będzie skopiować go w ../WEB-INF/classes/hibernate.cfg.xml

1

Jest to plik hibernate.cfg.xml połączyć postgresql 9.5 i jest to pomoc dla podstawowa konfiguracja.

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

Upewnij Lokalizacja pliku powinna być pod src/main/resources/hibernate.cfg.xml

0

Tak za pomocą sprężyny-boot z plików konfiguracyjnych hibernacji możemy utrzymywać dane do bazy danych. Przechowuj hibernację .cfg.xml w folderze src/main/resources, aby odczytać konfiguracje związane z bazą danych.

enter image description here

+1

Proszę ** [edytuj] ** swój wpis i pokaż rzeczywisty kod/XML jako tekst zamiast zrzutu ekranu. Inni nie mogą kopiować i wklejać zdjęć. [Zobacz tutaj] (https://meta.stackoverflow.com/a/285557/1402846) w celu uzyskania szczegółowych informacji. Dziękuję Ci. – Pang