-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Problem Delete Hibernate
PostPosted: Thu May 24, 2012 4:41 am 
Newbie

Joined: Thu May 24, 2012 4:08 am
Posts: 1
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();
         
    }

}


Top
 Profile  
 
 Post subject: Re: Problem Delete Hibernate
PostPosted: Fri May 25, 2012 3:26 am 
Expert
Expert

Joined: Tue Jun 16, 2009 3:36 am
Posts: 990
You should first remove the GUI part and reduce it all to a simple testcase (a JUNIT testcase).
In this way it is easier for other people to reproduce this error and investigate for the cause.
Alternatively if you want investigate by yourself I suggest you to:
-disable batch update, in this way the error message will be clearer
Code:
hibernate.jdbc.batch_size=0

-trace all jdbc-activities using p6spy, in this way you will see excactly which update-sql is generating the ConstraintsViolation exception.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.