你如何在hibernate activerecord中加入?

时间:2009-09-03 13:57:39

标签: c# nhibernate castle-activerecord

如何进行联接,返回类似于

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

我目前使用语法 -

  Project[] projects = Project.FindAll();

我的表是使用ActiveRecord / hibernate设置的,如下所示 -

    [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 个答案:

答案 0 :(得分:1)

如果您将Project类中的ProjectRegion属性声明为ProjectRegion类型而不是string,那么当NHibernate获取Project时,它将自动加入(根据获取策略)ProjectRegion表。