java - 从 IntelliJ 使用 Hibernate 连接到 PostgreSQL 服务器

标签 java postgresql hibernate jpa jdbc

我正在尝试通过 Hibernate 将数据持久保存到 PostgreSQL 数据库,输入我的用户/密码,检查它是否正常工作,创建一个数据库和一些表。 编译的时候报错

org.postgresql.util.PSQLException: The server
requested password-based authentication, but no password was provided.

我正在使用 IntelliJ Ultimate 2017.1 并尝试使用提供的 pg 驱动程序和 42.00 作为外部库。

我在以前的版本中使用过它,但以前从未见过这个。必须承认我不是很擅长这个。 基本上,我定义的密码没有正确传递到服务器。好像它认出了我的用户名。我通过修改我的 pg_hba.conf 文件以信任没有密码的本地连接暂时避免了这个问题,但我将把数据保存在在线服务器上,所以我需要更好的修复。 驱动程序有一个标准的 URL 模板,如下所示:

jdbc:postgresql:{database::postgres}[\?<&,user={user:param},password={password:param},{:identifier}={:param}>]

这是我的 hibernate.cfg.xml

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD//EN"
    "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="connection.url">jdbc:postgresql://localhost:5432/gigahertz</property>
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <mapping class="no.hvl.dat101.gigahertz.ReservationJPA"/>

    <!-- <property name="connection.username"/> -->
    <!-- <property name="connection.password"/> -->

    <!-- DB schema will be updated if needed -->
    <!-- <property name="hbm2ddl.auto">update</property> -->
</session-factory>

这是我生成的主类

package no.hvl.dat101.gigahertz;

import org.hibernate.HibernateException;
import org.hibernate.Metamodel;
import org.hibernate.query.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

import javax.persistence.metamodel.EntityType;

import java.util.Map;

/**
 */
public class Main {
    private static final SessionFactory ourSessionFactory;

    static {
        try {
            Configuration configuration = new Configuration();
            configuration.configure();

            ourSessionFactory = configuration.buildSessionFactory();
        } catch (Throwable ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static Session getSession() throws HibernateException {
        return ourSessionFactory.openSession();
    }

    public static void main(final String[] args) throws Exception {
        final Session session = getSession();
        try {
            System.out.println("querying all the managed entities...");
            final Metamodel metamodel = session.getSessionFactory().getMetamodel();
            for (EntityType<?> entityType : metamodel.getEntities()) {
                final String entityName = entityType.getName();
                final Query query = session.createQuery("from " + entityName);
                System.out.println("executing: " + query.getQueryString());
                for (Object o : query.list()) {
                    System.out.println("  " + o);
                }
            }
        } finally {
            session.close();
        }
    }
}

感谢任何建议!

最佳答案

解决方案是在 main 的 try block 中以 configuration.setProperty("hibernate.connection.username","u‌ sername) 等形式添加参数。 由于某种原因,IntelliJ 未将登录详细信息正确传递给服务器。

关于java - 从 IntelliJ 使用 Hibernate 连接到 PostgreSQL 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43505347/

相关文章:

.net - 如何使用具有客户端和服务器身份验证的 TLS 从 .NET 连接到 PostgreSQL?

sql - 根据时间差将时间序列数据组合成具有开始结束间隔的数据点

PostgreSQL 自动清理。是否有必要在 Postgresql.conf 中显式激活

spring - Hibernate 返回 PersistentBag 而不是 List

java - 按下按钮的 Android Firebase RecyclerView 出现无法解释的错误

java - 带有动态 XSD 的 WSDL

java - 使用java rmi激活系统的主要目的是什么?

java - 解释这个关于对象引用转换的输出?

java - 用于从 ManyToMany 连接表检索行的 HQL 查询

java - 带两个参数的 Hibernate IN 查询