java - 我的第一个 hibernate 程序出现错误,请帮助我解决错误

标签 java hibernate postgresql

错误:

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.annotations.Version). log4j:WARN Please initialize the log4j system properly. Exception in thread "main" org.hibernate.exception.GenericJDBCException: Cannot open connection at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103) at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43) at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:29) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:420) at org.hibernate.jdbc.ConnectionManager.getConnection(ConnectionManager.java:144) at org.hibernate.jdbc.JDBCContext.connection(JDBCContext.java:119) at org.hibernate.transaction.JDBCTransaction.begin(JDBCTransaction.java:57) at org.hibernate.impl.SessionImpl.beginTransaction(SessionImpl.java:1326) at org.koushik.hibernate.HibernateTest.main(HibernateTest.java:20) Caused by: org.postgresql.util.PSQLException: FATAL: database "5432/hibernatedb" does not exist at org.postgresql.core.v3.ConnectionFactoryImpl.readStartupMessages(ConnectionFactoryImpl.java:691) at org.postgresql.core.v3.ConnectionFactoryImpl.openConnectionImpl(ConnectionFactoryImpl.java:207) at org.postgresql.core.ConnectionFactory.openConnection(ConnectionFactory.java:65) at org.postgresql.jdbc.PgConnection.(PgConnection.java:159) at org.postgresql.Driver.makeConnection(Driver.java:415) at org.postgresql.Driver.connect(Driver.java:283) at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.hibernate.connection.DriverManagerConnectionProvider.getConnection(DriverManagerConnectionProvider.java:110) at org.hibernate.jdbc.ConnectionManager.openConnection(ConnectionManager.java:417) ... 5 more

代码

hibernate .cfg.xml

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

<!DOCTYPE hibernate-configuration SYSTEM "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/5432/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="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="org.javabrains.koushik.dto.UserDetails" />
    <!--    <mapping resource="org/hibernate/tutorial/domain/Event.hbm.xml"/> -->
  </session-factory>
</hibernate-configuration>

UserDetails.java

package org.javabrains.koushik.dto;

import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
public class UserDetails {

@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;
}





}

HibernateTest.java

package org.koushik.hibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

import org.javabrains.koushik.dto.UserDetails;

public class HibernateTest {

    public static void main(String[] args) {


        UserDetails user = new UserDetails();
        user.setUserID(1);
        user.setUserName("First user");

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

    }


}

最佳答案

看起来可能是您的连接 URL 有误。不应该是吗

jdbc:postgresql://localhost:5432/hibernatedb

?

关于java - 我的第一个 hibernate 程序出现错误,请帮助我解决错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35012000/

相关文章:

java - 查找相似的字符串

java - 如何在Hibernate中运行mysql更新批量查询?

postgresql - 将 Oracle upsert 转换为 PostgreSQL 准备好的语句

hibernate - MS SQL Server 与 Hibernate 的高并发性能低下

postgresql - 为什么允许 "subqueries used as an expression"返回一组内容而不是表?

postgresql - 如何在 Ubuntu 中找到 pgAdminIII GUI

java - 我怎么知道java中当前的classLoader

java - 将 Java 泛型与特定类型结合使用

java - 从 Core Java 中的数组中删除重复元素

java - mysql + hibernate中有很多空sql语句