views:

37

answers:

1

How do I do a join that would return me results similar to-

SELECT * FROM Project LEFT JOIN ProjectRegion ON ProjectRegion.id = Project.projectRegion

I currently use the syntax-

  Project[] projects = Project.FindAll();

My tables are setup with ActiveRecord/hibernate as follows -

    [ActiveRecord]
    public class ProjectRegion : ActiveRecordBase<ProjectRegion>
    {
        private int id;
        private String title;

        public ProjectRegion()
        {
        }

        [PrimaryKey]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public String Title
        {
            get { return title; }
            set { title = value; }
        }
    }


    [ActiveRecord]
    public class Project : ActiveRecordBase<Project>
    {
        private int id;
        private String projectNumber;
        private String projectRegion;

        public Project()
        {
        }

        [PrimaryKey]
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        [Property]
        public String ProjectInternalCode
        {
            get { return projectNumber; }
            set { projectNumber = value; }
        }

        [Property]
        public String ProjectRegion
        {
            get { return projectRegion; }
            set { projectRegion = value; }
        }
...
+1  A: 

If you declare the ProjectRegion property in the Project class to be of type ProjectRegion instead of string, when NHibernate fetches a Project it will automatically join (according to a fetch policy) the ProjectRegion table.

Darin Dimitrov
I did that and had to change [Property] to [BelongsTo("ProjectRegion")] and now it works.Beautiful, thanks!
Tom