你如何在 Hibernate 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; }
}
...
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; }
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您将 Project 类中的 ProjectRegion 属性声明为 ProjectRegion 类型而不是字符串类型,则当 NHibernate 获取 Project 时,它将自动连接(根据获取策略)ProjectRegion 表。
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.