有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java处理hibernate一对多双向关系

我的遗留应用程序中有两个表

HostTables。表HostTables具有外键关系(Tables_fk) column。在我的代码中,我有一个如下的方法

public class Tables {
    public void addHosts(Host host) {
         getHostsCollection().add(host);
    }
}

现在我正在拆分我的数据库,Tables将进入一个单独的数据库。我现在从Host中删除Tables_fk列,并在那里添加一个Table_ID列,我将从我的应用程序中编写该列。现在,因为我正在从应用程序将Table_ID写入主机表,所以我猜addHosts(Host host)方法现在不需要存在

我说得对吗?另外,由于Tables表与Host没有任何外键关系,Hibernate在该方法中添加了什么


共 (1) 个答案

  1. # 1 楼答案

    首先,如果在数据库中删除一个表,hibernate将不再适用于多通关系(表实体),因此:

    you need to remove your ManyToOne annotation within your Table class and then change it to @Transient or remove the hosts variable entirely together with getters and setters

    如果您这样做,您的关系不再是双向的,这种关系将变成单向的,并且工作正常,您仍然可以获取所有的“表”列行

    Take note adding column is different with adding a Foreign Key and the section you have mentioned about dropping Tables_fk creates ambiguity