我有像:
这样的实体class Doc
{
public virtual int Id {get;set;}
public virtual int Code {get;set;}
}
其中Id
是身份。
Code
放在另一个表中。获取Code
我使用下一个SQL语句:
Select Distinct A.CODE from DOCLIST D Left Join DOCSLINKS DL On DL.TODOC_ID=D.DOC_ID
Left Join ARTICLES A On DL.ART_ID=A.ART_ID Where D.DOC_ID=*ourid*
其中ourid
是我们实体的Id
Doc类的地图是:
public class DocMap : ClassMap<Doc>
{
public DocMap()
{
Table("DOCLIST");
Id(x =>x.Id).Column("DOC_ID").GeneratedBy.Custom<NHibernate.Id.TriggerIdentityGenerator>();
//HOW TO MAP CODE?
}
}
我尝试使用Formula
:
Map(x => x.Code).Formula("(Select Distinct A.CODE from DOCLIST D Left Join DOCSLINKS DL On DL.TODOC_ID=D.DOC_ID
Left Join ARTICLES A On DL.ART_ID=A.ART_ID Where D.DOC_ID=Id)").Not.Update();
但我使用消息处理异常ORA-00904 : "DOC0_"."ID": invalid identifier
:
could not load an entity: [Doc#1562][SQL: SELECT doc0_.DOC_ID as DOC1_3_0_, (Select Distinct A.OKP_CODE from DOCLIST D Left join DOCSLINKS DL ON DL.TODOC_ID=D.DOC_ID Left join ARTICLES A ON DL.ART_ID=A.ART_ID Where D.DOC_ID=doc0_.Id) as formula0_0_ FROM DOCLIST doc0_ WHERE doc0_.DOC_ID=?]
有人可以帮我映射Code
吗?
答案 0 :(得分:1)
我想我们应该改变选择。它不应该使用当前表DOCLIST
(用于映射根类Doc
的alreaedy .. Table("DOCLIST")
)
// current select
(SELECT Distinct A.CODE
FROM DOCLIST D
Left Join DOCSLINKS DL On DL.TODOC_ID=D.DOC_ID
Left Join ARTICLES A On DL.ART_ID=A.ART_ID
Where D.DOC_ID=Id)
这应该完成工作(或进行一些调整)
(SELECT Distinct A.CODE
FROM DOCSLINKS DL
Left Join ARTICLES A On DL.ART_ID=A.ART_ID
Where DL.TODOC_ID = DOC_ID) // DOC_ID should be column on current table
我们已经可以访问表DOCLIST
(当前表)。我们只需要从其他表中进行SELECT,同时在wwhere子句中使用我们的ID来过滤DOCSLINKS:
Where DL.TODOC_ID = DOC_ID // DOC_ID will be used from current table