These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Mapping Audit Fields
PostPosted: Sun Apr 20, 2008 11:48 am 
Newbie

Joined: Sun Mar 23, 2008 5:28 pm
Posts: 6
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


Top
 Profile  
 
 Post subject: Mapping Audit Fields
PostPosted: Sun Apr 20, 2008 12:22 pm 
Senior
Senior

Joined: Thu Jun 21, 2007 8:03 am
Posts: 127
Location: UK
Hi imm102,

Mapping the properties as many-to-one seems fair enough. Bear in mind that that means you can't go deleting the 'user' object (without deleting everything it refers to first).

Quote:
But now if im understanding correctly I have to make this relationship bi-directional.


I'm not sure that you need to do this. I think the mapping will work fine the way you have it.

Regards,
Richard


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

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.