Spring net error - c#

Hi i have problem with following error in spring net
An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in Spring.Core.dll but was not handled in user code
Additional information: Error creating context 'spring.root': Keyword not supported: 'port'.
I have configurtaion in xml for dataprovider ans session factory, where probably is problem but i cant find where. Because when i am remove port (Port=3306;) from datasource definition it work neither
Error
An exception of type 'System.Configuration.ConfigurationErrorsException' occurred in Spring.Core.dll but was not handled in user code
Additional information: Error creating context 'spring.root': The system cannot find the file specified
<db:provider id="DbProvider"
provider="System.Data.SqlClient"
connectionString="Server=127.0.0.1;Port=3306;Database=dbschema;User ID=XXX; Password=XXXX"/>
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate4">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>WarehouseTemplate</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.MySQL5Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.MySqlDataDriver"/>
<entry key="use_proxy_validator" value="false" />
<entry key="show_sql" value="true"/>
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
My DB is MySql 5.7. Db is running on local but i want configuratiin with ip because i am planning after test move db elsewhere.

Related

NHibernate.MappingException: NHibernate.PropertyNotFoundException: Could not find a getter for property in class

Having trouble with nHibernate mapping my enum to the DB.
Tried looking at the mapping but can't see anything wrong, my suspicions by this point are that it may have something to do with the enum?
Here's Fortron.Crm.Domain.Policy:
namespace Fortron.Crm.Domain
{
[Serializable]
public class Policy
{
private PolicyStatus _policyStatus;
public PolicyStatus PolicyStatus
{
get { return _policyStatus; }
set { _policyStatus = value; }
}
}
Here's the class mapping
<class name="Fortron.Crm.Domain.Policy, Fortron.Crm.Domain" table="Policy" lazy="false">
<id name="Id" access="nosetter.camelcase-underscore" column="PolicyId" unsaved-value="-1">
<generator class="identity" />
</id>
<set name="Claims" access="field.camelcase-underscore" inverse="true" lazy="false" cascade="save-update">
<key column="PolicyId" />
<one-to-many class="Fortron.Crm.Domain.Claim, Fortron.Crm.Domain" />
</set>
<many-to-one name="Product" column="ProductId" access="nosetter.camelcase-underscore" />
<property name="PolicyNumber" />
<property name="VehicleRegistrationNumber" />
<property name="ContractNumber" />
<property name="ContractPaymentAuthorised" />
<property name="ContractPaymentAuthorisedAt" />
<component name="Contact" access="nosetter.camelcase-underscore">
<property name="Title" />
<property name="GivenNames" />
<property name="Surname" />
<property name="BusinessName" />
<property name="DateOfBirth" />
<property name="Gender" column="GenderId" />
<property name="TelephoneNumber" />
<property name="MobileTelephoneNumber" />
<property name="WorkTelephoneNumber" />
<component name="Address" access="nosetter.camelcase-underscore">
<property name="StreetLine1" column="StreetLine1" />
<property name="StreetLine2" column="StreetLine2" />
<property name="CityTown" column="CityTown" />
<property name="Postcode" column="Postcode" />
<many-to-one name="StateTerritory" column="StateTerritoryId" />
</component>
</component>
<property name="CustomerNumber" column="CustomerNumber" not-null="false" />
<property name="Vin" column="Vin" not-null="false" />
<property name="PolicyStatus" column="PolicyStatusId" />
</class>
Lastly here's the stack trace:
Service cannot be started. System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> NHibernate.MappingException: Could not compile the mapping document: Fortron.Crm.Domain.Policy.hbm.xml ---> NHibernate.MappingException: Problem trying to set property type by reflection ---> NHibernate.MappingException: class Fortron.Crm.Domain.Policy, Fortron.Crm.Domain, Version=2.0.0.1, Culture=neutral, PublicKeyToken=6f168f2566a816b4 not found while looking for property: PolicyStatus ---> NHibernate.PropertyNotFoundException: Could not find a getter for property 'PolicyStatus' in class 'Fortron.Crm.Domain.Policy'
at NHibernate.Properties.BasicPropertyAccessor.GetGetter(Type type, String propertyName)
at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
--- End of inner exception stack trace ---
at NHibernate.Util.ReflectHelper.ReflectedPropertyClass(String className, String name, String accessorName)
at NHibernate.Mapping....
Any ideas?
Change your member signature to make it virtual:
public virtual PolicyStatus PolicyStatus
NHibernate expects all the members of your entity to be virtual for binding. This is necessary because NHibernate creates proxy (to implement lazy loading) in certain cases. Without marked as virtual, this will not be possible for NHibernate.
You may refer this (or archive) article:
The quick answer to that question is: because we need members to be virtual in order to do our lazy loading magic/voodoo.
The longer answer is more interesting though. An important feature that any real ORM must have is transparent Lazy Loading. If you retrieve an object through an ORM, you don't want it to automatically pull in an entire object graph (not by default anyway), yet you don't want to litter your code with checks to see if certain associations have been loaded yet, and then loading them if necessary. This is the ORM's responsibility. Ideally, you want to be able to access properties and have the ORM load the necessary data upon first access of those properties if the data hasn't been retrieved yet.
NHibernate has this ability, yet it doesn't require you to inherit from some kind of NHibernate base class or implement any interfaces or anything like that. So how does it work? Well, NHibernate uses proxies of your classes at runtime whenever lazy loading is required. Ok, so what exactly is a proxy? In this case, an NHibernate proxy is a type which is generated dynamically when NHibernate is initialized for your application (this only happens once upon application startup). A proxy type will be generated for each of your entities that hasn't explicitly been mapped to avoid lazy loading (more on this later). A proxy type for one of your entities will actually inherit from your entity, and will then intercept each possible call you can perform on that type.

EDMX model is generating Defining Query node even the table has a primary key

I am using a custom ado.net provider for a java based SQL database. I'm now integrating Entity Framework using this custom ado.net provider. It is generating model from existing tables. But somehow during model generation, it is creating "DefiningQuery" node for the table and giving this following error. I have provided the whole SSDL content below. As you can see after the error message, the table definition is provided with Entity Key, which is the PRIMARY KEY in the database.
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model21.Store" Alias="Self" Provider="myADO.NET.Provider" ProviderManifestToken="myADO" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="Model21StoreContainer">
<EntitySet Name="TEST3" EntityType="Model21.Store.TEST3" store:Type="Tables" store:Schema="DBO" store:Name="TEST3">
<DefiningQuery>SELECT
"TEST3"."ID" AS "ID",
"TEST3"."NAME" AS "NAME"
FROM "DBO"."TEST3" AS "TEST3"</DefiningQuery>
</EntitySet>
</EntityContainer>
<!--Errors Found During Generation:
warning 6002: The table/view 'myDatabase.DBO.TEST3' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.
-->
<EntityType Name="TEST3">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="integer" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="NAME" Type="varchar" MaxLength="50" />
</EntityType>
</Schema>
</edmx:StorageModels>
Any help and pointers would be highly appreciated.

LocalSessionFactoryObject - MappingResources - Path to the hbm file

The project structure is:
Now I am trying to create a bean NHibernateSessionFactory as:
<object id="NHibernateSessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate32">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingResources">
<list>
<value>EMSApplication.Domain/EMSApplication.hbm.xml</value>
</list>
</property>
<property name="HibernateProperties">
<dictionary>
<entry key="hibernate.connection.provider" value="NHibernate.Connection.DriverConnectionProvider"/>
<entry key="dialect" value="NHibernate.Dialect.MsSql2008Dialect"/>
<entry key="connection.driver_class" value="NHibernate.Driver.SqlClientDriver"/>
<entry key="proxyfactory.factory_class" value="NHibernate.Bytecode.DefaultProxyFactoryFactory, NHibernate"/>
<entry key="show_sql" value="true"/>
<entry key="hbm2ddl.auto" value="update"/>
<entry key="cache.use_query_cache" value="true"/>
</dictionary>
</property>
<property name="ExposeTransactionAwareSessionFactory" value="true" />
</object>
But I am getting the error message:
Error creating context 'spring.root': file [C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\EMSApplication.Domain\EMSApplication.hbm.xml] cannot be resolved to local file path - resource does not use 'file:' protocol.
Now the project is on D drive, I am wondering why this is pointing C drive?
Also how can I add the specified hbm file in the mapping resource? This file is set as 'Embedded Resource'.
Is there any way to specify a directory here to scan for the multiple hbm files?
Any information will be very helpful to me.
You are not using a protocol identifier (e.g. file:// or assembly://) in your resource name, so Spring uses the default file protocol. It tries to resolve the hbm file from the location where the web app is running (the devserver path), not where it's files are stored, which you seem to expect.
To reference a file in your website use a ~ to identify the root of your web site, e.g. <value>file://~/EMSApplication.Domain/EMSApplication.hbm.xml</value>. But make sure the file is copied, which might not be the case, since you have set it to embedded resource (why?).
Consider the following improvements, they'll make your live more easy:
move the code in your folders dao, domain and NHibernate to a separate assembly (a class library project that is referenced from your web application) named Your.AssemblyName.
The session factory also supports specifying mapping assemblies to scan, so that you don't have to specify each file - see the docs on setting up a session factory using spring.net. Specify Your.AssemblyName in the mapping assembly list:
<object id="MySessionFactory" type="Spring.Data.NHibernate.LocalSessionFactoryObject, Spring.Data.NHibernate">
<property name="DbProvider" ref="DbProvider"/>
<property name="MappingAssemblies">
<list>
<value>Your.AssemblyName</value>
</list>
</property>
<property name="HibernateProperties">
<!-- snip -->
</object>
And check out the NHibernate example in the spring docs - it's very applicable to the questions you are raising here.

nhibernate 3.0 Oracle connection through ODP.NET

Can someone tell me whats wrong with the nhibernate config below? Using the latest ODP.NET version.
<hibernate-configuration xmlns="urn:nhibernate-configuration-2.2" >
<session-factory>
<property name="connection.driver_class">NHibernate.Driver.OracleDataClientDriver</property>
<property name="connection.connection_string">DATA SOURCE=Oracle2;PERSIST SECURITY INFO=True;USER ID=***;PASSWORD=*****</property>
<property name="show_sql">true</property>
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="dialect">NHibernate.Dialect.Oracle9Dialect</property>
<property name="query.substitutions">true 1, false 0, yes 'Y', no 'N'</property>
</session-factory>
</hibernate-configuration>
I get the following error
Error 1 Test 'ODPNETNH.Tests.GenerateSchema_Fixture.Can_generate_schema' failed: NHibernate.MappingException : Could not compile the mapping document: ODPNETNH.Mappings.Vendor.hbm.xml
----> NHibernate.HibernateException : Could not instantiate dialect class NHibernate.Dialect.Oracle9Dialect
----> System.TypeLoadException : Could not load type NHibernate.Dialect.Oracle9Dialect. Possible cause: no assembly name specified.
at NHibernate.Cfg.Configuration.LogAndThrow(Exception exception)
at NHibernate.Cfg.Configuration.AddDeserializedMapping(HbmMapping mappingDocument, String documentFileName)
at NHibernate.Cfg.Configuration.AddValidatedDocument(NamedXmlDocument doc)
at NHibernate.Cfg.Configuration.ProcessMappingsQueue()
I had quite a few problems working with NHibernate and Oracle (ODP.NET).
At the end the only thing which worked for me was to reference Oracle.DataAccess.dll ver. 2.111, specifying Copy Local = true for the DLL.
In my Nhibernate config I've used Oracle10gDialect.
Last but not least I've used this new connection-string style:
<property name="connection.connection_string">
Data Source=(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=192.168.1.1)(PORT=1522)))(CONNECT_DATA=(SERVICE_NAME=DIAP60FF)));User ID=myusername;Password=mypassword;</property>
I think I figured it out. Had to call Configure("hibernate.cfg.xml");

NHibernate Duplicate class/entity mapping problem

I've have started my foray into C#.NET and NHibernate and I'm finally stuck on an exception I can't seem to figure out, and Google isn't helping.
I'm getting a "NHibernate.DuplicateMappingException : Duplicate class/entity mapping" on my Parent class. Below is my mapping file for the Parent class, and the Youth class that uses the Parent class:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Surrix.Cerberus.YouthData"
namespace="Surrix.Cerberus.YouthData.Domain">
<class name="Parent">
<id name="parentId">
<generator class="guid" />
</id>
<property name="firstName" not-null="true" />
<property name="lastName" not-null="true" />
<property name="homePhone" />
<property name="parentEmail" />
<property name="relationshipToYouth" />
<!-- Address component that should map to the Address class -->
<component name="parentAddress">
<property name="street" />
<property name="state" />
<property name="zipCode" />
<property name="city" />
</component>
</class>
</hibernate-mapping>
And here is the relevant part of the Youth class (it is considerably bigger)
<set name="YouthParents" table="YouthParents" cascade="none">
<key column="youthId" />
<many-to-many column="parentId" class="Parent"/>
</set>
Only other thing is the Youth class also has the firstName and lastName properties, but I can't see that being a problem.
Make sure you are not doing both of these 2 things.
(1) adding the assembly in code:
// Code Configuration
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof(Employee).Assembly);
// Presuming Employee resides in "MyAssembly" as seen below.
(2) And then also adding the assembly in the config file:
<!-- .config configuration -->
<session-factory>
<!-- bunch of other stuff -->
<mapping assembly="MyAssembly"/> <!-- as in MyAssembly.dll -->
</session-factory>
You are adding the file or assembly containing the mapping twice to your Configuration object.
I had this problem, and solved it by putting this statement in hibernate.cfg.xml file:
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
Another possible cause to generate this error is multiple hbm files referencing the same Assembly during a Configuration.AddAssembly.
All hbm files in the same assembly are processed with one AddAssembly call.
As it gives a duplicate class entity mapping I can only imagen that you have two or more *.xml.hbm files referring to the same .net class.
Make sure that the xml class element for your Youth class does not have the same value for the name attribute.

Categories