Exception:
com.mysql.jdbc.exceptions.MySQLSyntaxErrorException: Access denied for user ''@'localhost' to database 'school'
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:936)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2985)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:885)
at com.mysql.jdbc.MysqlIO.secureAuth411(MysqlIO.java:3421)
at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1247)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:2775)
at com.mysql.jdbc.Connection.(Connection.java:1555)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:285)
at java.sql.DriverManager.getConnection(DriverManager.java:582)
at java.sql.DriverManager.getConnection(DriverManager.java:154)...
Reason :
- you are passing transient object instead of persistent object for delete
Possible solution :
- Pass persistent object instead of transient object.
For example :
you would be trying something like this.Here studentToDelete is transient.
public void deleteStudent(Student studentToDelete)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
// Here studentToDelete is transient object
session.delete(studentToDelete);
session.getTransaction().commit();
session.flush();
}
We will remove persisted object and pass to delete function like below one.
public void deleteStudentA(Student studentToDelete)
{
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
// Getting persisted object of Student.
Student persistentStudent = (Student) session.get(Student.class, studentToDelete.getId());
session.delete(persistentStudent);
session.getTransaction().commit();
session.flush();
}
Read more →