Java – hibernate annotation mapping for parent / child relationships?

I encountered a problem converting manytoone from parent to child relationship The onetomany series fills lazily However, as opposed to a single entity, this is not the case

I have a server instance table that is immutable

When I create a list of servers, it populates the collection of requests that the server has assigned to it without problems

When I add a new request and select server preference, saveOrUpdate (or even try to load and get) from the drop-down list, it will not fill in the server attribute 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 application 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 almost every combination of annotation mappings I can find online, which is the setting used to get the request collection

Now, of course, I can get my request command object from simpleformcontroller, load the server object according to the user's selection, and persist it in this way - but this is not the key to relationship mapping, is it now?

Any suggestions? If you need any other code, please let me know

Usage: Spring 2.5, hibernate 3.2 5,Sybase DB

Solution

The problem is that request is the owner, so you need to add the server to the request, that is, call setserver () The server field is the server field responsible for the corresponding column in the database

Adding a request to the list is not enough because it is only an inverse mapping If you set up the server in the request and then reload the server from the database, the request should be automatically added to the list (although you usually don't do this, you can also add the server to the list manually)

Therefore, the request should be as follows:

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;
 }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>