Announcement Announcement Module
Collapse
No announcement yet.
problem in posting data in one to one mapping Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • problem in posting data in one to one mapping

    i am experiencing problem when my form post data to save to database by jpa where i have used one to one mapping to a class. the there is exception occurs that says that

    org.springframework.beans.NullValueInNestedPathExc eption: Invalid property 'user' of bean class [org.mkcl.iforum.adnan.domain.UsersCredentials]: Value of nested property 'user' is null at org.springframework.beans.BeanWrapperImpl.getNeste dBeanWrapper(BeanWrapperImpl.java:453) at org.springframework.beans.BeanWrapperImpl.getBeanW rapperForPropertyPath(BeanWrapperImpl.java:428) at org.springframework.beans.BeanWrapperImpl.setPrope rtyValue(BeanWrapperImpl.java:645) at org.springframework.beans.AbstractPropertyAccessor .setPropertyValues(AbstractPropertyAccessor.java:7 8) at org.springframework.validation.DataBinder.applyPro pertyValues(DataBinder.java:532) at org.springframework.validation.DataBinder.doBind(D ataBinder.java:434) at org.springframework.web.bind.WebDataBinder.doBind( WebDataBinder.java:147) at org.springframework.web.bind.ServletRequestDataBin der.bind(ServletRequestDataBinder.java:108) at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter$ArgumentsResolver.reso lveArguments(AnnotationMethodHandlerAdapter.java:5 98) at org.springframework.web.servlet.mvc.annotation.Ann otationMethodHandlerAdapter.handle(AnnotationMetho dHandlerAdapter.java:242) at org.springframework.web.servlet.DispatcherServlet. doDispatch(DispatcherServlet.java:874) at org.springframework.web.servlet.DispatcherServlet. doService(DispatcherServlet.java:808) at org.springframework.web.servlet.FrameworkServlet.p rocessRequest(FrameworkServlet.java:476) at org.springframework.web.servlet.FrameworkServlet.d oPost(FrameworkServlet.java:441) ..... ..... ....

    the details of my code is given below..

    I have class

    @Entity @Table(name="USERS") public class UsersCredentials {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ID")
    private long id;

    @Column(length =30)
    private String username;

    @Column(length =30)
    private String password;

    @Column(length =30)
    private boolean enabled;


    @OneToOne(cascade = javax.persistence.CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinColumn(name = "UserID")
    @Cascade(value = CascadeType.ALL)
    private User user;
    /// followed by getter setter

    }

    and another class that is one property of above class making one to one mapping

    @Entity @Table(name="USER") public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "USER_ID")
    private long userID;

    @Column(length =30)
    private String firstName;

    @Column(length =30)
    private String lastName;

    @Column(length =160)
    private String aboutMe;

    @Column(length = 100000)
    private byte[] photoInByte;
    //getter and setters }

    as you see the relation ship between two table is one to one. now the controller that handles get and post request

    @Controller @RequestMapping("/EmployeeRepository.htm*") public class EmployeeRepositoryController {

    @Autowired
    IUser user;

    @Autowired
    IUserCredentials userCredentials;

    @RequestMapping(method = RequestMethod.GET)
    public String showUserDetails(ModelMap model) {
    System.out.println("in get method");
    model.addAttribute("userMap",userCredentials.findB yUsername(SecurityContextHolder.getContext().getAu thentication().getName()));
    return "EmployeeRepository";
    }

    @InitBinder
    public void setBinder(WebDataBinder binder, WebRequest requ8est){
    UserCustomEditor custom = new UserCustomEditor();
    custom.setUser(this.user);
    binder.registerCustomEditor(User.class,"user", custom);

    }


    @RequestMapping(method = RequestMethod.POST)
    public String UpdateEmployee(@ModelAttribute("userMap") UsersCredentials cred){
    System.out.println("before to be saved");
    User u = user.findByUserid(2);
    cred.setUser(u);
    userCredentials.store(cred);
    return "redirect:EmployeeRepository.htm";
    }
    }

    and the form that display that content in response to get and post request the EmployeeRepository.jsp

    User Id ${userMap.user.userID} First Name Enter/Update first name of the employee Last Name Enter/Update last name of the employee Username Enter/Update user of the employee Password Enter/Update password of the employee Location Enter/Update location of the employee Designation Enter Designation of the employee About Employee
    Not more than 160 chars.

    and one more class ie the custom binding editor class is

    public class UserCustomEditor extends PropertyEditorSupport {

    IUser user;
    private String format;


    public IUser getUser() {
    return user;
    }

    public void setUser(IUser user) {
    this.user = user;
    }

    public void setFormat(String format) {
    this.format = format;
    }

    public void setAsText(String text){
    User user1 = user.findByUserid(Long.parseLong(text));
    setValue(user1);
    }
    }

    now when i get data by user object of UserCredential there no problem in fetching data but once i post data after editing as u see the post handler of controller the exception thrown... help me as i am not able to solve it from 2 days...
Working...
X