views:

225

answers:

1

Здравствуйте, ANRY! Совсем недавно при прохождении практики от университета, столкнулся с NHibernate. Тут же прочитал вашу статью "Hello NHibernate!". Понадобилось реализовать некое подобие магазина: то есть, есть товар, клиент, заказ. Соответственно создал 4 таблицы в MSSQL 2010: Товар(id_товара, название, цена), Клиент(id_клиента, имя, фамилия), Заказ(id_заказа, id_клиента, стоимость) и Строка заказа(id_строки заказа, id_заказа, id_товара, количество). Соответственно, создал 4 класса: Товар, Клиент, Заказ, Строка заказа. вопрос мой такой: нужно ли создавать 4 mapping-файла, или же можно ограничиться одним? А когда идет Debug выдается следующая ошибка: "Could not compile the mapping document: Sklad.products.hbm.xml". Причем "билдится" нормально, без ошибок. В чем может быть проблема и как ее можно решить? С уважением, Андрей.


Google translation

(I cleaned this up a but, don't don't speak russian, someone else please improve if it's wrong)

Hello, ANRY! Most recently, at the university, faced with NHibernate. I read your article "Hello NHibernate!". I tried to implement something like a store: that is, a product, the customer order. Accordingly I created 4 tables in MSSQL 2010:

  • Goods (id_tovara, name, price)
  • Client (id_klienta, name, surname)
  • Order (id_zakaza, id_klienta, cost)
  • Order Line (id_stroki order id_zakaza, id_tovara, quantity)

Accordingly, I created 4 classes: Product, Customer, Order, Order Line. y question is this: whether you want to create 4 class mapping-file, or you can make only one?

And when there is a Debug gives the following error:

Could not compile the mapping document: `Sklad.products.hbm.xml`

Build is normal, no errors. What may be the problem and how can I solve it?

Regards, Andrew.

A: 

you can put all your class mappings into a single file. consider allmaps.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
 <hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
  <class  name="MyAsm.Foo,MyAsm" table="Foo">
   <id name="Id>
    <generator class="native" />
   </id>
   <property name="AName" type="string" />
  </class>

  <class  name="MyAsm.Bar,MyAsm" table="Bar">
   <id name="Id>
    <generator class="native" />
   </id>
   <property name="BName" type="string" />
  </class>
</hibernate-mapping>

however i would advise against it, especially if your project contains more than 5 classes, for clarity.

also, remember to set open the file properties from visual studio and set "Build Action" -> Embedded Resource

Now, that error means that you have made an error in the mapping it self (for example it can be a typo like type="streng" instead of type="string"

With the NHibernate library you will find a file "nhibernate-mapping.xsd". If you copy this file into $VisualStudioInstallDir\Xml\Schemas (and reload visual studio) you will have intellisense support on the mapping files (or any XML with the same namespace).

This is very helpfull, especially when learning nhibernate and you can avoid silly mistakes.

Jaguar