Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4565952
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T18:43:05+00:00 2026-05-21T18:43:05+00:00

I’m having problems mapping ManyToOne from a Parent to Child relationship. The OneToMany collection

  • 0

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.

I have a table of Server instances, which is immutable.

When I create a List of Servers, it populates the Collection of Requests that a Server has assigned to it without problems.

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.

Server.java

public class Server {

    private int serverId;
    private List<Request> requests;
    ...
    @OneToMany(mappedBy="server", fetch=FetchType.LAZY)
    public List<Request> getRequests() {
      return requests;
    }
}

Request.java

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

add.jsp (Add Request form)

...

<form:select id="preferred-server" path="server.serverId">
    <c:forEach items="${servers}" var="server">
        <form:option value="${server.serverId}">${server.serverName}</form:option>
    </c:forEach>
</form:select>

...

AddRequestController.java

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

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.

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?

Any suggestions? Let me know if you require any other code.

Using: Spring 2.5, Hibernate 3.2.5, Sybase DB

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T18:43:05+00:00Added an answer on May 21, 2026 at 6:43 pm

    The problem is that Request is the owning side, so you need to add the server to the request, i.e. call setServer(). The server field is the one responsible for the corresponding column in your database.

    It is not sufficient to add the request to the list since that is just the inverse mapping. If you’d set the server in the request and then reload the server from the db, the request should be automatically added to the list (although you’d normally not do that but manually add the server to the list as well).

    Thus the Request should look like this:

    public class Request {
    
     private int requestId;
     //I don't see any need for this
     //private int server_serverId; // foreign key to Server.serverId
     private Server server;
     ...
     @ManyToOne ( fetch=FetchType.LAZY ) //employ lazy loading, you can put that on @OneToMany too
     @JoinColumn( name="server_serverId", nullable=false }
     public Server getServer() {
      return server;
     }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.