Hi,
I can't find an example of how to best achieve this. I have a User class which represents a user of a website. When that user performs activities he/she will create other records, articles, comments etc. All these have the audit properties CreatedBy, CreatedTime, LastChangedBy, LastChangedTime. See Below:
Code:
CREATE TABLE `Comments`(
`CommentId` INT UNSIGNED NOT NULL AUTO_INCREMENT UNIQUE,
`ParentId` INT UNSIGNED,
`Type` INT UNSIGNED NOT NULL,
`Status` INT UNSIGNED NOT NULL,
`Rank` INT UNSIGNED,
`Content` TEXT,
`CreatedBy` INT UNSIGNED NOT NULL,
`CreatedTime` DATETIME,
`LastChangedBy` INT UNSIGNED NOT NULL,
`LastChangedTime` TIMESTAMP,
CONSTRAINT `PK_EventComment` PRIMARY KEY CLUSTERED (`CommentId`),
CONSTRAINT FOREIGN KEY (`ParentId`) REFERENCES `scifflec`.`Comments` (`CommentId`) ON DELETE NO ACTION,
CONSTRAINT FOREIGN KEY (`LastChangedBy`) REFERENCES `scifflec`.`User` (`UserId`) ON DELETE NO ACTION,
CONSTRAINT FOREIGN KEY (`CreatedBy`) REFERENCES `scifflec`.`User` (`UserId`) ON DELETE NO ACTION
);
How should these be mapped in the Comment.hbm.xml? This is technically a many-to-one relationship. So I have initially done the following in each class:
Code:
<property name="CreatedTime" column="CreatedTime" type="DateTime"/>
<many-to-one name="CreatedBy" column="CreatedBy" class="Core.Domain.User, Core"/>
<property name="LastChangedTime" column="LastChangedTime" type="Timestamp"/>
<many-to-one name="LastChangedBy" column="LastChangedBy" class="Core.Domain.User, Core"/>
But now if im understanding correctly I have to make this relationship bi-directional. If that is the case then I will have to add collections to the User class to map all commentcreated, commentschanged etc etc etc. I don't want to do this as it doesn't make sense in this context and would greatly increase querie sizes relating to Users. Could these be mapped as one-to-one relationships?
Examples of what other people have done would be most helpful.
Name and version of the database you are using:
MySQL 5.0