Hi all, please help me with this mapping issue (I'm using Hibernate 3.6).
I have these tables
Code:
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;
In the Hibernate mapping classes I'd like to have a Department class that have the correspondig Tower and LineOfBusiness objects:
Code:
public class Departments {
private int code;
private String name;
private LineOfBusiness lineOfBusiness;
private Towers tower;
//Constructors and access methods here
}
I was trying to do the mapping this way, but is wrong:
Code:
<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>
Which would be the correct mapping to do what I want to?
Can It be done?
Is there another better strategy?
Thank to you so much.
Atte:
Edgar