nhibernate to save only required properties - nhibernate

I'm using NHibernate 2.2 for my database work and I've faced an issue recently. I have a class called PrescDrugItem which is shown below
public class PrescDrugItem
{
public virtual int ItemNumber { get; set; }
[DataMember]
public virtual int AmountIssued { get; set; }
[DataMember]
public virtual string TimePeriod { get; set; }
}
following is the mapping file
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly ="DataContractsLib"
namespace="DataContractsLib.Prescription" >
<class name="PrescDrugItem">
<id name="ItemNumber" type="Int32">
<generator class="native" />
</id>
<property name="AmountIssued" type="Int32" />
<property name="TimePeriod" type="String" length="30" />
</class>
my problem is, now I need to add another property to the class Item (say ItemTradeName etc), but I dont want it to be saved to the database( because I want to use this new property to store some data temporary). I tried update=false and insert=false in the mapping file but no success yet. Could you guys please tell me is this possible thing to do . Thank you.

If it's not to be fetched from the database either, just add it as a normal property of your class and don't map it.

Related

NHibernate 3.2 ignores entity-name

I'm using NHibernate 3.2 and have two tables mapped for same class, specifying the "entity-name" in mapping. The trouble is that when I use the method in ISession to indicate the entity name NHibernate insists deduct on their own behalf, ignoring my specification.
This is code from my unit tests:
public class Cliente
{
public virtual Guid UID { get; set; }
public virtual long Revisao { get; set; }
public virtual string Nome { get; set; }
public virtual DateTime DataNascimento { get; set; }
}
<class name="Cliente">
<id name="UID">
<generator class="guid"/>
</id>
<version name="Revisao" />
<property name="Nome" />
<property name="DataNascimento" />
</class>
<class name="Cliente" entity-name="ClienteAudit" schema="audit">
<composite-id>
<key-property name="UID" />
<key-property name="Revisao" />
</composite-id>
<property name="Nome" />
<property name="DataNascimento" />
</class>
var cliente = new Cliente {DataNascimento = DateTime.Parse("1988/07/09"), Nome = "Heber Senger"};
using (var ss = sf.OpenSession())
{
ss.Save("Cliente", cliente);
ss.Flush();
}
NHibernate insists in save the entity as "ClienteAudit" (I verify in listener and table), and I explicity inform entity name as "Cliente".
I just try:
- Specify entity name in Cliente mapping;
- Omit name in method save, let NHibernate free to discover the name, implying in "ClienteAudit" again;
- Now I studying internal code of SessionImpl and so on.
If anyone can help would be great. Thanks.
Two changes were needed to all work:
Default type of version property is int and NOT long;
And the most important: the name specified in save method is the full name of class when entity-name don't was explicity indicated in HBM.
By the way, thanks!

How to Load the RadGrid when fetching a huge data from the Table of the database using Nhibernate

I like to reduce the loading time when running the Default.aspx in the browser because it is fetching a huge data from the Table in the database.
When I use criteria.SetMaxResults(200) the grid load only 200 datas but I should load all the data's in the RadGrid from the table in the database .When I use criteria.SetFetchSize(200) it takes also a huge time to Load can anyone help me to find a solution for this.
Here is the code
ICriteria criteria = session.CreateCriteria(typeof(Ttable1));
criteria.SetMaxResults(200);
IList result = criteria.List();
if (result.Count > 0)
{
grid1.DataSource = result;
grid1.DataBind();
}
else
{
grid1.DataSource = new string[] { };
grid1.DataBind();
}
Here the Mapping is
<class name="Ttable1" table="Ttable1" lazy="false" mutable="true">
<cache usage="read-only"/>
<id name="ID" column="ID" >
</id>
<property name="CustNumber" column="CustNumber" type="String" />
<property name="CustName" column="CustName" type="String"/>
<property name="PNo" column="PNor" type="String"/>
<property name="ONo" column="ONo" type="String"/>
<property name="Ln" column="Lns" type="String"/>
<property name="Comments" column="Comments" type="String"/>
<property name="size" column="size" type="String"/>
<property name="Qty" column="Qty" type="String"/>
</class>
</hibernate-mapping>
Here is the class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Business.entities
{
public class Ttable1
{
public virtual string displayobj { get; set; }
public virtual Ttable1 TLQDataObj { get; set; }
public virtual int? ID { get; set; }
public virtual string CustNumber { get; set; }
public virtual string CustName { get; set; }
public virtual string PNo { get; set; }
public virtual string ONo { get; set; }
public virtual string Ln { get; set; }
public virtual string Comments { get; set; }
public virtual string size { get; set; }
public virtual string Qty { get; set; }
}
}
Are you show all data in grid with paging? if yes you can create custom paging for your grid by load 10 rows for every page you entered you can first retrieve paged data by using query.SetFirstResult(firstrowinpage); and query.SetMaxResults(pageSize); (HQL)
, and set Radgrid property grid.AllowCustomPaging = true; to allow custom paging and set VirtualItemCount to know grid how number of rows to allow paging.
grdItems.VirtualItemCount =numberofrows();
It is impossible to tell what is wrong from what you have given us. We don't know your mappings, how many columns does the table have, nothing.
Problems could be caused either by:
N+1 selects because all your associations are eager
You have some column in the database with huge data (maybe some attachments or something)
You should also try to paginate your results.
You must debug NHibernate queries and see what is going on. You can do that either by using show_sql, by logging with some logger, or with some profiler like NHProf or even a SQL profiler.
You will probably see many queries executed because there is no lazy load enabled.
EDIT:
If you have some columns in the table that you don't want to load unless you access them, you can set their property mapping with lazy=true, or you can map these columns with components and set these components as lazy=true. This way when you access these columns for the first time, additional queries will be performed.
If you need to fetch data for display purposes you should create a light object from the table data.
In any case, if fetching speed is your only concern you could set the object as mutable=false and set the cache <cache usage="read-only"/>.
You could do this in code also:
ICriteria crit = session.CreateCriteria(typeof(Ttable1));
crit.SetCacheable(true);
crit.SetCacheMode(CacheMode.Get);
crit.SetMaxResults(200);
IList result = crit.List();
Mapping -
<class name="Ttable1" table="Ttable1" lazy="false" mutable="false">
<cache usage="read-only"/>
<id name="ID">
<column name="ID"/>
<generator class="identity"/>
</id>
<property name="CustNumber" column="CustNumber" type="String" />
<property name="CustName" column="CustName" type="String"/>
<property name="PNo" column="PNor" type="String"/>
<property name="ONo" column="ONo" type="String"/>
<property name="Ln" column="Lns" type="String"/>
<property name="Comments" column="Comments" type="String"/>
<property name="size" column="size" type="String"/>
<property name="Qty" column="Qty" type="String"/>
</class>

Automatically removing associations when deleting entities in NHibernate

I have the following entities:
namespace NhLists {
public class Lesson {
public virtual int Id { get; set; }
public virtual string Title { get; set; }
}
public class Module {
public virtual int Id { get; set; }
public virtual IList<Lesson> Lessons { get; set; }
public Module() {
Lessons = new List<Lesson>();
}
}
}
And the following mappings:
<class name="Module" table="Modules">
<id name="Id">
<generator class="identity"/>
</id>
<list name="Lessons" table="ModuleToLesson"
cascade="save-update">
<key column="moduleId"/>
<index column="position"/>
<many-to-many
column="lessonId"
class="NhLists.Lesson, NhLists"/>
</list>
</class>
<class name="Lesson" table="Lessons">
<id name="Id">
<generator class="identity"/>
</id>
<property name="Title">
<column name="Title" length="16" not-null="true" />
</property>
</class>
When I delete a lesson by session.Delete(lesson), is there anyway I can have NHibernate automatically update the association in Module.Lessons to remove the entry from the set? Or am I forced to go through all Modules and look for the lesson and remove that by hand?
Edit: Fixed ICollection and <set> in mappings to IList<> and <list> like I want and tested it.
You have false idea. If you want to delete the Lesson object from Module you do that manually. NHibernate just tracks such your action and when session.Commit() is called then the reference between Module and Lesson is deleted in the database.
Calling session.Delete(lesson) deletes the lesson object from database (if foreign keys are set properly then reference between Module and Lesson is deleted of course but it is not responsibility for NHibernate).
In conclusion, it is not possible to delete the lesson object from the Module.Lessons list automatically by calling session.Delete(lesson). NHibernate does not track such entity references.
Turns out that if we do not need IList semantics and can make do with ICollection the update problem can be solved by adding a reference back from Lesson to Module, such as:
public class Lesson {
...
protected virtual ICollection<Module> InModules { get; set; }
...
}
And to the mapping files add:
<class name="Lesson" table="Lessons">
...
<set name="InModules" table="ModuleToLesson">
<key column="lessonId"/>
<many-to-many column="moduleId" class="NhLists.Module, NhLists"/>
</set>
</class>
Then a Lesson deleted is also removed from the collection in Module automatically. This also works for lists but the list index is not properly updated and causes "holes" in the list.

nHibernate - Complicated many-to-many class mapping

I'm new to nhibernate, and I'm sorry if this is answered elsewhere, but I've been looking for the last couple of hours, and can't find a solution that works.
A bit of background:
I'm trying to write an Admin area where there are users and sites, and a user can have access to multiple sites - but at various permission levels for each site.
Ideally I would like my classes look like this.
namespace MyApp.Users
{
public class User
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Password { get; set; }
public virtual IList<AdminUserSite> Sites { get; set; }
}
public class AdminUserSite
{
public virtual int UserTypeId { get; set; }
public virtual Site AdminSite { get; set; }
public virtual IList<Permission> Permissions { get; set; }
}
public class Permission
{
public virtual int Id { get; set; }
public virtual int AreaID { get; set; }
public virtual bool CanView { get; set }
public virtual bool CanEdit { get; set }
}
}
namespace MyApp.Sites
{
public class Site
{
public virtual int Id { get; set; }
public virtual string Title { get; set; }
}
}
And my database schema looks like this
f_user
{
f_user_id (int, PK)
name (nvarchar(50))
password (nvarchar(25))
}
f_user_site
{
f_user_id (int, PK)
f_site_id (int, PK)
d_user_type_id (int)
}
f_perm
{
f_perm_id (int, PK)
f_site_id (int)
f_user_id (int)
d_area_id (int)
can_read (bit)
can_write (bit)
}
f_site
{
f_site_id (int, PK)
title (nvarchar(50))
}
And the hibernate mapping files currently look like:
Users.hbm.xml
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyApp"
namespace="MyApp.Users"
default-lazy="true">
<class name="User" table="f_user">
<id name="Id" column="f_user_id">
<generator class="identity" />
</id>
<property name="Name" column="name" />
<property name="Password" column="password" />
<bag name="Sites" table="f_user_site" inverse="true" cascade="all-delete-orphan">
<key column="f_user_id"/>
<one-to-many class="AdminUserSite"/>
</bag>
</class>
<class name="Permission" table="f_perm">
<id name="Id" column="f_perm_id">
<generator class="identity" />
</id>
<property name="AreaId" column="d_area_id" />
<property name="CanView" column="can_read" />
<property name="CanEdit" column="can_write" />
</class>
<class name="AdminUserSite" table="f_user_site">
<property name="UserTypeId" column="d_user_type_id" />
<many-to-one name="Site" class="MyApp.Sites.Site, MyApp.Sites" foreign-key="f_site_id"></many-to-one>
</class>
</hibernate-mapping>
and Sites.hbm.xml is
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="MyApp"
namespace="MyApp.Sites"
default-lazy="true">
<class name="Site" table="f_site">
<id name="Id" column="f_site_id">
<generator class="identity" />
</id>
<property name="Title" column="title" />
</class>
</hibernate-mapping>
Individually the User, Permission and Site classes all map fine - but i just can't figure out what AdminUserSite should be, and I haven't even attempted to put the permissions list in there yet.
Does anyone have any ideas?
Any help would be very appreciated.
Saan
I think you need to work on the database schema a bit. I think the AdminUserSite table is your problem.
Firstly, work out your entities (these should be simple nouns, which is why AdminUserSite seems out of place to me).
Entities: User, Permission, Site, SiteArea
Next, work out the relationships:
1 User has many Permissions
1 SiteArea has many Permissions
1 Site has many SiteAreas
(hope I got that right :) )
After that, your hbm and tables should flow more naturally.
Remember that normally you will have 1 table per entity, unless you have a many-many relationship (in which case you will need a joining table).
Generally a relation table mapping look like this :
<class name="AdminUserSite" table="f_user_site">
<composite-id >
<key-many-to-one name="Site" column="f_site_id" class="Site" />
<key-many-to-one name="User" column="f_user_id" class="User"/>
</composite-id>
</class>
In that way you can access either Site or User by its relation object or load relation object by criteria based on Site or User.
By the way, if your Permission object contains reference to user and site you may not need the AdminUserSite relation, the permission mapping already do it.
Edit about your comment Kind of replicating the same information in two spots.
Since NHibernate -like all ORM- has a first level cache you don't have to matter if your object can be accessed in two ways. You just have to ensure that your mappings are usefull to optimize and design your application well.
In this case, it's not a 'replication' but a reference. The object will be the same in the two spots if loaded by/retrieved from the same NHibernate session.
Accessing an object in two ways if they have a logic (ie. direct way and cross-relation way) is not an heresy.
This is most a data layer work to achieve to provide right methods to access the right objects in the right way :)

NHibernate one-to-many mapping does not populate the bag

I have a simple object model of a header with multiple detail lines. I have mapped a bag property on the header object to the line class and also put a header property on the line class to set the two way relationship.
When I run my tests, using NHibernate Profiler I can see that the query is being performed, and the header and lines are fetched from the database, but the collection is always empty.
When I query for a collection of line objects directly I can get them and see the header object is correctly populated, so I know the mapping is probably OK.
The slightly non-standard aspect to this is that the line object has a compound key. (This is a legacy DB and I cannot change this), so I'm wondering if that is the problem.
Here are my classes and mappings (simplified)
<class name="Header" table="HEADER">
<id name="ID" column="HEAD_ID">
<generator class="assigned" />
</id>
<bag name="Lines" table="BODY" order-by="BODY_LINE">
<key column="BODY_HEADER_ID"/>
<one-to-many class="Line"/>
</bag>
</class>
<class name="Line" table="BODY">
<composite-id>
<key-property name="ID" column="BODY_HEADER_ID"/>
<key-property name="Line" column="SBODY_LINE"/>
</composite-id>
<many-to-one class="Header" name="Head" column="BODY_HEADER_ID" />
</class>
public class Header {
public virtual string ID { get; set; }
public virtual IList<Line> Lines { get; set; }
}
public class Line {
public virtual string ID { get; set; }
public virtual int Line { get; set; }
public virtual Header Head { get; set; }
public override bool Equals(object obj) {
var other = obj as Line;
return ID == other.ID && Line == other.Line;
}
public override int GetHashCode() {
return (ID + "|" + Line.ToString()).GetHashCode();
}
}
I can get around this by querying for the Line objects separately, but I'd like to know what I'm doing wrong.
EDIT: Ok, when I simplified things, I didn't do it very consistently. Sorry for the confusion. I have changed the mapping and class definitions to reflect things more accurately.
You are mapping the same column twice (BODY_HEADER_ID), and using different three different class names for the HEADER table.
This is the correct Line mapping:
<class name="Line" table="BODY">
<composite-id>
<key-many-to-one class="Header" name="Header" column="BODY_HEADER_ID"/>
<key-property name="Line" column="SBODY_LINE"/>
</composite-id>
</class>
And of course, Line should have only that single reference to Header.