Hibernate 使用 hbm.xml 将多个类映射到一张表

lou*_*xie 5 java mapping hibernate hibernate-mapping jakarta-ee

我对 Hibernate 相当陌生,需要一些有关 hibernate 映射的帮助。

我有 4 个不同的类,我想将它们映射到一个表中,其中主键由 2 个不同类的属性组成。同时,我想仅将每个类中选定的属性映射到本地数据库中。我希望避免 JPA 注释并在 hbm.xml 文件中定义映射样式。我怎么做?

举个例子:

public class Tenant implements Serializable {
    private final static long serialVersionUID = 1L;
    protected List<Rack> rack;
    protected String type;
    //getters setters
}

public class Rack implements Serializable {
    private final static long serialVersionUID = 1L;        
    protected List<Circuit> circuit;
    protected String rackLabel;
    protected Boolean excludes;
    //getters setters
}

public class Circuit implements Serializable {
    private final static long serialVersionUID = 1L;
    protected List<CircuitReadings> circuitReadings;
    protected String circuitNo;
    protected Boolean excludes;
    //getters setters
}

public class CircuitReadings
    implements Serializable {
    private final static long serialVersionUID = 1L;
    protected String date;
    protected String kva;
    protected String current;
    protected String kwh;
    //getters setters
}
Run Code Online (Sandbox Code Playgroud)

最终的表应包含以下内容:

    type | rackLabel | circuitNo | date | kva | current | energy
Run Code Online (Sandbox Code Playgroud)

上面的“电路号”和“日期”应构成复合主键。

有人可以给我举一个例子来说明我应该如何映射它吗?谢谢!

Ara*_*d A 0

没有什么可以阻止你这样做。创建 4 个 HBM 指向同一个表但不同的 Pojo。尽管可以像@Ioan Alexandru Cucu 那样完成,但不建议这样做。

   <!-- HBM1-->

<class name="com.myProject.Rack"
    table="My_Table">

   <!-- HBM2-->

   <class name="com.myProject.Rack"
    table="My_Table">
Run Code Online (Sandbox Code Playgroud)