Hibernate Books

All times are UTC - 5 hours [ DST ]



Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: Mappind doubt (many to one with composite table as joining)
PostPosted: Thu Jul 12, 2012 2:41 pm 
Newbie

Joined: Fri May 25, 2012 2:28 pm
Posts: 2
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.