Results 1 to 8 of 8
  1. #1
    anouar2002 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Problem Delete Hibernate

    Hello Guys,

    when I try to delete a row from a table 'Client', this error appears to me:
    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 :
    Java 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:
    Java 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>

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    758
    Rep Power
    8

    Default Re: Problem Delete Hibernate

    Can you post your hibernate related code? the code inside your ClientServiceImpl class.

  3. #3
    anouar2002 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem Delete Hibernate

    Here's it :
    Java 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 :

    Java 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();
    		
    	}
    
    }
    Thanks for answer

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem Delete Hibernate

    Are there any tables that have a foreign key to your client table?
    That's where I woudl look.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    anouar2002 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem Delete Hibernate

    yes.
    there is reservation table and this is its structure :
    Java Code:
    CREATE TABLE IF NOT EXISTS `reservation` (
      `MATRICULE` int(11) NOT NULL,
      `CIN` int(11) DEFAULT NULL,
      `DATEDEBUT` varchar(255) DEFAULT NULL,
      `DATEFIN` varchar(255) DEFAULT NULL,
      PRIMARY KEY (`MATRICULE`),
      KEY `FK2328D7ACC02AEED0` (`CIN`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem Delete Hibernate

    There you go.
    You can't delete a Client row without possibly invalidating that foreign key.
    You'll need to nullify or remove those 'reservations' first.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    anouar2002 is offline Member
    Join Date
    Apr 2012
    Posts
    13
    Rep Power
    0

    Default Re: Problem Delete Hibernate

    i found the solution which is adding cascade='delete'
    thank u

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Problem Delete Hibernate

    I didn't want to suggest since I noticed that 'null' was a valid value for the foreign key.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. New File.delete() problem
    By ozzyman in forum New To Java
    Replies: 7
    Last Post: 04-01-2011, 04:26 PM
  2. problem with delete row from <h:dataTable>
    By irakli_java_dev in forum JavaServer Faces (JSF)
    Replies: 1
    Last Post: 01-21-2011, 05:36 AM
  3. Replies: 2
    Last Post: 04-20-2009, 08:00 AM
  4. Replies: 1
    Last Post: 03-01-2009, 04:47 PM
  5. problem when I try to delete a file
    By tommy in forum Advanced Java
    Replies: 2
    Last Post: 07-31-2007, 02:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •