我正在尝试运行一个简单的Java项目来查看休眠的工作原理,但是当我执行代码时,它给出了休眠异常,我正在使用mysql 5.6和eclipse neon,我什至检查了授予访问权限,而且一切似乎都很好,请帮助这是代码:

main.java

    package com.demo;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

public class Main {

 public static void main(String[] args) {
    // TODO Auto-generated method stub

    Session session = null;
    SessionFactory sessionFactory= null;

    //create sessionFactory

    sessionFactory= new  Configuration().configure().buildSessionFactory();


    // create Session
    session=sessionFactory.openSession();
    // create and begin transaction

    Transaction t = session.beginTransaction();

    //create contact object and set values to the object

    Contact contact = new Contact();

    contact.setId(101);
    contact.setFirstName("Dina");
    contact.setLastName("adams");
    contact.setEmailID("[email protected]");

    // insert record by saving session

    session.save(contact);

    session.flush();

    // commit transaction

    t.commit();

    //close the session

    session.close();




  }

}


contact.java

package com.demo;

public class Contact {
//properties

private String firstName;
private String lastName;
private String emailID;
private int id;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
 }
 public String getLastName() {
 return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailID() {
return emailID;
}
public void setEmailID(String emailID) {
this.emailID = emailID;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
 }


 }


hibernate.cfg.xml

  <?xml version='1.0' encoding='utf-8'?>
   <!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/emp</property>
<property name="hibernate.connection.usename">rajesh</property>
<property name="hibernate.connection.password">Nepal123</property>
<property name="hibernate.connection.poolsize">10</property>
<property name="show_sql">true</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>

<!-- Mapping file  -->
<mapping resource="Contact.hbm.xml"/>

</session-factory>

</hibernate-configuration>


contact.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
<class name = "com.demo.Contact" table="Contact">
<id column= "ID" name="id" type= "int" >
<generator class = "assigned"></generator>
</id>

<property name = "firstName">
<column name="FIRST NAME"></column>
</property>
<property name="lastName">
<column name="LAST NAME"></column>
</property>
<property name="emailID">
<column name ="EMAIL ID"></column>

</property>
</class>
</hibernate-mapping>


错误信息:-

  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:426)
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 com.demo.Main.main(Main.java:25)
Caused by: java.sql.SQLException: Access denied for user ''@'localhost' (using password: YES)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:963)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:875)
at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1712)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1228)
at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2253)
at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2284)
at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2083)
at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:806)
at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:47)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:410)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:328)
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:423)
... 5 more

最佳答案

查看此标签:

<property name="hibernate.connection.usename">rajesh</property>


名称应为hibernate.connection.username而不是hibernate.connection.usename。

09-26 06:27