title: Hibernate Annotation Mapping for Parent/Child Relationship?
body: <p>I'm having problems mapping ManyToOne from a Parent to Child relationship.  The OneToMany collection populates lazily fine.  However, the inverse to a single entity, does not.</p>
  
  <p>I have a table of Server instances, which is immutable.</p>
  
  <p>When I create a List of Servers, it populates the Collection of Requests that a Server has assigned to it without problems.</p>
  
  <p>When I add a new Request, and select the Server preferred from a dropdown, saveOrUpdate, (or even tried load, and get) it does not populate the Server property in the Request instance.</p>
  
  <p>Server.java</p>
  
  <pre><code>public class Server {
  
      private int serverId;
      private List&lt;Request&gt; requests;
      ...
      @OneToMany(mappedBy="server", fetch=FetchType.LAZY)
      public List&lt;Request&gt; getRequests() {
        return requests;
      }
  }
  </code></pre>
  
  <p>Request.java</p>
  
  <pre><code>public class Request {
  
      private int requestId;
      private int server_serverId; // foreign key to Server.serverId
      private Server server;
      ...
      @ManyToOne(fetch=FetchType.LAZY)
      @JoinColumn(name="server_serverId", nullable=false, updatable=false, insertable=false}
      @ForeignKey(name="server_serverId", inverseName="serverId") // Don't think this really does anything
      public Server getServer() {
        return server;
      }
  }
  </code></pre>
  
  <p>add.jsp (Add Request form)</p>
  
  <pre><code>...
  
  &lt;form:select id="preferred-server" path="server.serverId"&gt;
      &lt;c:forEach items="${servers}" var="server"&gt;
          &lt;form:option value="${server.serverId}"&gt;${server.serverName}&lt;/form:option&gt;
      &lt;/c:forEach&gt;
  &lt;/form:select&gt;
  
  ...
  </code></pre>
  
  <p>AddRequestController.java</p>
  
  <pre><code>public class AddRequestController extends SimpleFormController {
    ...
    @Override
    protected ModelAndView onSubmit(Object command) throws Exception {
  
      Request request = (Request)command;
  
      try {
          logger.info("BEFORE SAVE, request.server = " + request.getServer());
      } catch (NullPointerException npe) {
          logger.error("onSubmit() caught NPE: " + npe);
      }
  
      // Would rather not do these next two lines.  Would prefer to saveOrUpdate
      // Request, and have it populate server property via server_serverId foreign key.
      //Server server = serverDao.loadByServerId(request.getServer_serverId());
      //request.setServer(server);
      //Added this instead, which calls getHibernateTemplate().refresh(server, LockMode.READ)
      serverDao.refreshServer(request.getServer());
  
      requestDao.addRequest(request); // calls getHibernateTemplate.saveOrUpdate(request)
  
      try {
          logger.info("AFTER SAVE, request.server = " + request.getServer());
      } catch (NullPointerException npe) {
          logger.error("onSubmit() caught NPE: " + npe);
      }        
  
      return new ModelAndView(getSuccessView(), "request", request);
    }
  }
  </code></pre>
  
  <p>I've tried virtually every combination of Annotation mapping I could find online, and this is the setup that works with fetching a Collection of Requests.</p>
  
  <p>Now, of course, I can get my Request command object from the SimpleFormController, load the Server object based on what the user selects, and persist it that way--but that's not the point of relationship mapping, now is it?</p>
  
  <p>Any suggestions? Let me know if you require any other code.</p>
  
  <p>Using: Spring 2.5, Hibernate 3.2.5, Sybase DB</p>