I have solve the problem by changing the hbm.xml mapping file as follows;
Code:
<class name="Marchant" table="Marchants">
<id name="stringId" column="MarchantId" length="50"/>
<property name="marchantName" column="MarchantName" length="200" not-null="true" />
<property name="email" column="Email" length="100" />
<property name="msisdn" column="Msisdn" length="20" />
<property name="lastModifiedDateTime" column="LastModifiedDateTime" type="timestamp"/>
<property name="status" column="Status" length="10" />
<property name="shortCode" column="ShortCode" length="20" />
</class>
<class name="Customer" table="Customers">
<composite-id >
<key-property name="stringId" column="CustomerId" length="50" />
<key-many-to-one name="marchant" column="MarchantIdCustomer" />
</composite-id>
<property name="msisdn" column="MSISDN" length="20" unique="true"/>
<property name="shortName" column="ShortName" length="200"/>
<property name="fullName" column="FullName" length="200"/>
<property name="email" column="Email" length="100" unique="true"/>
<set name="groups" table="CustomerGroups">
<key>
<column name="CustomerId"/>
<column name="MarchantIdCustomer" />
</key>
<many-to-many class="Group" >
<column name="GroupId"/>
<column name="MarchantIdGroup"/>
</many-to-many>
</set>
</class>
<class name="Group" table="Groups">
<composite-id >
<key-property name="stringId" column="GroupId" length="50" />
<key-many-to-one name="marchant" column="MarchantIdGroup" />
</composite-id>
<property name="groupName" column="GroupName" length="100"/>
<property name="description" column="Description"/>
<property name="lastModifiedDateTime" column="LastModifiedDateTime" type="timestamp"/>
<set name="customers" table="CustomerGroups" inverse="true">
<key>
<column name="GroupId"/>
<column name="MarchantIdGroup" />
</key>
<many-to-many class="Customer" >
<column name="CustomerId"/>
<column name="MarchantIdCustomer"/>
</many-to-many>
</set>
</class>
I can see the created database tables as follows;
Code:
CREATE TABLE Marchants (
MarchantId varchar(50) NOT NULL default '',
MarchantName varchar(200) NOT NULL default '',
Email varchar(100) default NULL,
Msisdn varchar(20) default NULL,
LastModifiedDateTime datetime default NULL,
Status varchar(10) default NULL,
ShortCode varchar(20) default NULL,
PRIMARY KEY (MarchantId)
) TYPE=InnoDB;
CREATE TABLE Groups (
GroupId varchar(50) NOT NULL default '',
MarchantIdGroup varchar(50) NOT NULL default '',
GroupName varchar(100) default NULL,
Description varchar(255) default NULL,
LastModifiedDateTime datetime default NULL,
PRIMARY KEY (GroupId,MarchantIdGroup),
KEY FK7FA2C5F4688C34A1 (MarchantIdGroup),
FOREIGN KEY (`MarchantIdGroup`) REFERENCES `Marchants` (`MarchantId`)
) TYPE=InnoDB;
CREATE TABLE Customers (
CustomerId varchar(50) NOT NULL default '',
MarchantIdCustomer varchar(50) NOT NULL default '',
MSISDN varchar(20) default NULL,
ShortName varchar(200) default NULL,
FullName varchar(200) default NULL,
Email varchar(100) default NULL,
PRIMARY KEY (CustomerId,MarchantIdCustomer),
UNIQUE KEY MSISDN (MSISDN),
UNIQUE KEY Email (Email),
KEY FKD7809C35C7E54D5E (MarchantIdCustomer),
FOREIGN KEY (`MarchantIdCustomer`) REFERENCES `Marchants` (`MarchantId`)
) TYPE=InnoDB;
CREATE TABLE CustomerGroups (
CustomerId varchar(50) NOT NULL default '',
MarchantIdCustomer varchar(50) NOT NULL default '',
GroupId varchar(50) NOT NULL default '',
MarchantIdGroup varchar(50) NOT NULL default '',
PRIMARY KEY (CustomerId,MarchantIdCustomer,GroupId,MarchantIdGroup),
KEY FK225733727F7EFDF1 (CustomerId,MarchantIdCustomer),
KEY FK22573372B72C759C (GroupId,MarchantIdGroup),
FOREIGN KEY (`GroupId`, `MarchantIdGroup`) REFERENCES `Groups` (`GroupId`, `MarchantIdGroup`),
FOREIGN KEY (`CustomerId`, `MarchantIdCustomer`) REFERENCES `Customers` (`CustomerId`, `MarchantIdCustomer`)
) TYPE=InnoDB;
But the problem is CustomerGroups table have 2 colums (MarchantIdCustomer and MarchantIdGroup) refers to same data, because both MarchantIdCustomer and MarchantIdGroup refers to Merchant class. This results duplicate data.
But I want to have a CustomerGroups table with 3 columns (i.e. CustomerId, MarchantId, GroupId) . MarchantId represents both MarchantIdCustomer and MarchantIdGroup columns in the previous table. (Since both reperesents same thing, I want to make it one)
Is this possible with Hibernate. If so.. how can we change the hbm.xml mapping file?
I really tired with searching this. Sorry for the long comment...
Thank you.
thilinaa