Spring MVC + Hibernate +一对一映射:session.save

时间:2015-04-29 14:16:23

标签: spring hibernate spring-mvc

问题定义:

USER_TABLE和USER_DETAIL_TABLE是使用userid字段映射的OneToOne。 面对两个问题!

  1. 在保存UserDetailTable时遇到nullpointer异常。

    • 首先设置UserTable,然后保存USerDetailTable。
  2. userid值生成为0,但我使用nullable = false。 - 有时我看到USER_TABLE的userid值为1,USER_DETAIL_TABLE的userid值为0.特别是当我已经为' admin'在创建表时。

  3. 我想要默认' admin'在创建数据库时插入的userid。

  4. 我正在使用以下网站提供的布局 http://websystique.com/springmvc/spring-4-mvc-and-hibernate4-integration-example-using-annotations/

    使用上面示例中的AppInitializer,AppConfig和Hibernate配置。

    我更新AppConfig以包含tile和jsp页面,两个viewresolvers分别有一个和两个排序。

    为了简短起见,提供代码片段

    User.Class

             @Entity
        @Table(name="USER_TABLE")
    
        public class User{
    
            @Id
            @GeneratedValue(strategy = GenerationType.IDENTITY)
            @Column(name="userid", nullable=false)
            private int userid;
       ... 
    ...
        }
    

    UserDetail.Class

     @Entity @Table(name="USER_DETAIL_TABLE")
    public class UserDetail {
    
        //@Id
        @Column(name="userid", nullable=false)
        @Id @GeneratedValue(generator = "genuserid")
        @GenericGenerator(name = "genuserid", strategy="foreign", parameters =  @Parameter(value="USER_TABLE", name = "property") )
        private int userid;
    
    
        private String firstname;
        private String lastname;
    
    
        //@PrimaryKeyJoinColumn
        @OneToOne(cascade = CascadeType.ALL)
        @JoinColumn(name="userid")
        private User user;
    
    
        public User getUser() {
            return user;
        }
        public void setUser(User user) {
            this.user = user;
        }
    

    UserDetailDao Class

    @Repository("UserDetailDao")
    public class UserDetailDaoImpl extends AbstractDao implements UserDetailDao {
    
    
        @Override
        public void saveUserDetail( UserDetail userdetail, User user) {
                // some time  userdetail.userid is 0!!
                //  and user.userid is 1 !!
    
            // setting User table info
            userdetail.setUser(user);
    
            //persist(userdetail);
    
            //getSession().beginTransaction(
            getSession().save(userdetail);
            //getSession().getTransaction().commit();
    
        }
    

    UserDetailService类

    @Service("userdetailService")
    @Transactional
    public class UserDetailServiceImpl implements  UserDetailService{
    
        @Autowired
        private UserDetailDao userDetailDao;
    
        @Override
        public void saveUserDetail(UserDetail userdetail,User user) {
    
            userDetailDao.saveUserDetail(userdetail, user);
        }
    

    控制器类

    @Controller
    public class WebMainController {
    
    
        @Autowired
        UserDetailService userDetailService;
    
     //For add and update person both
            @RequestMapping(value= "useradd", method = RequestMethod.POST)
            public String addPerson(
      @ModelAttribute("userdetail") UserDetail userdetail,BindingResult resultUser,
      @ModelAttribute("user") User user,BindingResult resultUserdetail
                                    )
           {
    
                    //UserDetail userdetail = new UserDetail();
                    //userdetail.setUser(user);
                    userDetailService.saveUserDetail(userdetail, user);
    
                return "redirect:/useradd";
            }
    

    使用MySQL:SQL脚本

    CREATE  TABLE user_table (
      userid INT(10) not null auto_increment, 
      loginname VARCHAR(16) NOT NULL ,
      password VARCHAR(32) NOT NULL ,
    
      PRIMARY KEY (userid)
      )ENGINE=InnoDB  DEFAULT CHARSET=utf8;
    
     CREATE  TABLE user_detail_table(
      userid INT(10) not null, 
      firstname VARCHAR(16)  ,
       PRIMARY KEY (userid),
       FOREIGN KEY (userid) REFERENCES user_table (userid) 
       ON UPDATE CASCADE 
       ON DELETE CASCADE
    
      )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    
    INSERT INTO user_table(userid, loginname,password) VALUES (1,'admin','admin');
    INSERT INTO user_detail_table(userid, firstname) VALUES (1,'Administrator');
    

    又一个问题

    1. 我可以在'用户'中手动插入一行。和' user_detail'表格有“管理员”#39;领域?注释@GeneratedValue检查数据库,然后递增(生成)用户标识的值,对不对?
    2. 异常错误如下

      message Request processing failed; nested exception is java.lang.NullPointerException
      
      description The server encountered an internal error that prevented it from fulfilling this request.
      
      exception
      
      org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
          org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)
          org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
          org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
          org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      root cause
      
      java.lang.NullPointerException
          org.hibernate.tuple.entity.AbstractEntityTuplizer.getPropertyValue(AbstractEntityTuplizer.java:650)
          org.hibernate.persister.entity.AbstractEntityPersister.getPropertyValue(AbstractEntityPersister.java:4736)
          org.hibernate.id.ForeignGenerator.generate(ForeignGenerator.java:96)
          org.hibernate.event.internal.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:117)
          org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:209)
          org.hibernate.event.internal.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:55)
          org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:194)
          org.hibernate.event.internal.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:49)
          org.hibernate.event.internal.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:90)
          org.hibernate.internal.SessionImpl.fireSave(SessionImpl.java:715)
          org.hibernate.internal.SessionImpl.save(SessionImpl.java:707)
          org.hibernate.internal.SessionImpl.save(SessionImpl.java:702)
          com.cms.sweb.dao.UserDetailDaoImpl.saveUserDetail(UserDetailDaoImpl.java:49)
          com.cms.sweb.service.UserDetailServiceImpl.saveUserDetail(UserDetailServiceImpl.java:23)
          sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          java.lang.reflect.Method.invoke(Unknown Source)
          org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:317)
          org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
          org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
          org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
          org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
          org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
          org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
          org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
          com.sun.proxy.$Proxy40.saveUserDetail(Unknown Source)
          com.cms.sweb.controller.WebMainController.addPerson(WebMainController.java:87)
          sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
          sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
          sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
          java.lang.reflect.Method.invoke(Unknown Source)
          org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
          org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
          org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
          org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)
          org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)
          org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
          org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)
          org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
          org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
          org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
          org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
          javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
          org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
      note The full stack trace of the root cause is available in the Apache Tomcat/8.0.20 logs.
      

      如果我错过任何内容,请告诉我,我会添加它。提前谢谢!

1 个答案:

答案 0 :(得分:0)

错误发生在GenericGenerator Annotation中。

 @GenericGenerator(name = "genuserid", strategy="foreign", parameters =  @Parameter(value="USER_TABLE", name = "property") ) 

正确的一个如下

 @GenericGenerator(name = "genuserid", strategy="foreign", parameters =  @Parameter(value="user", name = "property") ) 

'value'应该是外键映射到的对象。