0

我正在 spring mvc 中尝试 CRUD 示例。我能够将用户保存到数据库中,但我无法从数据库表中删除条目。我的 spring 控制器类如下

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类如下

用户1.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 的用户时,我能够将值从 jsp 传递给控制器​​,然后传递给休眠模板类中的删除函数,在该函数中我打印 id 并且值 23 被成功打印。但是删除查询没有被执行。查询中是否有任何错误?

4

1 回答 1

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);

于 2013-06-03T05:21:25.477 回答