Ho un'applicazione che utilizza NHibernate come ORM. Ho una classe persistente:NHibernate - Restituire oggetto complesso dalla funzione sql
public class Match : IEntity
{
public virtual int ID { get; set; }
public virtual string Word { get; set; }
public virtual int WordIntervalBeginning { get; set; }
public virtual int WordIntervalEnding { get; set; }
}
e ho una funzione SQL sul lato server:
CREATE FUNCTION ftMatchTest
()
RETURNS TABLE
AS
RETURN
(
SELECT mt1.*, mt2.*,
CASE WHEN mt1.Word = mt2.Word THEN 1 ELSE 0 END AS sc
FROM
dbo.tMatchesTest mt1, dbo.tMatchesTest mt2
)
Voglio essere in grado di chiamare questa funzione e mappare il risultato da esso nella seguente classe
public class FResult
{
public Match Match1 { get; set; }
public Match Match2 { get; set; }
public int sc { get; set; }
}
È possibile farlo con NHibernate 3.0? È possibile farlo con FluentNHibernate?
Grazie in anticipo!
AGGIORNATO
ho map classe Partita nella tabella tMatchesTest.
Struttura della tabella tMatchesTest è:
CREATE TABLE [dbo].[tMatchesTest](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Word] [varchar](50) NOT NULL,
[WordIntervalBeginning] [int] NOT NULL,
[WordIntervalEnding] [int] NOT NULL,
CONSTRAINT [PK_tMatchesTest] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
UPDATED2
La soluzione che ho trovato per conto mio:
1. Creare query denominata come questo
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace=" ConsoleApplication8.Domain.Entities"
assembly="ConsoleApplication8">
<resultset name="fresult-resset">
<return alias="Match1" class="Match"/>
<return alias="Match2" class="Match"/>
<return-scalar column="sc" type="int"/>
</resultset>
<sql-query name="getfresult" resultset-ref="fresult-resset">
SELECT {Match1.*}, {Match2.*},
CASE WHEN Match1.Word = Match2.Word THEN 1 ELSE 0 END sc
FROM dbo.tMatchesTest Match1, dbo.tMatchesTest Match2
</sql-query>
</hibernate-mapping>
ed eseguire la query come questa :
Session.GetNamedQuery("getfresult")
.SetResultTransformer(new AliasToBeanResultTransformer(typeof(FResult)))
.List<FResult>();
Questo è il modo più semplice e semplice che ho trovato finora per eseguire l'operazione.
Potresti postare la struttura della tabella tMatchesTest? –
Ho aggiunto la struttura di questa tabella nel post originale. – StuffHappens
è il codice di esempio della funzione SQL? Il suo contenuto sarà più complesso? Perché fare affidamento sulla funzione per il quering effettivo? – Jaguar