gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 03:44:25 26 4
gpt4 key购买 nike

我在从父子关系映射 ManyToOne 时遇到问题。 OneToMany 集合懒惰地很好地填充。但是,与单个实体相反。

我有一个服务器实例表,它是不可变的。

当我创建服务器列表时,它会毫无问题地填充服务器分配给它的请求集合。

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

服务器.java

public class Server {

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

请求.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 命令对象,根据用户的选择加载服务器对象,并以这种方式持久化它——但这不是关系映射的重点,不是吗?

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

使用:Spring 2.5、Hibernate 3.2.5、Sybase 数据库

最佳答案

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

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

因此请求应该是这样的:

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

关于java - 父/子关系的 Hibernate 注释映射?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5824570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com