2017-04-19 23 views
1

我试图通过Hibernate将数据持久化到PostgreSQL数据库,已经把我的用户/通过,检查它的工作,做了一个数据库和一些表。 当我编译,我得到一个错误从IntelliJ连接到Hibernate的PostgreSQL服务器

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

我使用的IntelliJ终极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(); 
     } 
    } 
} 

任何意见赞赏!

+1

你的配置没有设置用户名或密码,也没有设置URL,也没有使用'connection。*'属性,还要注意这不是编译时错误,但在运行时出现异常 –

+0

感谢您的回复Mark我已经在GUI中添加了登录信息我尝试添加用户名/密码到xml文件中的URL中,并使用了模式url?username = username&password = password,didn'然后我尝试使用注释掉的属性,但它给了我一个编译器错误,说这些属性不允许在内部。 我可以使用主文件中的connection.-property来定义密码吗?如果你能指出我在正确的地方使用正确的语法,我将非常感激。我认为IntelliJ会以安全的方式通过'引擎盖下'。 –

+0

什么GUI?显示的代码没有GUI。 –

回答

0

解决方法是在main的try块中添加parameters.setProperty(“hibernate.connection.username”,“u sername)等形式的参数 由于某些原因,登录信息是没有通过IntelliJ正确传递给服务器