I have two classes: Order and User
Order has User class inside of it:
[DataMember]
public virtual User User { get; set; }
I have NHibernate mapping for both of them:
For User:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Purchasing" namespace="Purchasing.Other">
<class name="User" table="tUser">
<id name="RID">
<column name="RID" sql-type="bigint"/>
<generator class="native"/>
</id>
<property name="Created"/>
<property name="Modified"/>
<property name="UserName"/>
<property name="Email"/>
<property name="ExternalUserId"/>
</class>
</hibernate-mapping>
And I tried to add User class to Order mapping, BUT looks like something is wrong:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Purchasing" namespace="Purchasing.Other">
<class name="Order" table="tOrder">
<id name="Id">
<column name="OrderId" sql-type="bigint"/>
<generator class="native" />
</id>
<property name="DataOwnerId" />
<property name="UserId" />
<property name="OrderNo"/>
<property name="FirstTaken"/>
<property name="DateRequired" />
<property name="ExternalOrderId" />
<many-to-one name="User" class="User" property-ref="ExternalUserId">
<column name="UserId" not-null="false"/>
</many-to-one>
</class>
</hibernate-mapping>
I have a situation, when, writing Order in tOrder database, I also need to write its User to tUser database.
But looks like it didn't work. (Order mapping without User map works fine).
How in this situation mapping should look correctly, and what am I missing?
P.S Sorry for my bad English.
If we want to persist User with its Order, we can use cascading setting:
5.1.10. many-to-one
An ordinary association to another persistent class is declared using a many-to-one element. The relational model is a many-to-one association. (It's really just an object reference.)
<many-to-one
name="PropertyName" (1)
...
cascade="all|none|save-update|delete" (4)
...
/>
(4) cascade (optional): Specifies which operations should be cascaded from the parent object to the associated object.
So the mapping could be like this:
<many-to-one name="User" class="User"
property-ref="ExternalUserId"
cascade="save-update"
column="UserId" not-null="false"
/>
Now, we only have to be sure, that we try to find a user (it could be existing)
var user = session.Get<User>(userId);
if(user == null)
{
user = new User { ... };
}
And then we can assing that user to the Order
var order = new Order { ... };
order.User = user;
and calling session to persist Order - user will be as well:
session.SaveOrUpdae(order);
NOTE: if you can reference user by its native ID ... it would be better. Mapping then would look like:
<many-to-one name="User" class="User"
cascade="save-update"
column="RID" not-null="false"
/>
Mapping with property-ref should be used exceptionally
Related
I have an issue with a two subclasses using the same table and same fields. Even though the generated table has this fields as nullable uniqueidentifiers, when I save the one record that does not use the third column, then the created field has a value of "000000000-0000-0000-0000-000000000000" instead of NULL. Other parts of application require this place to be null. Here's the code. What's wrong?
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly=".."
namespace="...">
<class name="TEST" table="..." discriminator-value="not null">
<id name="Id" column="tID">
<generator class="assigned" />
</id>
<discriminator column="iTestTypeID" type="Int32" not-null="true"/>
<property name="TotalScore" column="fTotalScore"/>
<property name="IsTimedOut" column="bTimedOut"/>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly=".."
namespace="...">
<subclass name="TestA" extends="TEST" discriminator-value="3">
<many-to-one name="Department" column="uTestCriteria01" cascade="save-update" not-null="false" />
<many-to-one name="Building" column="uTestCriteria02" cascade="save-update" not-null="false" />
</subclass>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly=".."
namespace="...">
<subclass name="TestB" extends="TEST" discriminator-value="3">
<many-to-one name="Building" column="uTestCriteria01" cascade="save-update" not-null="false" />
<many-to-one name="Floor" column="uTestCriteria02" cascade="save-update" not-null="false" />
<many-to-one name="Room" column="uTestCriteria03" cascade="save-update" not-null="false" />
</subclass>
</hibernate-mapping>
My guess would be that your objects aren't using nullable types.
Do you have the types defined like this:
public class A
{
public Guid? field { get; set; }
}
Take a look at this StackOverflow article for an explanation:
How can I default a parameter to Guid.Empty in C#?
Also in your question I would list the specific field(s) you are talking about so there is no confusion and post the code for your objects that are tied to this mapping.
Wondering if anyone could point me in the right direction..
I currently have the following 3 tables in my sqlserver database
Parameter
ParameterId
ParameterName
ParameterValue
ParameterValueId
ParameterValue
ParameterParameterValue
ParameterId
ParameterValueId
I'm trying to get it where the Parameter domain object will also fetch all the ParameterValue objects as well (I'm guessing Parameter has a one-to-many relationship with ParameterValue, since a parameter can have more than one value) but I'm getting no where - the msot I've achieved is fetching the first value, rather than all :(
If anyone is willing to help or anything I can post some code and/or the mappings I'm using - as always, any help is much appreciated :)
Mappings for Parameter
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="StockMarketAdvisorDatabaseAccess.Domain"
assembly="StockMarketAdvisorDatabaseAccess">
<class name="Parameter" table="Parameter">
<id name="ParameterId">
<column name="ParameterId" sql-type="int" not-null="true" />
<generator class="identity" />
</id>
<property name="ParameterName" />
<bag name="ParameterValues" table="ParameterParameterValue" cascade="none">
<key column="ParameterValueId" />
<one-to-many class="ParameterValue" />
</bag>
</class>
</hibernate-mapping>
Mappings for ParameterValue
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
namespace="StockMarketAdvisorDatabaseAccess.Domain"
assembly="StockMarketAdvisorDatabaseAccess">
<class name="ParameterValue" table="ParameterValue">
<id name="ParameterValueId">
<column name="ParameterValueId" sql-type="int" not-null="true" />
<generator class="identity" />
</id>
<property name="Value" column="ParameterValue"/>
</class>
</hibernate-mapping>
Thanks again, just started using nHibernate so still trying to figure most it out! :)
Your issue is you are describing one thing (1-many) but your table structure is another (many-many)
If you need many values per parameter then you should simplify your table structure to:
Parameter
-------------
* ParameterId
ParameterName
ParameterValue
--------------------
* ParameterValueId
ParameterId
ParameterValue
Then your mapping can use a 1-many mapping:
<class name="Parameter" table="Parameter">
<id name="ParameterId">
<column name="ParameterId" sql-type="int" not-null="true" />
<generator class="identity" />
</id>
<property name="ParameterName" />
<bag name="ParameterValues" table="ParameterValue" cascade="none">
<key column="ParameterId" />
<one-to-many class="ParameterValue" />
</bag>
</class>
I am trying to map a SQL Server database with nHibernate that is full of tables with varchar primary keys that are generated by external software and I need update/read (no insert) access.
I cannot find a way to get past the following error:
XXXX.Tests.GMCRepository_Fixture.Can_get_existing_GMC_by_parameter'
failed: NHibernate.MappingException :
XXXX.Domain.Mappings.GMC2.hbm.xml(4,6): XML validation error: The element 'class' in namespace 'urn:nhibernate-mapping-2.2' has invalid child element 'property' in namespace 'urn:nhibernate-mapping-2.2'. List of possible elements expected: 'meta, subselect, cache, synchronize, comment, tuplizer, id, composite-id' in namespace 'urn:nhibernate- mapping-2.2'.
Research has suggested this error is relating to there not being a valid primary key (id) defined.
Mapping XML looks like:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="XXXX.Domain" namespace="XXXX.Domain" xmlns="urn:nhibernate-mapping-2.2" schema="GM.dbo">
<class name="GMC2" table="C2" lazy="true" >
<property name="PARAMETER">
<column name="PARAMETER" sql-type="varchar" not-null="true" />
</property>
...
<id name="Recid">
<column name="recid" sql-type="varchar" not-null="true" />
</id>
</class>
</hibernate-mapping>
Thanks for your help!
I believe that the convention is to have the ID mapping as the first thing under the class declaration.
Also as part of the Id mapping you need to specify the Generator of the ID. In your case I think you will need the assigned generator added to your ID mapping. Your class mapping will look something like this.
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping assembly="XXXXCRMAPI.Domain" namespace="XXXXCRMAPI.Domain" xmlns="urn:nhibernate-mapping-2.2" schema="GoldMine.dbo">
<class name="GMContact2" table="CONTACT2" lazy="true" >
<id name="Recid">
<generator type="assigned" />
<column name="recid" sql-type="varchar" not-null="true" />
</id>
<property name="Accountno">
<column name="ACCOUNTNO" sql-type="varchar" not-null="true" />
</property>
...
</class>
</hibernate-mapping>
The Scenario
I've been banging my head against the wall trying to figure out the correct mapping for 3 entities: User, Role, and Privilege. In my application, Users can have Privileges, which just give a user additional permissions. Users can also have Roles which are essentially privileges that require additional properties.
For instance, a user might have a role of "Application Administrator" in which case the ApplicationAdministratorRole.cs would need a property to contain the list of applications that a user can manage. A user could also have a privilege of "Event Administrator" in which case Privilege.cs would NOT contain any additional properties for events because in our application an event administrator can manage all events. I hope this example makes sense. If not, I can elaborate a little more.
Table Structure
[Table Name]
TBL_USERS
[Columns]
UserId (PK),
FirstName,
LastName,
CompanyId,
etc...
[Table Name]
TBL_ROLEREF (just defines the roles within the system)
[Columns]
RoleId (PK),
RoleName
[Table Name] TBL_USERROLES (table to cross reference users to roles)
[Columns]
UserRoleId (PK),
UserId,
RoleId,
ActiveDate,
DeactiveDate
[Table Name] TBL_APPLICATIONADMINISTRATORS
[Columns]
ApplicationAdministratorId (PK),
ApplicationId,
UserId,
RoleId,
ActiveDate,
DeactiveDate
[Table Name] TBL_PRIVILEGEREF
[Columns]
PrivilegeId (PK),
PrivilegeName
[Table Name] TBL_USERPRIVILEGES
[Columns]
UserPrivilegeId (PK),
UserId,
PrivilegeId,
ActiveDate,
DeactiveDate
The table structure is pretty straight forward, all privileges and roles have an ActiveDate and DeactiveDate so that we can maintain a history of a users previous roles and privileges. One thing to note is that any role requires an additional table to store any additional information that goes along with this role, in this case, TBL_APPLICATIONADMINISTRATORS will tie a user's Application Administrator role to different applications from TBL_APPLICATIONREF. Again, please let me know if I need to reword this to make better sense.
Mapping Files
[User.hbm.xml]
User objects should have a collection of Privileges and Roles. These bags should probably be sets, but for the sake of this example I don't think it should matter.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.User, Core" table="TBL_USERS">
<id name="UserId" column="USERID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERS</param>
</generator>
</id>
<property name="Title" column="USERTITLE" type="string" length="50" not-null="false" />
<property name="FirstName" column="USERFIRSTNAME" type="string" length="50" not-null="true" />
<property name="LastName" column="USERLASTNAME" type="string" length="50" not-null="true" />
<bag name="Privileges" generic="true" table="TBL_USERPRIVILEGES">
<key column="USERID" />
<many-to-many column="PRIVILEGEID" class="Core.Entities.Privilege, Core" />
</bag>
<bag name="Roles" generic="true" table="TBL_USERROLES" >
<key column="USERID" />
<many-to-many column="ROLEID" class="Core.Entities.Role, Core" />
</bag>
</class>
</hibernate-mapping>
[Privilege.hbm.xml]
Privilege objects should have a PrivilegeId, PrivilegeName, a collection of users associated with the privilege, and ActiveDate/DeactiveDate. I have commented out my failed attempts to map this.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.Privilege, Core" table="TBL_PRIVILEGEREF">
<id name="PrivilegeId" column="PRIVILEGEID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERPRIVILEGES</param>
</generator>
</id>
<property name="Name" column="PRIVILEGENAME" type="string" length="128" not-null="false" />
<!--
This does not work. NHibernate is complaining about the repeated Column "USERID"
I have made several attempts to get this to work with no luck... where am I going wrong?
<bag name="Users" generic="true" table="TBL_USERPRIVILEGES" inverse="true">
<key column="USERID" />
<many-to-many column="USERID" class="Core.Entities.User, Core" />
</bag>
-->
<!--
This also does not work. This was my attempt to join the ActiveDate and DeactiveDate into Privilege.cs
The join NHibernate creates with this setup is completely wrong...
<join table="TBL_USERPRIVILEGES">
<key column="USERPRIVILEGEID" />
<property name="ActiveDate" column="ACTIVEDATE" type="DateTime" not-null="false" />
<property name="DeactiveDate" column="DEACTIVEDATE" type="DateTime" not-null="false" />
</join>
-->
</class>
</hibernate-mapping>
[Role.hbm.xml]
I would like to have a Role base class that has properties for ActiveDate and DeactiveDate that each role (like ApplicationAdministratorRole) can inherit from so that every role is forced to have these properties. I suppose I could use an interface to enforce this as well, but this is my first shot at mapping something semi-complicated in NHibernate, so please give me some direction on this.
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="Core" namespace="Core">
<class name="Core.Entities.Role, Core" table="TBL_ROLEREF">
<id name="RoleId" column="ROLEID" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SEQ_TBL_USERROLES</param>
</generator>
</id>
<property name="Name" column="ROLENAME" type="string" length="128" not-null="false" />
<bag name="Users" generic="true" table="TBL_USERROLES">
<key column="ROLEID" />
<many-to-many column="USERID" class="Core.Entities.User, Core" />
</bag>
<joined-subclass name="Core.Entities.ApplicationAdministratorRole, Core" table="TBL_APPLICATIONADMINISTRATORS" extends="Core.Entities.Role, Core">
<key column="ROLEID" />
<property name="ApplicationAdministratorId" column="APPLICATIONADMINISTRATORID" type="Int32" />
<bag name="Applications" generic="true" table="TBL_APPLICATIONREF">
<key column="APPLICATIONID" />
<one-to-many class="Core.Entities.Application, Core" />
</bag>
</joined-subclass>
<!-- Do I need to use <join> here to set the ActiveDate and DeactiveDate in the Role base class? -->
</class>
</hibernate-mapping>
I've done a lot of reading on this stuff, but I'm obviously missing something. As you may have gathered, I am implementing a table-per-subclass strategy for Roles.
These mapping files work as they are, but they do not return the correct results. Any and all help is greatly appreciated.
Thanks guys,
Josh
You are trying to map the Users when using USERID twice, both as the key and as the reference.
You probably mean:
I don't understand this, what is the relation between TBL_PRIVILEGEREF and TBL_USERPRIVILEGES, there doesn't seems to be any:
You join-subclass mapping seems fine
just started out with NHIbernate and have one question, probably a bit of a stupi one! ;-)
I have 2 tables, Email and Attachment, an email can have zero or more attachments. so I created a hbm file like this:
<set name="Attachments" table="Attachments">
<key column="EmailId" foreign-key="fk_Attachments_Emails"/>
<composite-element class="Foo.Emails.Attachment, Foo.Emails">
<!-- PROBLEM HERE!!! -->
<property name="Id" column="Id" type="long" />
<!-- END PROBLEM -->
<property name="Name" column="Name" type="String" length="50"/>
<property name="Mime" column="MimeType" type="String" length="50"/>
<property name="Size" column="Size" type="long" />
<property name="FilePath" column="FilePath" type="String" length="256"/>
<property name="Parsed" column="Parsed" type="Boolean" />
</composite-element>
</set>
As I want to be able to search for the attachments by PK (the Id column in the set) I included it, but now everytime I try to save an email with attachments I get an error from the db as Nhibernate tries to insert a value into the PK, which my db naturally wont allow.
So, my question is, can I extract the pk for the Attqachment table but stop Nhiberntate from writing it when inserting a an email/attachment? Should I swap to another container like ?? if so wold you be abler to provide an example as I struggling to find a one that I understand!
THanks for your help!
Perhaps a more practical example? Where you have an object structure like this:
Email
--EmailId
--EmailProperty1
--AttachmentCollection
Attachment
--AttachmentId
--ParentEmail
--AttachmentProperty1
mapped to a table structure like this (not how i'd name it, but it's for example):
email
--emailId int PK, identity
--emailProp1 varchar(50)
emailattachment
--attachmentId int PK, identity
--emailId int, FK to email table
--attachmentProp1 varchar(50)
<hibernate-mapping>
<class name="Email" table="email">
<id name="EmailId">
<!-- this tells NHibernate to use DB to generate id -->
<generator class="native"/>
</id>
<property name="EmailProperty1" column="emailProp1"/>
<bag name="AttachmentCollection" lazy="true" inverse="true">
<key column="emailId"/>
<one-to-many class="Foo.Emails.Attachment, Foo.Emails"/>
</bag>
</class>
<class name="Attachment" table="emailattachment">
<id name="AttachmentId">
<generator class="native"/>
</id>
<property="AttachmentProperty1" column="attachmentProp1"/>
<many-to-one name="ParentEmail" class="Foo.Emails.Email, Foo.Emails" lazy="proxy" column="emailId">
</class>
</hibernate-mapping>
In this map, you'd get the bi-directional relationship, and that generator tag is telling nhibernate that objects with a null Id property (you can also specify another "unsaved-value"), then you're inserting the object, else updating. Should fix your current problem.
Couple other things: examine closely what kind of containers you need to use when mapping (bag vs. set vs. list). There's an excellent writeup in hibernatingrhino's NHibernateFAQ.
Also, since you're new to NHibernate, I very, very greatly recommend the summer of nhibernate screencasts. The best tool I've found so far for learning.
I think you want an bi-directional relationship. This way you can navigate the association both ways. This includes generated keys... Here is an example:
<class name="Order" table="ORDERHEADER" lazy="false" >
<id name="OrderId" column="ORDERID" type="int">
<generator class="sequence">
<param name="sequence">"ORDERID_SEQ"</param>
</generator>
</id>
<property name="OrderType" column="ORDERTYPE" type="Int16"/>
<bag name="OrderDetail" table="ORDERDETAIL" lazy="false" inverse="true">
<key column="OrderId"/>
<one-to-many class="OrderDetail" />
</bag>
<class name="OrderDetail" table="ORDERDETAIL" lazy="false">
<id name="OrderDetailId" column="ORDERDETAILID" type="int">
<generator class="sequence">
<param name="sequence">"ORDERDTLID_SEQ"</param>
</generator>
</id>
<property name="OrderId" column="ORDERID" type="Int32"/>
<property name="ItemNumber" column="ITEMNUMBER" type="Int32"/>
<property name="OrderQuantity" column="ORDERQUANTITY" type="Int32"/>
<many-to-one name="Order" class="Order" column="OrderId" />