2013-09-26 53 views
5

Tôi cố gắng kết nối hibernate với PostgreSQL, nhưng tôi không biết tại sao nó không hoạt động. Tôi đã tìm kiếm nhiều bài đăng về cài đặt nhưng chúng không hoạt động đối với tôi. Cảm ơn bạn rất nhiều!Làm thế nào để kết nối hibernate với PostgreSQL trong nhật thực?

Dưới đây là cấu trúc của tác phẩm của tôi:

enter image description here

hibernate.cfg.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-configuration SYSTEM 
"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.url"> 
     jdbc:postgresql://localhost:5432/hibernatedb 
    </property> 
    <property name="hibernate.connection.username"> 
     eric 
    </property> 
    <property name="hibernate.connection.password"> 
     eric123 
    </property> 

    <mapping resource="Employee.hbm.xml"/> 
</session-factory> 
</hibernate-configuration> 

Employee.hbm.xml

<?xml version="1.0" encoding="utf-8"?> 
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD//EN" 
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

<hibernate-mapping> 
    <class name="Employee" table="EMPLOYEE"> 
     <meta attribute="class-description"> 
     This class contains the employee detail. 
     </meta> 
     <id name="id" type="int" column="id"> 
     <generator class="native"/> 
     </id> 
     <property name="firstName" column="first_name" type="string"/> 
     <property name="lastName" column="last_name" type="string"/> 
     <property name="salary" column="salary" type="int"/> 
    </class> 
</hibernate-mapping> 

Employee.java

package hibernate.application; 

    public class Employee { 
     private int id; 
     private String firstName; 
     private String lastName; 
     private int salary; 

     public Employee() {} 
     public Employee(String fname, String lname, int salary) { 
      this.firstName = fname; 
      this.lastName = lname; 
      this.salary = salary; 
     } 
     public int getId() { 
      return id; 
     } 
     public void setId(int id) { 
      this.id = id; 
     } 
     public String getFirstName() { 
      return firstName; 
     } 
     public void setFirstName(String first_name) { 
      this.firstName = first_name; 
     } 
     public String getLastName() { 
      return lastName; 
     } 
     public void setLastName(String last_name) { 
      this.lastName = last_name; 
     } 
     public int getSalary() { 
      return salary; 
     } 
     public void setSalary(int salary) { 
      this.salary = salary; 
     } 
    } 

ManageEmployee.java

public class ManageEmployee { 
    private static SessionFactory factory; 
    private static ServiceRegistry serviceRegistry; 

    public static void main(String[] args) { 
     Configuration configuration = new Configuration(); 
     configuration.configure(); 
     serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();   
     factory = configuration.buildSessionFactory(serviceRegistry); 

     ManageEmployee ME = new ManageEmployee(); 
     Integer empID1 = ME.addEmployee("Zara", "Ali", 1000); 
     ... 
    } 
} 

Error Messages

2013/9/26 上午 10:40:56 org.hibernate.annotations.common.Version <clinit> 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final} 
2013/9/26 上午 10:40:56 org.hibernate.Version logVersion 
INFO: HHH000412: Hibernate Core {4.2.5.Final} 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Environment <clinit> 
INFO: HHH000206: hibernate.properties not found 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Environment buildBytecodeProvider 
INFO: HHH000021: Bytecode provider name : javassist 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration configure 
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration getConfigurationInputStream 
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration addResource 
INFO: HHH000221: Reading mappings from resource: Employee.hbm.xml 
2013/9/26 上午 10:40:56 org.hibernate.cfg.Configuration doConfigure 
INFO: HHH000041: Configured SessionFactory: null 
2013/9/26 上午 10:40:56 org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure 
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!) 
Exception in thread "main" org.hibernate.service.classloading.spi.ClassLoadingException: Specified JDBC Driver org.postgresql.Driver could not be loaded 
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:111) 
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.buildJdbcConnectionAccess(JdbcServicesImpl.java:223) 
    at org.hibernate.engine.jdbc.internal.JdbcServicesImpl.configure(JdbcServicesImpl.java:89) 
    at org.hibernate.service.internal.StandardServiceRegistryImpl.configureService(StandardServiceRegistryImpl.java:75) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.initializeService(AbstractServiceRegistryImpl.java:159) 
    at org.hibernate.service.internal.AbstractServiceRegistryImpl.getService(AbstractServiceRegistryImpl.java:131) 
    at org.hibernate.cfg.Configuration.buildTypeRegistrations(Configuration.java:1818) 
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1776) 
    at hibernate.application.ManageEmployee.main(ManageEmployee.java:18) 
Caused by: org.hibernate.service.classloading.spi.ClassLoadingException: Unable to load class [org.postgresql.Driver] 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:149) 
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.configure(DriverManagerConnectionProviderImpl.java:106) 
    ... 11 more 
Caused by: java.lang.ClassNotFoundException: org.postgresql.Driver 
    at java.lang.Class.forName(Class.java:172) 
    at org.hibernate.service.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:146) 
    ... 12 more 

Trả lời

6

cuối cùng tôi đã tìm ra cách để giải quyết nó !!!

Tôi phải nhập tệp JDBC của PostgreSQL (postgresql-9.2-1003.jdbc4.jar). Tập tin này có thể cho phép nhật thực biết cách kết nối với PostGreSQL.

+0

Cảm ơn! Nó không có ý nghĩa với tôi vì tôi nghĩ rằng ngủ đông là cách khác với JDBC, nhưng dường như đó là những gì tôi nên làm ... – George

1

Dường như bạn cần đưa PosgreSQL vào dự án của mình. Tôi đề nghị sử dụng một hệ thống phụ thuộc động như Maven, nhưng bạn có thể tải về bản thân postgreSQL.

How to set up posgreSQL with Maven

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