gpt4 book ai didi

Java - Hibernate 一个类可以引用多个类之一

转载 作者:行者123 更新时间:2023-11-30 02:29:10 24 4
gpt4 key购买 nike

我有一个类可以引用多个类之一。

旧类的结构如下:

public class CarOne {
private int id;
private int type; //one
...
}

public class CarTwo {
private int id;
private int type; //two
...
}

public class CarThree {
private int id;
private int type; //three
...
}

它们不是 SupperMapped,它们都存在于自己的表中,并具有自己的 ID。我对此无能为力,因为它已经投入生产很长一段时间了。

现在我需要添加一个新类:

public class Numberplate {
//here I'd like a one to one mapping to one of the classes?
}

我自己能想到的一个选择是我可以在汽车对象上添加 OneToOne 关系,但我希望有一个选项可以使用“id”和“type”作为鉴别器来放置关系车牌侧。有这种可能吗?

(免责声明:这些例子纯属虚构,但情况并非如此。)

最佳答案

您可以使用联合继承类型,创建一个父类(super class) Car,然后 CarOne、CarTwo、CarThree 必须扩展它。您可以找到加入 here 的完整示例

关于Java - Hibernate 一个类可以引用多个类之一,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44715998/

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