benny7 wrote:
This is my HQL query:
Code:
Select new Pieces(p.CodePiece, p.LibPiece) from Pieces as p where p.CodeDomaine is null
Pieces is a mapped class that has NHibernate Mapping Attributes in it, with a constructor like:
Code:
public Pieces(string codePiece, string libPiece)
So, when I execute this query, I get no error but all the fields of my table are shown while I only want to see 2 fields.
That's because the select statement you use returns a list of Piece instances. If you're only interested in a some of the fields, and want a class that wraps those fields, and only those two fields, you have to go the import way.
Define a new class:
Code:
public class PieceWrapper
{
public PieceWrapper(string piece, string libPiece)
// Define properties that expose the piece and libPiece, etc.
}
create a new hbm.xml file that contains the import (i.e. "imports.hbm.xml"):
Code:
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0">
<import class="<AssemblyQualifiedName of the PieceWrapper class>" />
</hibernate-mapping>
and change the select statement to
Code:
select new PieceWrapper(p.CodePiece, p.LibPiece) from Pieces p where p.CodeDomaine is null