Java 从IntelliJ通过Hibernate连接到PostgreSQL server

Java 从IntelliJ通过Hibernate连接到PostgreSQL server,java,postgresql,hibernate,jpa,jdbc,Java,Postgresql,Hibernate,Jpa,Jdbc,我试图通过Hibernate将数据持久化到PostgreSQL数据库中,输入了我的user/pass,检查它是否工作,创建了一个db和一些表。 当我编译时,我得到一个错误 org.postgresql.util.PSQLException: The server requested password-based authentication, but no password was provided. 我正在使用IntelliJ Ultimate 2017.1,并尝试使用提供的pg驱动程序和4

我试图通过Hibernate将数据持久化到PostgreSQL数据库中,输入了我的user/pass,检查它是否工作,创建了一个db和一些表。 当我编译时,我得到一个错误

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}>]
jdbc:postgresql:{database::postgres}[\?]
这是我的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>

jdbc:postgresql://localhost:5432/gigahertz
org.postgresql.Driver

这是我生成的主类

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();
        }
    }
}
包装编号hvl.dat101.GHz;
导入org.hibernate.hibernateeexception;
导入org.hibernate.Metamodel;
导入org.hibernate.query.query;
导入org.hibernate.Session;
导入org.hibernate.SessionFactory;
导入org.hibernate.cfg.Configuration;
导入javax.persistence.metamodel.EntityType;
导入java.util.Map;
/**
*/
公共班机{
私有静态最终会话工厂ourSessionFactory;
静止的{
试一试{
配置=新配置();
configure.configure();
ourSessionFactory=configuration.buildSessionFactory();
}捕获(可丢弃的ex){
抛出新异常InInitializeRerror(ex);
}
}
公共静态会话getSession()引发HibernateeException{
返回我们的sessionfactory.openSession();
}
公共静态void main(最终字符串[]args)引发异常{
最终会话=getSession();
试一试{
System.out.println(“查询所有托管实体…”);
最终元模型元模型=session.getSessionFactory().getMetamodel();
对于(EntityType EntityType:metamodel.getEntities()){
最后一个字符串entityName=entityType.getName();
最终查询=session.createQuery(“from”+entityName);
System.out.println(“正在执行:“+query.getQueryString());
对于(对象o:query.list()){
系统输出打印项次(“+o”);
}
}
}最后{
session.close();
}
}
}

任何建议,谢谢

解决这个问题的方法是将try块中的参数以configuration.setProperty(“hibernate.connection.username”、“u”的形式添加到main中‌​(姓名)等。
由于某些原因,IntelliJ没有将登录详细信息正确地传递给服务器。

解决方案是在表单configuration.setProperty(“hibernate.connection.username”)、“u‌​(姓名)等。
由于某些原因,IntelliJ没有将登录详细信息正确地传递到服务器。

您的配置没有设置用户名或密码,没有在URL中,也没有使用
连接。*
属性。还要注意,这不是编译时错误,而是运行时异常。感谢您的回复,Mark。我在GUI中添加了登录信息。我尝试使用模式URL?username=username&password=password向xml文件中的URL添加user/pass,但没有成功。然后,我尝试使用注释掉的属性,但它给了我一个编译器错误,说这些类型的属性是不允许的。我可以使用主文件中的connection.-属性来定义密码吗?如果你能在正确的地方给我指出正确的语法,我将不胜感激。我以为IntelliJ会以一种安全的方式传递这个“秘密”的ostgresql://localhost:5432/testdb?currentSchema=testdb&user=postgres&password=postgres“由于这是XML,
&
需要替换为
&连接。*
属性。还要注意,这不是编译时错误,而是运行时异常。感谢您的回复,Mark。我在GUI中添加了登录信息。我尝试使用模式URL?username=username&password=password向xml文件中的URL添加user/pass,但没有成功。然后,我尝试使用注释掉的属性,但它给了我一个编译器错误,说这些类型的属性是不允许的。我可以使用主文件中的connection.-属性来定义密码吗?如果你能在正确的地方给我指出正确的语法,我将不胜感激。我以为IntelliJ会以一种安全的方式传递这个“秘密”的ostgresql://localhost:5432/testdb?currentSchema=testdb&user=postgres&password=postgres“由于这是XML,
&
需要替换为
&取而代之。鹰眼,@markrottveel