Hello Guys,
when I try to delete a row from a table 'Client', this error appears to me:
Quote:
Exception in thread "AWT-EventQueue-0" org.hibernate.exception.ConstraintViolationExcepti on: Could not execute JDBC batch update
What should I do?
Here's the code:
Some code interface :
Code:
JButton btnSupprimer = new JButton("Supprimer");
btnSupprimer.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
ClientService clientService = new ClientServiceImpl();
clientService.supprimerClient(Integer.parseInt(comboBox.getSelectedItem().toString()));
System.out.println("delete success");
}
});
Client.hbm.xml :
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 23 mai 2012 14:09:32 by Hibernate Tools 3.3.0.GA -->
<hibernate-mapping>
<class name="tn.com.isamm.model.Client" table="CLIENT">
<id name="cin" type="java.lang.Integer">
<column name="CIN" />
<generator class="increment" />
</id>
<property name="nom" type="java.lang.String">
<column name="NOM" />
</property>
<property name="prenom" type="java.lang.String">
<column name="PRENOM" />
</property>
<property name="adresse" type="java.lang.String">
<column name="ADRESSE" />
</property>
<property name="etat" type="java.lang.String">
<column name="ETAT" />
</property>
<property name="telephone" type="java.lang.Integer">
<column name="TELEPHONE" />
</property>
<set name="reservation" table="CLIENT" inverse="false" lazy="true" >
<key>
<column name="CIN" />
</key>
<element type="string">
<column name="RESERVATION" />
</element>
</set>
</class>
</hibernate-mapping>
ClientServiceImpl :
Code:
package tn.com.isamm.serviceImpl;
import java.util.List;
import tn.com.isamm.DaoImpl.ClientDaoImpl;
import tn.com.isamm.dao.ClientDao;
import tn.com.isamm.model.Client;
import tn.com.isamm.service.ClientService;
public class ClientServiceImpl implements ClientService{
ClientDaoImpl clientDaoImpl= new ClientDaoImpl();
@Override
public void ajouterClient(Client client) {
clientDaoImpl.ajouterClient(client);
}
@Override
public Client getClient(Integer cin) {
// TODO Auto-generated method stub
return clientDaoImpl.getClient(cin);
}
@Override
public void supprimerClient(Integer cin) {
clientDaoImpl.supprimerClient(cin);
}
@Override
public List<Client> listClient() {
// TODO Auto-generated method stub
return clientDaoImpl.listClient();
}
@Override
public void modifierClient(Integer cin) {
clientDaoImpl.modifierClient(cin);
}
}
and this clientDaoImpl :
Code:
package tn.com.isamm.DaoImpl;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import InterfaceSwing.ModifierClient;
import InterfaceSwing.SuppressionC;
import tn.com.isamm.dao.ClientDao;
import tn.com.isamm.model.Client;
import util.HibernateUtil;
public class ClientDaoImpl implements ClientDao {
@Override
public void ajouterClient(Client client) {
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(client);
session.getTransaction().commit();
}
@Override
public Client getClient(Integer cin) {
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Client client;
client=(Client) session.load(Client.class, cin);
System.out.println(client.getCin());
System.out.println(client.getNom());
System.out.println(client.getPrenom());
session.getTransaction().commit();
return client;
}
@Override
public void supprimerClient(Integer cin) {
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Client client;
client=(Client) session.load(Client.class, cin);
session.delete(client);
session.getTransaction().commit();
}
@Override
public List<Client> listClient() {
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List list=session.createQuery("from Client").list();
session.getTransaction().commit();
return list;
}
@Override
public void modifierClient(Integer cin) {
Session session=HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
// String hql="update Client set nom=:newName where cin="+cin;
// Query query=session.createQuery(hql);
// query.setString("newName", "ghazi");
// query.executeUpdate();
Client client=(Client) session.load(Client.class, cin);
System.out.println("entree");
client.setNom(ModifierClient.textField_1.getText());
session.update(client);
client.setPrenom(ModifierClient.textField_2.getText());
session.update(client);
client.setAdresse(ModifierClient.textField_3.getText());
session.update(client);
client.setEtat(ModifierClient.comboBox_1.getSelectedItem().toString());
session.update(client);
client.setTelephone(Integer.parseInt(ModifierClient.textField_5.getText()));
session.update(client);
System.out.println("sortie");
session.getTransaction().commit();
}
}