Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Deleting an element from a list

  1. #1

    Default Deleting an element from a list

    Hello
    I'm developping a web application with Hibernate, spring and spring mvc .
    I want to delete an Employee from a ist displayed but I 'm unable to program the right button.
    This is my code for EmployeDAOImpl:
    Code:
    @Override
    	public void createEmploye(Employes employe) {
    		
    		hibernateTemplate.saveOrUpdate(employe);
     
    	}
     
    	@Override
    	@SuppressWarnings("unchecked")
    	public List<Employes> findallEmploye() {
    		
    		employeList= hibernateTemplate.find("from Employes");
    		return employeList;
    	}
     
    	@Override
    	public void deleteEmploye(int idEmp) {
    		Employes employe=(Employes)hibernateTemplate.get(Employes.class,idEmp);	
    		hibernateTemplate.delete(employe);
    		
    	}
    Here are my controllers
    [CODE]public class EmployeControllerAdd extends MultiActionController {

    private EmployeDAO employeDAO;

    public void setEmployeDAO(EmployeDAO employeDAO) {
    this.employeDAO = employeDAO;
    }

    public ModelAndView add(HttpServletRequest request,
    HttpServletResponse response, Employes employe) throws Exception {
    employeDAO.createEmploye(employe);
    return new ModelAndView("redirect:list.htm");
    }[CODE]

    Code:
    public class EmployeControllerDelete extends MultiActionController {
     
    	private EmployeDAO employeDAO;
    	private int idEmp;
     
    	public void setEmployeDAO(EmployeDAO employeDAO) {
    		this.employeDAO = employeDAO;
    	}
    	
    	public void setidEmp(int  idEmp) {
    		this.idEmp = idEmp;
    	}
     
    	public ModelAndView delete(HttpServletRequest request,
    			HttpServletResponse response, int idEmp) throws Exception {
    		
    	
    		employeDAO.deleteEmploye(idEmp);
    	
    		return new ModelAndView("redirect:list.htm");
    	}
    Code:
    public ModelAndView list(HttpServletRequest request,
    			HttpServletResponse response) throws Exception {
    		ModelMap modelMap = new ModelMap();
    		modelMap.addAttribute("employeList", employeDAO.findallEmploye());
    		modelMap.addAttribute("employe", new Employes());
    		return new ModelAndView("employeForm", modelMap);
    	}
    and here is my

    employeForm.jsp
    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    	pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
    <%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
    <%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <style type="text/css">
    .even {
    	background-color: silver;
    }
    </style>
    <title>Registration Page</title>
    </head>
    <body>
     
     <center> <h1>Ajouter un Employe</h1> </center> 
     
    <form:form action="add.htm" commandName="employe">
    	<table>
    	
    		<tr>
    			<td>Nom :</td>
    			<td><form:input path="firstame" /></td>
    		</tr>
    		
    		<tr>
    			<td>Prenom:</td>
    			<td><form:input path="lastname" /></td>
    		</tr>
    		<tr>
    			<td>mail:</td>
    			<td><form:input path="mail" /></td>
    		</tr>
    		<tr>
    			<td>job:</td>
    			<td><form:input path="function" /></td>
    		</tr>
    		<tr>
    			<td>fiche:</td>
    			<td><form:input path="sheet" /></td>
    		</tr>
    	
    	<tr>
    			<td>RIB:</td>
    			<td><form:input path="rib" /></td>
    		</tr>
    		<tr>
    			<td>situation maritale:</td>
    			<td><form:input path="maritalStatus" /></td>
    		</tr>
     
    	<tr>
    			<td>Nombre des enfants :</td>
    			<td><form:input path="numberOfChildren" /></td>
    		</tr>
    	
    		<tr>
    			<td colspan="2"><input type="submit" value="Register"></td>
    		</tr>
    	</table>
    </form:form>
     
     
     
    <c:if test="${fn:length(employeList)> 0}">
    	<table cellpadding="5">
    		<tr class="even">
    	
    			<th>prenom</th>
    			<th>nom</th>
    			<th>mail</th>
    			<th>job</th>
    			<th>fiche</th>
    			<th>RIB</th>
    			<th>situation maritale</th>
    			<th>Nombre des enfants</th>
    		</tr>
    		
    		<form:form action="delete.htm" commandName="employe">
    		
    		<c:forEach items="${employeList}" var="employe" varStatus="status">
    			<tr class="<c:if test="${status.count % 2 == 0}">even</c:if>">
    				
    				<td>${employe.firstame}</td>
    				<td>${employe.lastname}</td>
    				<td>${employe.mail}</td>
    				<td>${employe.function}</td>
    				<td>${employe.sheet}</td>
    				<td>${employe.rib}</td>
    				<td>${employe.maritalStatus}</td>
    				<td>${employe.numberOfChildren}</td>
     
    			<td height="28" colspan="5" bgcolor="#99B1CC">
            	
    		</td>
          
            <td><a href="<c:url value="delete.htm?idEmp=${employe.idemployees}"/>">Supprimer</a></td>
            <td><a href="<c:url value="/do/edit?id=${personne.id}"/>">Modifier</a></td>
    			</tr>
    			
    		</c:forEach>
    	 </form:form>	 
    	</table>
    </c:if>
     <a href="<c:url value="/accueil.htm"/>">Home</a>
    </body>
    </html>
    for the button of deleting I would highlight the code:
    Code:
    <td><a href="<c:url value="delete.htm?idEmp=${employe.idemployees}"/>">Supprimer</a></td>
    Now the error is
    Code:
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [int]: No default constructor found; nested exception is java.lang.NoSuchMethodException: int.<init>()
    	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:81)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.newCommandObject(MultiActionController.java:521)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:468)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
    	org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    Help It's very urgent !
    That drives me crazy!
    Last edited by ibtissem07; Mar 25th, 2010 at 07:05 PM.

  2. #2
    Join Date
    Mar 2009
    Posts
    126

    Default

    Well, I think you need to change field data type to "java.lang.Integer" from "int".This seems to me like a case of incorrect mapping between Hibernate and Java Types.So you would have to change your POJO's , HBM as well as Dao accordingly to make the EmpId field as "Integer" and not "int".
    You need to pass an Integer in your DAO as well :
    Code:
    public void deleteEmploye(Integer idEmp)
    I think this should sort it out.Incase ,it doesn't please post your Bean config XML.
    I hope this helps.
    Cheers,
    Sushant

  3. #3

    Default

    Hello Thank you for your suggestion .
    In fact, the type was Integer but I change it to int
    because I have almost the same error. That's it :

    Code:
    org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [java.lang.Integer]: No default constructor found; nested exception is java.lang.NoSuchMethodException: java.lang.Integer.<init>()
    	org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:81)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.newCommandObject(MultiActionController.java:521)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:468)
    	org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
    	org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
    	org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
    	org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
    	org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
    	org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:571)
    	org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:501)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    	javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    this my Dao Object
    Code:
    package com.proxymit.grh.model;
    
    /
    
    
    /**
     * Employes generated by hbm2java
     */
    @Entity
    @Table(name = "employes", catalog = "proxym")
    public class Employes implements java.io.Serializable {
    
    	private Integer idemployees;
    	private String firstame;
    	private String lastname;
    	private String mail;
    	private String function;
    	private String sheet;
    	private Integer rib;
    	private String maritalStatus;
    	private Integer numberOfChildren;
    
    	public Employes() {
    		super();
    	}
    
    	public Employes(Integer idemployees) {
    		 setIdemployees(getIdemployees());
    	}
    	
    
    	public Employes(String firstame, String lastname, String mail,
    			String function) {
    		this.firstame = firstame;
    		this.lastname = lastname;
    		this.mail = mail;
    		this.function = function;
    	}
    
    	
    	
    	public Employes( String firstame, String lastname,
    			String mail, String function, String sheet, Integer rib,
    			String maritalStatus, Integer numberOfChildren) {
    		
    		
    		
    		
    		this.firstame = firstame;
    		this.lastname = lastname;
    		this.mail = mail;
    		this.function = function;
    		this.sheet = sheet;
    		this.rib = rib;
    		this.maritalStatus = maritalStatus;
    		this.numberOfChildren = numberOfChildren;
    		
    		
    		
    	}
    
    	public Employes(Integer idemployees, String firstame, String lastname,
    			String mail, String function, String sheet, Integer rib,
    			String maritalStatus, Integer numberOfChildren) {
    			 setLastname(lastname);
    			 setFirstame(firstame);
    			 setMail(mail);
    			 setFunction(function);
    			 setSheet(sheet);
    			 setNumberOfChildren(numberOfChildren);
    			 setMaritalStatus(maritalStatus);
    			 setRib(rib);
    			 setIdemployees(idemployees);
    			 
    			 }
    	
    	
    	public Employes(Employes employe) {
    			 setLastname(getLastname());
    			 setFirstame( getFirstame());
    			 setMail(getMail());
    			 setFunction(getFunction());
    			 setSheet(getSheet());
    			 setNumberOfChildren( getNumberOfChildren());
    			 setMaritalStatus(getMaritalStatus());
    			 setRib(getRib());
    			 setIdemployees(getIdemployees());
    			 
    			 }
    	
    	
    	@Id
    	@GeneratedValue(strategy = IDENTITY)
    	@Column(name = "idemployees", unique = true, nullable = false)
    	public Integer getIdemployees() {
    		return this.idemployees;
    	}
    
    	public void setIdemployees(Integer idemployees) {
    		this.idemployees = idemployees;
    	}
    
    	
    
    	@Column(name = "firstame", nullable = false, length = 45)
    	public String getFirstame() {
    		return this.firstame;
    	}
    
    	public void setFirstame(String firstame) {
    		this.firstame = firstame;
    	}
    
    	@Column(name = "lastname", nullable = false, length = 45)
    	public String getLastname() {
    		return this.lastname;
    	}
    
    	public void setLastname(String lastname) {
    		this.lastname = lastname;
    	}
    
    	@Column(name = "mail", nullable = false, length = 45)
    	public String getMail() {
    		return this.mail;
    	}
    
    	public void setMail(String mail) {
    		this.mail = mail;
    	}
    
    	@Column(name = "function", nullable = false, length = 45)
    	public String getFunction() {
    		return this.function;
    	}
    
    	public void setFunction(String function) {
    		this.function = function;
    	}
    
    	@Column(name = "sheet")
    	public String getSheet() {
    		return this.sheet;
    	}
    
    	public void setSheet(String sheet) {
    		this.sheet = sheet;
    	}
    
    	@Column(name = "rib")
    	public Integer getRib() {
    		return this.rib;
    	}
    
    	public void setRib(Integer rib) {
    		this.rib = rib;
    	}
    
    	@Column(name = "MaritalStatus", length = 45)
    	public String getMaritalStatus() {
    		return this.maritalStatus;
    	}
    
    	public void setMaritalStatus(String maritalStatus) {
    		this.maritalStatus = maritalStatus;
    	}
    
    	@Column(name = "number_of_children")
    	public Integer getNumberOfChildren() {
    		return this.numberOfChildren;
    	}
    
    	public void setNumberOfChildren(Integer numberOfChildren) {
    		this.numberOfChildren = numberOfChildren;
    	}
    	
    	
    }
    and here is my xml configuration
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:p="http://www.springframework.org/schema/p"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    	<!--Choisir une stratégie de résolution de vue BeanNameViewResolver -->
    	
    	<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" />
    
    	<!-- Configuration de DataSource -->
    	
    	<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    		<property name="url" value="jdbc:mysql://localhost:3306/proxym"/>
    		<property name="username" value="root"/>
    		<property name="password" value=""/>
    	</bean>
    	
    		<!-- Configuration de Session  -->
    		
    	<bean id="mySessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    		<property name="dataSource" ref="myDataSource" />
    		<property name="annotatedClasses">
    			<list>
    			<value>com.proxymit.grh.model.Vacation</value>
    			<value>com.proxymit.grh.model.Employes</value>
    			<value>com.proxymit.grh.model.Vacationtype</value>	
    			<value>com.proxymit.grh.model.Experiences</value>
    			<value>com.proxymit.grh.model.Adressemployes</value>
    			<value>com.proxymit.grh.model.Adresstrainee</value>
    			<value>com.proxymit.grh.model.Cv</value>
    			<value>com.proxymit.grh.model.Permission</value>
    			<value>com.proxymit.grh.model.Phoneemployees</value>
    			<value>com.proxymit.grh.model.Phonetrainee</value>
    			<value>com.proxymit.grh.model.Rules</value>
    			<value>com.proxymit.grh.model.Salary</value>
    			<value>com.proxymit.grh.model.Technologie</value>									
    			<value>com.proxymit.grh.model.Trainee</value>
    			</list>
    		</property>
    		<property name="hibernateProperties">
    			<props>
    				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
    				<prop key="hibernate.show_sql">true</prop>
    				
    			</props>
    		</property>
    	</bean>
    	
    	<!-- Configuration  de bean de la couche daoImpl --> 
    	
    	
    	<bean id="myvacationDAO" class="com.proxymit.grh.service.interfImpl.VacationDAOImpl">
    		<property name="sessionFactory" ref="mySessionFactory"/>
    	</bean>
    
    		<bean name="/vacation/*.htm" class="com.proxymit.grh.web.VacationController" >
    		<property name="vacationDAO" ref="myvacationDAO" />
    
    	</bean>
    
    	<bean id="myemployeDAO" class="com.proxymit.grh.service.interfImpl.EmployeDAOImpl">
    		<property name="sessionFactory" ref="mySessionFactory"/>
    	</bean>
    	
    	<bean name="/employe/add.htm" class="com.proxymit.grh.web.EmployeControllerAdd" >
    		<property name="employeDAO" ref="myemployeDAO" />
    	</bean>
    	<bean name="/employe/list.htm" class="com.proxymit.grh.web.EmployeControllerList" >
    		<property name="employeDAO" ref="myemployeDAO" />
    	</bean>
    	<bean name="/employe/delete.htm" class="com.proxymit.grh.web.EmployeControllerDelete" >
    		<property name="employeDAO" ref="myemployeDAO" />
    	</bean>
    
    	<bean id="myvacationtypeDAO" class="com.proxymit.grh.service.interfImpl.VacationtypeDAOImpl">
    		<property name="sessionFactory" ref="mySessionFactory"/>
    	</bean>
    		<bean name="/vacationtype/*.htm" class="com.proxymit.grh.web.VacationtypeController" >
    		<property name="vacationtypeDAO" ref="myvacationtypeDAO" />
    	</bean>
    
    	<bean id="mysalaryDAO" class="com.proxymit.grh.service.interfImpl.SalaryDAOImpl">
    		<property name="sessionFactory" ref="mySessionFactory"/>
    	</bean>
    		<bean name="/salary/*.htm" class="com.proxymit.grh.web.SalaryController" >
    		<property name="salaryDAO" ref="mysalaryDAO" />
    	</bean>
    
    	<bean name="/accueil.htm"  class="com.proxymit.grh.web.AcceuilController"/>
    	
    	
    	
    </beans>

    I would appreciate if you could help to carry on my project.
    Thank you.

  4. #4
    Join Date
    Mar 2009
    Posts
    126

    Default

    Can you check if you are using java.persistence.entity package and not the org.hibernate.annotations.Entity in your Employees class? And also check and post if you are able to run other database operations except delete.This will help in tracking down the problem.

  5. #5
    Join Date
    Mar 2009
    Posts
    126

    Default

    This can also be a comptability and support issue of Spring and Hibernate jars you are using.Check the versions.Also check if you have the persistence API jar in your classpath.Check your hibernate version , it must be atleast 3.2.1.GA

  6. #6
    Join Date
    Mar 2009
    Posts
    126

    Default

    Also add the following
    Code:
    <context:annotation-config />
    <context:component-scan base-package="com.proxymit.grh.model" />
    in your bean config XML.See if that helps.

  7. #7

    Default

    Thank you

    I tried the two first solutions but it doesn't work .
    For the third I didn't understand where to put that it shows an error when adding that to my onfi XML.
    I think the problem must be something concerning initialisation mainly for the idemployees I must select the right value to delete it but I couldn't .
    I tried to add element to the table and it works but without putting a value for idemployees because it's an auto-increment value.
    I've changed the delete method in my controller like this
    Code:
    public ModelAndView delete(HttpServletRequest request,
    			HttpServletResponse response, Integer idemployees) throws Exception {
    
    		employeDAO.deleteEmploye(Integer.parseInt(request.getParameter("idemployees")));
    	
    		return new ModelAndView("redirect:list.htm");
    	}
    and it doesn't work
    I try even to check my access to the method deleteEmploy method in my service layer
    Code:
    public void deleteEmploye(Integer idemployees) {
    		System.out.println("rrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrrr");
    		Employes employe=(Employes)hibernateTemplate.get(Employes.class,idemployees);	
    		hibernateTemplate.delete(employe);
    	
    		
    	}
    but nothing was displayed.
    ????

  8. #8
    Join Date
    Mar 2009
    Posts
    126

    Default

    For the third option , You need to add the following in the beans element's attribute xsi:schemaLocation:
    Code:
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    .
    It should not throw the error then.Try that.I will see till then what else can this be about ?

  9. #9
    Join Date
    Mar 2009
    Posts
    126

    Default

    Also try one more thing :
    Just use the following code in your DAO method.hard-code an employee id and see if that works at the first place.This may let us know where exactly the problem is.You can use some thing like this
    Code:
    employeDAO.deleteEmploye(new Integer(your employee id value));
    like 
    employeDAO.deleteEmploye(new Integer(2));
    and see if this works.

  10. #10

    Default

    Hello
    I tried many things and then I decided to try another class instead of the class Employes and it works.
    I think the problem is due to my contructors and the access to the database.
    I'm trying to resolve the problem .
    I hope it will work!

Posting Permissions

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