2013-06-03 62 views
0

我想在春天mvc.I能夠將用戶保存到數據庫,但我不能從數據庫表中刪除條目的CRUD示例。我的彈簧控制器類是如下想要在春天刪除數據庫中的記錄mvc

CController.java

package project4; 
    import project4.UserDAO1; 
    import project4.User1; 
    import java.util.Map; 
    import javax.servlet.http.HttpServletRequest; 
    import javax.servlet.http.HttpServletResponse; 
    import org.springframework.stereotype.Controller; 
    import org.springframework.transaction.annotation.Transactional; 
    import org.springframework.ui.ModelMap; 
    import org.springframework.web.bind.annotation.RequestMapping; 
    import org.springframework.web.bind.annotation.RequestMethod; 
    import org.springframework.web.servlet.ModelAndView; 
    import org.springframework.web.servlet.mvc.multiaction.MultiActionController; 
    import org.springframework.web.bind.annotation.ModelAttribute; 
    import org.springframework.validation.BindingResult; 
    import org.springframework.beans.factory.annotation.Autowired; 
    import org.springframework.beans.factory.annotation.Qualifier; 


    @Controller 




public class CController{ 

private UserDAO1 userDAO; 
@Autowired 
@Qualifier("myUserDAO") 
private UserDAOImpl1 myUserDAO; 

public void setUserDAO(UserDAO1 userDAO) { 
    this.userDAO = userDAO; 
} 

    @RequestMapping(value = "/frm4/add", method = RequestMethod.POST) 
public ModelAndView add(@ModelAttribute("add") User1 user,HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 
     System.out.println("hai"); 

    userDAO.saveUser(user); 
    System.out.println("hai"); 
    return new ModelAndView("redirect:/list.htm"); 
} 

    @RequestMapping(params = "/deletedpage/delete", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView delete(@ModelAttribute("delete") User1 user,HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 
     System.out.println("hai"); 
    userDAO.deleteUser(user); 
    return new ModelAndView("redirect:/list.htm"); 
} 


    @RequestMapping(params = "find", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView find(@ModelAttribute("find") User1 user,HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 
        userDAO.findUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 


    @RequestMapping(params = "update", method = RequestMethod.POST) 
    @Transactional 
    public ModelAndView update(@ModelAttribute("update") User1 user,HttpServletRequest request, 
      HttpServletResponse response) throws Exception { 
        userDAO.updateUser(user); 
         return new ModelAndView("redirect:list.htm"); 
    } 


    @RequestMapping(value = "list", method = RequestMethod.GET) 
public ModelAndView list(HttpServletRequest request, 
     HttpServletResponse response) throws Exception { 


    ModelMap modelMap = new ModelMap(); 
    modelMap.addAttribute("userList", userDAO.listUser()); 
    modelMap.addAttribute("user", new User1()); 
    return new ModelAndView("list", modelMap); 
} 
} 

和我的休眠模板類是如下

UserDAOImpl1.java

package project4; 
    import project4.User1; 
    import java.util.List; 
    import org.hibernate.Query; 
    import org.hibernate.Session; 
    import org.hibernate.SessionFactory; 
    import org.hibernate.cfg.Configuration; 
    import org.springframework.orm.hibernate3.HibernateTemplate; 
    import org.springframework.transaction.annotation.Propagation; 
    import org.springframework.transaction.annotation.Transactional; 

public class UserDAOImpl1 implements UserDAO1 { 

private HibernateTemplate hibernateTemplate; 

public void setSessionFactory(SessionFactory sessionFactory) { 
    this.hibernateTemplate = new HibernateTemplate(sessionFactory); 
} 

@Override 
@Transactional(propagation=Propagation.REQUIRED, readOnly=false) 
public void saveUser(User1 user) { 
    try { 
    System.out.println (user.getId()); 
    hibernateTemplate.save(user); 
    }catch (RuntimeException re){ 
     throw re; 
    } 
} 

@Override 
@SuppressWarnings("unchecked") 
public List<User1> listUser() { 
    List<User1> result = hibernateTemplate.find("from User1"); 
    System.out.println("hai");  
    return result; 
} 

    @Override 
public void deleteUser(Long id1) { 
    System.out.println(id1); 
    hibernateTemplate.delete("from User1 where USER_ID=:" +id1); 
    System.out.println("hello"); 
} 

    @Override 
    public List<User1> findUser(User1 user) { 
     List<User1> result =hibernateTemplate.find("from User1 where USER_ID=:" +user.getId()); 
     return result; 

    } 

    @Override 
    public void updateUser(User1 user) { 
     hibernateTemplate.update(user); 

    } 
} 

和我的用戶DAO類是如下

User1.java

package project4; 
import javax.persistence.Column; 
import javax.persistence.Entity; 
import javax.persistence.GeneratedValue; 
import javax.persistence.Id; 
import javax.persistence.Table; 

    @Entity 
    @Table(name="user") 
    public class User1 { 

     private long id; 
     private String name; 
     private String password; 
     private String gender; 
     private String country; 
     private int oid; 

     @Id 
     @GeneratedValue 
     @Column(name="USER_OID") 
     public int getOid(){ 
       return oid; 
     } 

     public void setOid(int id){ 
      this.oid=id; 
     } 



     @Column(name="USER_ID") 
     public long getId() { 
      return id; 
     } 
     public void setId(long id) { 
      this.id = id; 
     } 



     @Column(name="USER_NAME") 
     public String getName() { 
      return name; 
     } 
     public void setName(String name) { 
       this.name = name; 
     } 

     @Column(name="USER_PASSWORD") 
     public String getPassword() { 
      return password; 
     } 
     public void setPassword(String password) { 
      this.password = password; 
     } 

     @Column(name="USER_GENDER") 
     public String getGender() { 
       return gender; 
     } 
     public void setGender(String gender) { 
      this.gender = gender; 
     } 

     @Column(name="USER_COUNTRY") 
     public String getCountry() { 
       return country; 
     } 
     public void setCountry(String country) { 
      this.country = country; 
     } 
} 

我有兩個ID費爾德,OID是用於存儲在默認情況下和ID存儲值從獲得所產生的價值用戶

現在,當我嘗試刪除一個用戶我得到

SEVERE: Servlet.service() for servlet CController threw exception 
org.hibernate.MappingException: Unknown entity: java.lang.String 
at 
org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) 
at 
    org.hibernate.impl.SessionImpl.getEntityPersister 

org.hibernate.event.def.DefaultDeleteEventListener. 
    onDelete(DefaultDeleteEventListener.java:99) 
at org.hibernate.event.def.DefaultDeleteEventListener. 
    onDelete(DefaultDeleteEventListener.java:73) 
at org.hibernate.impl.SessionImpl.fireDelete(SessionImpl.java:956) 
at org.hibernate.impl.SessionImpl.delete(SessionImpl.java:934) 
at org.springframework.orm.hibernate3.HibernateTemplate$25. 
     doInHibernate(HibernateTemplate.java:843) 
at org.springframework.orm.hibernate3.HibernateTemplate. 
     doExecute(HibernateTemplate.java:406) 
at org.springframework.orm.hibernate3.HibernateTemplate. 
     executeWithNativeSession(HibernateTemplate.java:374) 
at org.springframework.orm.hibernate3.HibernateTemplate. 
     delete(HibernateTemplate.java:837) 
at org.springframework.orm.hibernate3.HibernateTemplate. 
       delete(HibernateTemplate.java:833) 
at project4.UserDAOImpl1.deleteUser(UserDAOImpl1.java:50) 
at project4.CController.delete(CController.java:52) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 
at sun.reflect.DelegatingMethodAccessorImpl. 
     invoke(DelegatingMethodAccessorImpl.java:25) 
at java.lang.reflect.Method.invoke(Method.java:597) 
at org.springframework.web.bind.annotation.support. 
     HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176) 
at org.springframework.web.servlet.mvc.annotation. AnnotationMethodHandlerAdapter. 
     invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:436) 
at org.springframework.web.servlet.mvc.annotation. 
     AnnotationMethodHandlerAdapter. 
      handle(AnnotationMethodHandlerAdapter.java:424) 
at org.springframework.web.servlet.DispatcherServlet. 
     doDispatch(DispatcherServlet.java:790) 
at org.springframework.web.servlet.DispatcherServlet. 
       doService(DispatcherServlet.java:719) 
at org.springframework.web.servlet.FrameworkServlet. 
      processRequest(FrameworkServlet.java:669) 
at org.springframework.web.servlet.FrameworkServlet. 
     doPost(FrameworkServlet.java:585) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:710) 
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803) 
at org.apache.catalina.core.ApplicationFilterChain. 
      internalDoFilter(ApplicationFilterChain.java:290) 
at org.apache.catalina.core.ApplicationFilterChain. 
     doFilter(ApplicationFilterChain.java:206) 
at org.apache.catalina.core.StandardWrapperValve. 
      invoke(StandardWrapperValve.java:233) 
at org.apache.catalina.core.StandardContextValve. 
     invoke(StandardContextValve.java:175) 
at org.apache.catalina.core.StandardHostValve. 
      invoke(StandardHostValve.java:128) 
at org.apache.catalina.valves.ErrorReportValve. 
      invoke(ErrorReportValve.java:102) 
at org.apache.catalina.core.StandardEngineValve. 
      invoke(StandardEngineValve.java:109) 
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286) 
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844) 
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler. 
     process(Http11Protocol.java:583) 
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447) 
at java.lang.Thread.run(Thread.java:619) 

因此,當我嘗試刪除ID爲23 IA用戶能夠將值從jsp傳遞給控制器​​,然後傳遞給hibernate模板類中的刪除函數,其中我打印了id,並且值23已成功打印。但刪除查詢沒有得到執行。是否有任何錯誤查詢?

回答

2

HibernateTemplate.delete將要刪除的對象作爲參數(您傳遞查詢)。

要麼改變hibernateTemplate.delete("from User1 where USER_ID=:" +id1);到: hibernateTemplate.delete(hibernateTemplate.get(User1.class, id1));,或將其更改爲:

hibernateTemplate.bulkUpdate("DELETE User1 where id=?", id1);

+0

非常感謝bunch..that幫助 – Ezhil