Ciao ragazzi,
preciso che sono alle prime esperienze con Hibernate. Dopo aver letto tanta documentazione ho provato a buttar giù un po di codice, ma non riesco a spiegarmi perchè riesco ad eseguire un'inserimento sulla tabella del mio DB ma proprio non riesco ad eseguire una cancellazione....
Il DBMS in uso è MySQL 5.0 e la tabella del mio DB si chiama "Contact" e prevede i campi id (int), firstName (String), lastName (String) ed email (String).
Nello specifico, la procedura che esegue correttamente l'inserimento in Contact è:
codice:
public void insertContact(String firstName, String lastName, String email ) {
Session session = null;
Contact contact = new Contact();
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
System.out.println("Inserting Record");
contact.setFirstName(firstName);
contact.setLastName(lastName);
contact.setEmail(email);
session.save(contact);
tx.commit();
System.out.println("Done");
session.flush();
session.close();
}
Mentre la procedura per cancellare una riga dalla tabella Contact:
codice:
public void deleteContact(String id) {
Session session = null;
SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
String hql = "delete Contact where id = 2";
Query query = session.createQuery(hql);
int row = query.executeUpate();
tx.commit();
session.close();
if (row == 0){
System.out.println("Didn't deleted any row!");
}
else{
System.out.println("DeletedRow: " + row);
}
}
Ebbene, come dicevo, mentre l'inserimento va tranquillamente a buon fine, quando provo ad eseguire una cancellazione il messaggio d'errore è:
Hibernate: delete from CONTACT contact0_ where (id=2)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not execute update query
at org.hibernate.exception.ErrorCodeConverter.convert (ErrorCodeConverter.java:70)
at org.hibernate.exception.JDBCExceptionHelper.conver t(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.UpdateStatementExecutor.exec ute(UpdateStatementExecutor.java:76)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeU pdate(QueryTranslatorImpl.java:294)
at org.hibernate.impl.SessionImpl.executeUpdate(Sessi onImpl.java:808)
at org.hibernate.impl.QueryImpl.executeUpate(QueryImp l.java:89)
at prove.FirstExample.deleteContact(FirstExample.java :94)
at prove.FirstExample.start(FirstExample.java:41)
at prove.FirstExample.main(FirstExample.java:19)
Caused by: java.sql.SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'contact0_ where (id=2)' at line 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.ja va:2851)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:15 34)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java :1625)
at com.mysql.jdbc.Connection.execSQL(Connection.java: 2297)
at com.mysql.jdbc.Connection.execSQL(Connection.java: 2226)
at com.mysql.jdbc.PreparedStatement.executeInternal(P reparedStatement.java:1812)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1906)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1831)
at com.mysql.jdbc.PreparedStatement.executeUpdate(Pre paredStatement.java:1693)
at org.hibernate.hql.ast.UpdateStatementExecutor.exec ute(UpdateStatementExecutor.java:67)
... 6 more
Faccio notare che ho provato anche ad usare "delete from
Contact contact where id = 2" al posto di "delete Contact where id = 2" ma in questo caso il risultato è addirittura una java.lang.NullPointerException....
Mi sembra evidente che non sia un problema di connessione al DB o di configurazione visto che l'inserimento funziona.
Ed è quasi superfluo dire che ho ampiamente ricercato la soluzione su google masenza venirne a capo....
Qualcuno riesce a darmi suggerimenti...? Please help me