【问题标题】:Connecting to PostgreSQL-server with Hibernate from IntelliJ从 IntelliJ 使用 Hibernate 连接到 PostgreSQL 服务器
【发布时间】:2017-09-16 06:01:18
【问题描述】:

我正在尝试通过 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();
        }
    }
}

任何建议表示赞赏!

【问题讨论】:

  • 您的配置没有设置用户名或密码,不在 URL 中,也没有使用 connection.* 属性。另请注意,这不是编译时错误,而是运行时异常。
  • 感谢您的回复,马克。我在 GUI 中添加了登录信息。我尝试使用模式 url?username=username&password=password 将用户/密码添加到 xml 文件中的 URL,但这不起作用。然后我尝试使用注释掉的属性,但它给了我一个编译器错误,说 中不允许使用这些属性。我可以使用主文件中的 connection.-property 来定义密码吗?如果您能在正确的地方指出正确的语法,我将不胜感激。我认为 IntelliJ 会以安全的方式通过这个“幕后”。
  • 可以使用的示例连接字符串是这样的:"jdbc:postgresql://localhost:5432/testdb?currentSchema=testdb&amp;user=postgres&amp;password=postgres")
  • 由于这是 XML,&amp;amp; 需要替换为 &amp;amp;
  • 鹰眼,@MarkRotteveel

标签: java postgresql hibernate jpa jdbc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2020-01-07
    • 1970-01-01
    • 2019-10-08
    • 1970-01-01
    • 2020-01-24
    • 1970-01-01
    • 2013-05-07
    • 2022-11-02
    • 1970-01-01
    相关资源
    最近更新 更多