NHibernate Domain Model and file mapping in separate projects

Is there a way to separate domain objects and mapping files into two separate projects? I would like to create one project called MyCompany.MyProduct.Core that contains my domain model and another project called MyCompany.MYProduct.Data.Oracle that contains my Oracle data mappings. However, when I try to execute the unit test, I get the following error message:

Named query "GetClients" not found.

Here is my mapping file:

<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="MyCompany.MyProduct.Core" namespace="MyCompany.MyProduct.Core" > <class name="MyCompany.MyProduct.Core.Client" table="MY_CLIENT" lazy="false"> <id name="ClientId" column="ClientId"></id> <property name="ClientName" column="ClientName" /> <loader query-ref="GetClients"/> </class> <sql-query name="GetClients" callable="true"> <return class="Client" /> call procedure MyPackage.GetClients(:int_SummitGroupId) </sql-query> </hibernate-mapping> 

Here is my unit test:

  try { var cfg = new Configuration(); cfg.Configure(); cfg.AddAssembly( typeof( Client ).Assembly ); ISessionFactory sessionFactory = cfg.BuildSessionFactory(); IStatelessSession session = sessionFactory.OpenStatelessSession(); IQuery query = session.GetNamedQuery( "GetClients" ); query.SetParameter( "int_SummitGroupId", 3173 ); IList<Client> clients = query.List<Client>(); Assert.AreNotEqual( 0, clients.Count ); } catch( Exception ex ) { throw ex; } 

I think that I can reference the assembly incorrectly, because if I put the domain model object in the MyComapny.MyProduct.Data.Oracle class, it works. Only when I separate the two projects do I encounter this problem.

+4
source share
1 answer

Yes it is possible. If the mappings are on the assembly "MyCompany.MYProduct.Data.Oracle", you need to pass this assembly to cfg.AddAssembly (). You are using the assembly "MyCompany.MyProduct.Core"

cfg.AddAssembly("MyCompany.MYProduct.Data.Oracle");

+6
source

Source: https://habr.com/ru/post/1306694/


All Articles