NHibernate, could not load an entity when column exists in the database.

Posted by Eitan on Stack Overflow See other posts from Stack Overflow or by Eitan
Published on 2010-05-25T12:17:27Z Indexed on 2010/05/25 12:21 UTC
Read the original article Hit count: 717

This is probably a simple question to answer but I just can't figure it out.

I have a "Company" class with a many-to-one to "Address" which has a many to one to a composite id in "City". When I load a "Company" it loads the "Address", but if I call any property of "Address" I get the error:

{"could not load an entity: [IDI.Domain.Entities.Address#2213][SQL: SELECT address0_.AddressId as AddressId13_0_, address0_.Street as Street13_0_, address0_.floor as floor13_0_, address0_.room as room13_0_, address0_.postalcode as postalcode13_0_, address0_.CountryCode as CountryC6_13_0_, address0_.CityName as CityName13_0_ FROM Address address0_ WHERE address0_.AddressId=?]"}

The inner exception is:

{"Invalid column name 'CountryCode'.\r\nInvalid column name 'CityName'."}

What I don't understand is that I can run the query in sql server 2005 and it works, furthermore both those columns exist in the address table.

Here are my HBMs:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="IDI.Domain" namespace="IDI.Domain.Entities" >
    <class name="IDI.Domain.Entities.Company,IDI.Domain" table="Companies">
        <id column="CompanyId" name="CompanyId" unsaved-value="0">
            <generator class="native"></generator>
        </id>
        <property column="Name" name="Name" not-null="true" type="String"></property>
        <property column="NameEng" name="NameEng" not-null="false" type="String"></property>
        <property column="Description" name="Description" not-null="false" type="String"></property>
        <property column="DescriptionEng" name="DescriptionEng" not-null="false" type="String"></property>
        <many-to-one name="Address" column="AddressId" not-null="false" cascade="save-update" class="IDI.Domain.Entities.Address,IDI.Domain"></many-to-one>
        <property column="Telephone" name="Telephone" not-null="false" type="String"></property>
        <property column="TelephoneTwo" name="TelephoneTwo" not-null="false" type="String"></property>
        <property column="Fax" name="Fax" not-null="false" type="String"></property>
        <property column="ContactMan" name="ContactMan" not-null="false" type="String"></property>
        <property column="ContactManEng" name="ContactManEng" not-null="false" type="String"></property>
        <property column="Email" name="Email" not-null="false" type="String"></property>        
    </class>
</hibernate-mapping>



<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="IDI.Domain" namespace="IDI.Domain.Entities" >
    <class name="IDI.Domain.Entities.Address,IDI.Domain" table="Address">
        <id name="AddressId" column="AddressId" type="Int32">
            <generator class="native"></generator>
        </id>
        <property name="Street" column="Street" not-null="false" type="String"></property>    
        <property name="Floor" column="floor" not-null="false" type="Int32"></property>
        <property name="Room" column="room" not-null="false" type="Int32"></property>
        <property name="PostalCode" column="postalcode" not-null="false" type="string"></property>
        <many-to-one class="IDI.Domain.Entities.City,IDI.Domain" name="City" update="false" insert="false">
            <column name="CountryCode" sql-type="String" ></column>
            <column name="CityName"  sql-type="String"></column>             
        </many-to-one>
        </class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="IDI.Domain" namespace="IDI.Domain.Entities" >
    <class name="IDI.Domain.Entities.City,IDI.Domain" table="Cities">
        <composite-id>
            <key-many-to-one class="IDI.Domain.Entities.Country,IDI.Domain" name="CountryCode" column="CountryCode">
            </key-many-to-one>
            <key-property name="Name" column="Name" type="string"></key-property>
        </composite-id>

    </class>
</hibernate-mapping>

Here is my code that calls the Company:

IList<BursaUser> user;
            if(String.IsNullOrEmpty(email) && String.IsNullOrEmpty(company))
                return null;
            ICriteria criteria = Session.CreateCriteria(typeof (BursaUser), "user").CreateCriteria("Company", "comp");
            if(String.IsNullOrEmpty(email) || String.IsNullOrEmpty(company) )
            {
                user = String.IsNullOrEmpty(email) ? 
                    criteria.Add(Expression.Eq("comp.Name", company)).List<BursaUser>() 
                    : criteria.Add(Expression.Eq("user.Email", email)).List<BursaUser>();

            }

And finally here is where i get the error:

"user" was already initialized with the code above:

 if (user.Company.Address.City == null)
                    user.Company.Address.City = new City();

Thanks.

© Stack Overflow or respective owner

Related posts about nhibernate

Related posts about many-to-one