-->
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.  [ 4 posts ] 
Author Message
 Post subject: Inheritance strategy for Baseclass w. Id functionality only
PostPosted: Wed Dec 12, 2007 9:37 am 
Newbie

Joined: Wed Dec 12, 2007 9:20 am
Posts: 3
Hibernate version: 1.2.1 GA

Hello,

I´m thinking about an architecture to use and came up with some questions. I use an baseclass that provides only identifier functionality. Now i´m searching for the best inheritance strategy in nhibernate.

My baseclass:
Code:
    public class BaseUniqueIdentifier {

        private Guid guid;

        public virtual Guid Guid
        {
            get
            {
                if (this.guid == Guid.Empty)
                {
                    this.guid = Guid.NewGuid();
                }
                return this.guid;
            }
            set { this.guid = value; }
        }
    }


Eg for a subclass:
Code:
    public class Course
    {
        private Guid guid;
        private string name;

        public Course()
        {
        }

        public virtual Guid Guid
        {
            get
            {
                if (this.guid == Guid.Empty)
                {
                    this.guid = Guid.NewGuid();
                }
                return this.guid;
            }
            set { this.guid = value; }
        }

        public virtual string Name
        {
            get { return this.name; }
            set { this.name = value; }
        }
    }


Has anyone an idea? Thank you!

Phil


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 10:11 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
Is your goal to be able to say session.Load<BaseUniqueIdentifier>(Id)?

If that's the case you could always do a table per subclass strategy with foreign keys from all of your tables to this BaseUniqueIdentifier table. That table would basically serve as just a lookup for the Id. You would then rely on the query to do the rest.

I really advice against this idea though. What you'll end up with is a join against every table in your database whenever you query for this object. If you're Ok with that potential performance hit then it will work.

The only other options you would have anyway is to query every table to achieve this functionality. No matter what you do these will introduce a performance hit. Is it really not possible to store both the type and id for what you need?


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 10:22 am 
Newbie

Joined: Wed Dec 12, 2007 9:20 am
Posts: 3
Hey, tanks for your fast answer.

I'd like to have tables for every object and not an additional table with all Guids. With an additional table an foreign keys to this table my concept would be needless.

I'd prefer tables like these:

Code:
Course
=====
Guid
Name


or

Code:
Student
=====
Guid
Name
CourseList


An option to solve my problem is to kill the Baseclass and to implement the Identifier-stuff in every class. I'd like to avoid this redundancy.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 12, 2007 11:22 am 
Senior
Senior

Joined: Thu Feb 09, 2006 1:30 pm
Posts: 172
What you are looking for will work. Just don't map the base class. The only needed duplication is in the each implementation of the object itself.

The only issue will be if you try to search for all instances of that base class. You would need to run a separate query for every single table in your database, which could be a problem. Then again if it is a small application it may not be a problem if you're only ever searching by primary key (assuming you aren't trying to do some kind of a join).


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.