父/子关系的Hibernate注释映射?

时间:2011-04-28 20:29:48

标签: java hibernate annotations mapping

我在将ManyToOne从Parent转换为Child关系时遇到问题。 OneToMany系列懒洋洋地填充。但是,与单个实体相反,不会。

我有一个Server实例表,它是不可变的。

当我创建服务器列表时,它会填充服务器已分配给它的请求集合而不会出现问题。

当我添加新请求,并从下拉列表中选择服务器首选项,saveOrUpdate,(甚至尝试加载和获取)时,它不会填充Request实例中的Server属性。

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(添加申请表)

...

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

我已经尝试了几乎我可以在网上找到的注释映射的每个组合,这是用于获取请求集合的设置。

现在,当然,我可以从SimpleFormController获取我的Request命令对象,根据用户选择的内容加载Server对象,并以这种方式持久化 - 但这不是关系映射的关键,现在是吗?

有什么建议吗?如果您需要任何其他代码,请告诉我。

使用:Spring 2.5,Hibernate 3.2.5,Sybase DB

1 个答案:

答案 0 :(得分:1)

问题是Request是拥有方,因此您需要将服务器添加到请求中,即调用setServer()server字段是负责数据库中相应列的字段。

将请求添加到列表是不够的,因为这只是逆映射。如果您在请求中设置服务器,然后从数据库重新加载服务器,则请求应自动添加到列表中(尽管您通常不会这样做,但也可以手动将服务器添加到列表中)。 / p>

因此请求应如下所示:

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