请帮我解决这个映射问题(我使用的是 Hibernate 3.6)。
我有这些桌子
CREATE TABLE Towers (
Id INT NOT NULL AUTO_INCREMENT ,
Name VARCHAR(5) NOT NULL ,
PRIMARY KEY (Id));
CREATE TABLE LineOfBusiness (
Id INT NOT NULL AUTO_INCREMENT,
Name VARCHAR(15) NOT NULL ,
PRIMARY KEY (Id));
CREATE TABLE LineOfBusinessXTowers (
LineOfBusinessId INT NOT NULL ,
TowerId INT NOT NULL ,
PRIMARY KEY (LineOfBusinessId, TowerId));
CREATE TABLE IF NOT EXISTS Departments (
Code INT NOT NULL ,
LineOfBusinessId INT NOT NULL ,
TowerId INT NOT NULL ,
PRIMARY KEY (Code));
ALTER TABLE LineOfBusinessXTowers
ADD CONSTRAINT FK_LineOfBusinessXTowers_LineOfBusiness
FOREIGN KEY (LineOfBusinessId)
REFERENCES LineOfBusiness (Id)
ON DELETE NO ACTION
ON UPDATE NO ACTION,
ADD CONSTRAINT FK_LineOfBusinessXTowers_Towers
FOREIGN KEY (TowerId)
REFERENCES Towers (Id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
ALTER TABLE Departments
ADD CONSTRAINT FK_Departments_LineOfBusinessXTowers
FOREIGN KEY (LineOfBusinessId, TowerId)
REFERENCES LineOfBusinessXTowers (LineOfBusinessId,TowerId)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
在 Hibernate 映射类中,我希望有一个具有对应 Tower 和 LineOfBusiness 对象的 Department 类:
public class Departments {
private int code;
private String name;
private LineOfBusiness lineOfBusiness;
private Towers tower;
//Constructors and access methods here
}
我试图以这种方式进行映射,但错误:
<hibernate-mapping>
<class name="Departments" table="Departments" catalog="DB">
<id name="code" type="int">
<column name="Code" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="Name" length="45" not-null="true" unique="true" />
</property>
<join table="LineOfBusinessXTowers" optional="false">
<key column="LineOfBusinessId" unique="false" />
<many-to-one name="lineOfBusiness"
class="LineOfBusiness">
<column name="LineOfBusinessId" not-null="true" />
</many-to-one>
</join>
<join table="LineOfBusinessXTowers" optional="false">
<key column="TowerId" unique="false" />
<many-to-one name="towers" class="Towers">
<column name="TowerId" not-null="true" />
</many-to-one>
</join>
</class>
</hibernate-mapping>
哪个是我想做的正确映射?可以做到吗?还有其他更好的策略吗?
非常感谢你。
阿特:埃德加