Добро пожаловать в форум, Guest  >>   Войти | Регистрация | Поиск | Правила | В избранное | Подписаться
Все форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM Новый топик    Ответить
 NHibernate-2.1.2.GA && NamedQuery  [new]
Ex_Soft
Member

Откуда:
Сообщений: 7712
+ ClassStaff.cs

using System;

namespace TestIV
{
	public class ClassStaff
	{
		int
			_ID;

		string
			_Name;

		decimal
			_Salary,
			_SalaryAdd;

		public virtual int ID
		{
			get
			{
				return _ID;
			}
			set
			{
				if (_ID != value)
					_ID = value;
			}
		}

		public virtual string Name
		{
			get
			{
				return _Name;
			}
			set
			{
				if (_Name != value)
					_Name = value;
			}
		}

		public virtual decimal Salary
		{
			get
			{
				return _Salary;
			}
			set
			{
				if (_Salary != value)
					_Salary = value;
			}
		}

		public virtual decimal SalaryAdd
		{
			get
			{
				return _SalaryAdd;
			}
			set
			{
				if (_SalaryAdd != value)
					_SalaryAdd = value;
			}
		}
	}
}

+ ClassStaff.xbm.xml

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" auto-import="true" namespace="TestIV" assembly="TestIV">

  <class name="ClassStaff" table="Staff" lazy="true">
    <id name="ID">
      <generator class="native" />
    </id>
    <property name="Name" column ="Name"/>
    <property name="Salary" column ="Salary"/>
    <!-- <property name="SalaryAdd" formula="(select s.Salary from Staff s where (s.ID=ID))"/> -->
    
  </class>

  <sql-query name="NamedQuery1">
    <return alias="NamedQuery1" class="TestIV.ClassStaff">
      <return-property name="ID" column="ID"/>
      <return-property name="Name" column="Name"/>
      <return-property name="Salary" column="Salary"/>
      <return-property name="SalaryAdd" column="SalaryAdd"/>
    </return>
    select
      s.*,
      testdb.dbo.SalaryAdd(s.Salary, :pCoeff) as SalaryAdd
    from
      Staff s
    where
      s.ID=:pID
  </sql-query>
</hibernate-mapping>

+ main.cs

using System;
using System.Collections.Generic;
using System.Reflection;
using NHibernate;
using NHibernate.Cfg;

namespace TestIV
{
	class Program
	{
		static ISessionFactory
			SessionFactory;

		static void Main(string[] args)
		{
			try
			{
				using (ISession session = OpenSession())
				{
					IQuery
						query;

					ClassStaff
						tmpClassStaff = session.Get<ClassStaff>(1);

					query = session.CreateQuery("select s from ClassStaff s where s.ID in (:ID)");
					query.SetParameterList("ID", new object[] { 1, 3, 5 });

					List<ClassStaff>
						ClassStaffRecords = (List<ClassStaff>)query.List<ClassStaff>();

					foreach (ClassStaff s in ClassStaffRecords)
					{
						;
					}

					query = session.GetNamedQuery("NamedQuery1");
					query.SetParameter("pID", 3);
					query.SetParameter("pCoeff", 4);
					ClassStaffRecords = (List<ClassStaff>)query.List<ClassStaff>();

					foreach (ClassStaff s in ClassStaffRecords)
					{
						;
					}
				}
			}
			catch (Exception e)
			{
				Console.WriteLine(e);
			}
		}

		static ISession OpenSession()
		{
			if (SessionFactory == null)
			{
				Configuration
					configuration = new Configuration();

				configuration.AddAssembly(Assembly.GetCallingAssembly());
				SessionFactory = configuration.BuildSessionFactory();
			}

			return SessionFactory.OpenSession();
		}
	}
}

Запрос отправляется правильно, resultset возвращается, но, вот, SalaryAdd==0. Почему?
_________________
"Helo, word!" - 17 errors 56 warnings
14 апр 10, 16:27    [8629794]     Ответить | Цитировать Сообщить модератору
Все форумы / ADO.NET, LINQ, Entity Framework, NHibernate, DAL, ORM Ответить