1

我有以下表结构。

Region table {
`RegionId` int(11) NOT NULL AUTO_INCREMENT,
`RegionName` varchar(45) DEFAULT NULL,
PRIMARY KEY (`RegionId`) }

Client table {
`RegionId` int(11) NOT NULL,
`ClientName` varchar(45) NOT NULL,
PRIMARY KEY (`RegionId`, `ClientName`) ,
KEY `Client_RegionId_FK` (`RegionId`),
CONSTRAINT `Client_RegionId_FK` FOREIGN KEY (`RegionId`) REFERENCES `Region` (`RegionId`) ON DELETE NO ACTION ON UPDATE NO ACTION
}

Product table {
`RegionId` int(11) NOT NULL ,
`ProductId` varchar(45) NOT NULL,
`ProductName` varchar(45) DEFAULT NULL, 
PRIMARY KEY (`RegionId`, `ProductId`),
KEY `Product_RegionId_FK` (`RegionId`),
CONSTRAINT `Product_RegionId_FK` FOREIGN KEY (`RegionId`) REFERENCES `Region` (`RegionId`) ON DELETE NO ACTION ON UPDATE NO ACTION
}

Order table {
`OrderId` int(11) NOT NULL AUTO_INCREMENT,
`RegionId` int(11) DEFAULT NULL,
  `ClientName` varchar(45) DEFAULT NULL,
`ProductId` int(11) DEFAULT NULL,
`OrderDate` datetime DEFAULT NULL,
 `OrderValue` int(11) DEFAULT NULL,
PRIMARY KEY (`OrderId`) ,
 KEY `Order_RegionId_FK` (`RegionId`),
KEY `Order_ClientName_FK` (`RegionId`,`ClientName`),
KEY `Order_ProductId_FK` (`RegionId`,`ProductId`),
CONSTRAINT `Order_RegionId_FK` FOREIGN KEY (`RegionId`) REFERENCES `Region` (`RegionId`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `Order_ClientName_FK` FOREIGN KEY (`RegionId`, `ClientName`) REFERENCES `Client` (`RegionId`, `ClientName`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `Order_ProductId_FK` FOREIGN KEY (`RegionId`, `ProductId`) REFERENCES `Product` (`RegionId`, `ProductId`) ON DELETE NO ACTION ON UPDATE NO ACTION
}

订单表有 3 个引用 RegionId 的不同外键约束。一种是直接的,另一种是通过客户和产品的。

下面是 Order 表的休眠映射文件

<hibernate-mapping>
<class name="com.test.model.Order" table="Order">
    <id name="orderId" type="java.lang.Integer">
        <column name="OrderId" />
        <generator class="identity" />
    </id>
    <many-to-one name="Region" class="com.test.model.Region" fetch="select">
        <column name="RegionId" />
    </many-to-one>
    <many-to-one name="product" class="com.test.model.Product" update="false" insert="false" fetch="select">
        <column name="RegionId" />
        <column name="ProductId" />
    </many-to-one>
    <many-to-one name="client" class="com.test.model.Client" update="false" insert="false" fetch="select">
        <column name="RegionId" />
        <column name="ClientName" length="45" />
    </many-to-one>
    <property name="orderDate" type="timestamp">
        <column name="OrderDate" length="19" />
    </property>
    <property name="orderValue" type="java.lang.Integer">
        <column name="OrderValue"  />
    </property>
</class>
</hibernate-mapping>

我遇到的问题是,当我将新订单插入 DB NULL 时,插入了 clientName 和 ProductId。我已经验证我在持久化之前将 Client 和 Product 对象提供给 Order Model 对象。但由于某种原因,NULL 被插入。我对正确添加的区域信息没有任何问题。

我尝试调试和检查 Hibernate 的 SQL,我看到 hibernate 正在尝试在插入之前读取 Client 和 Product 表,但令人惊讶的是 clientName 和 productId 不是插入顺序语句的一部分。

下面是 Hibernate sql 的输出

Hibernate: 
/* get current state com.test.model.Product */ select
    product_.RegionId,
    product_.ProductId,
    product_.ProductName as ProductName12_
from
    testdb.product product_ 
where
    product_.RegionId=? 
    and product_.ProductId=?
Hibernate: 
/* get current state com.test.model.Client */ select
    client_.RegionId,
    client_.ClientName 
from
    testdb.client client_ 
where
    client_.RegionId=? 
    and client_.ClientName=?
Hibernate: 
/* insert com.test.model.Order
    */ insert 
    into
        testdb.order
        (RegionId, OrderDate, OrderValue) 
    values
        (?, ?, ?)

请让我知道可能出现的问题以及解决问题的建议。

4

0 回答 0