Results 1 to 2 of 2

Thread: Problem binding to domain layer POJOs

  1. #1
    Join Date
    Sep 2004
    Location
    Leuven, Belgium
    Posts
    1,853

    Default Problem binding to domain layer POJOs

    The Spring data binding system allows you to bind servlet request data to any command object, e.g. a POJO from your domain layer. However, I'm having a problem with this.

    I have the following simple POJO User entity:
    Code:
    public class User extends Entity {
      private String userName;
      private Organization organization;
    
      //default constructor for use by Spring binding, Hibernate, ...
      public User() {
      }
    
      public User(String userName) {
        this.userName=userName;
      }
    
      //getters and setters omitted
    
      public void setLinkedOrganization(Organization org) {
        org.addUser(this);
      }
    
      public boolean equals(Object obj) {
        if (obj==this) return true;
        if (obj==null) return false;
        if (obj.getClass()!=this.getClass()) return false;
        User other=(User)obj;
        return this.getUserName().equals(other.getUserName());
      }
    
      public int hashCode() {
        return getUserName().hashCode();
      }
    }
    As you can see it uses the userName to determine equality. It also has a bidirectional associated with the Organization entity, so Organization has a method:
    Code:
    public class Organization extends Entity {
      private Set users=new HashSet();
    
      ...
    
      public void addUser(User user) {
        if (user.getOrganization()!=null) {
          user.getOrganization().removeUser(user);
        }
        user.setOrganization(this);
        users.add(user);
      }
    }
    The problem I now have is that when the ServletRequestDataBinder pushes in the Organization object when populating a new User in the CreateUserController, the userName has not yet been set! As a result the equals() method will throw a NullPointerException when the user is added to the "users" set of the organization (using users.add(user) in the code above).

    Is there something stupid I'm doing here? How can I fix this while still using the ServletRequestDataBinder to bind to my domain layer POJO? I don't want to create a dummy DTO just to bind to and manually doing all the binding also seems clumsy!

    Erwin

  2. #2
    Join Date
    Feb 2005
    Posts
    217

    Default

    Are you cascading your changes between User and Organization? If so that should take care of the relationship so you don't have to do that step of removing a user from an organization.

    I'm not sure what
    that when the ServletRequestDataBinder pushes in the Organization object when populating a new User in the CreateUserController
    means. Are you creating a new user? If so that's the time where you want to set his name. After you've created the user and an organization to it and then save.

Similar Threads

  1. Replies: 2
    Last Post: Oct 10th, 2005, 05:12 PM
  2. One ApplicationContext per Layer problem...
    By Alwin in forum Container
    Replies: 2
    Last Post: Jul 7th, 2005, 10:49 PM
  3. EJB service layer non-OO / Anemic Domain Model
    By Aro in forum Architecture
    Replies: 0
    Last Post: Jan 15th, 2005, 07:10 AM
  4. Replies: 0
    Last Post: Jan 6th, 2005, 08:19 AM
  5. Replies: 2
    Last Post: Sep 29th, 2004, 01:38 PM

Posting Permissions

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