Hibernate将单个外键列映射到多个表

时间:2014-02-18 11:04:05

标签: java hibernate

我有三张类似的表:

vehicles
- id1
- id2
- vehicle_type_id
- vehicle_detail_id
- primary key(id1, id2)
buses
- id
...
cars
- id
...

vehicleTypeId决定车辆的类型以及从哪个表中获取vehicleDetails。加入车辆的vehicleDetailId和特定车辆类型的id。 我用这种方式创建了类:

class Vehicle{
    VehiclePk pk;
    Long vehicleTypeId;
    Long vehicleDetailId;
}

class Bus extends Vehicle{
    Long id;
}

class Car extends Vehicle{
    Long id;
}

class VehiclePk {
    private Long id1;
    private Long id2;

    @Override
    public boolean equals(Object obj) {
        if(obj != null && obj instanceof VehiclePk) {
            VehiclePk p = (VehiclePk)obj;
            return id1.equals(p.id1) && id2.equals(p.id2);
        }
        return false;
    }

    @Override
    public int hashCode() {
        return (id1 + id2).hashCode();
    }
}

我尝试使用discriminator:

在hibernate映射文件中组合和元素
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping schema="test_db">
    <class name="Vehicle" table="vehicles" discriminator-value="0">
    <composite-id name="pk" class="VehiclePK">
        <key-property name="id1" column="id1" type="long"/>
        <key-property name="id2" column="id2" type="long"/>
    </composite-id>
    <discriminator column="vehicle_type_id" type="long"/>
<property name="vehicleDetailId" column="vehicle_detail_id" type="long"/>

    <subclass name="Bus" discriminator-value="1">
        <join table="vehicles" >
            <key column="vehicle_detail_id" />
            ... 
        </join>         
    </subclass>
    <subclass name="Car" discriminator-value="2">
        <join table="vehicles" >
            <key column="vehicle_detail_id" />
            ... 
        </join>         
    </subclass>
</class>

映射文件有问题,因为数据库未初始化。如果我错过了什么,请告诉我。感谢。

UPDATE : It gives ArrayIndexOutOfBoundsException: 1
Stacktrace :
...
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1206)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1026)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4421)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4734)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:799)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:779)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:601)
at org.apache.catalina.startup.HostConfig.deployWAR(HostConfig.java:943)
at org.apache.catalina.startup.HostConfig.deployWARs(HostConfig.java:778)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:504)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1385)
at org.apache.catalina.startup.HostConfig.lifecycleEvent(HostConfig.java:306)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:142)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1389)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1653)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1662)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1642)
at java.lang.Thread.run(Thread.java:662)

1 个答案:

答案 0 :(得分:0)

当列数不正确时,这是典型的错误消息。在这种情况下,您的联接中只有一个主键列。联接是主键相关表,因此它们需要具有相同的主键。在你的情况下,车辆有两个ids,而公共汽车和汽车只有一个。

join元素指定一个 new 表,其中键列为主键和外键。

考虑使它成为多对一,每个子类具有不同的类型。 (而不是加入)。

它也会映射到一对一的属性。