finding data column wise in nhibernate - nhibernate

I have a Employee class, i am retriving employee data using nhibernate.It gives
complete column.(eq. sql - Select * ). But, if i want to have slected column (like EmployeeName and EmployeeID only),then i need to create a class with these two properties(e.g.Empl class).and using AliasToBeanResultTransformer i.e. Projection i can retrive. I want to know that is there any way in nhibernate (without creating sub classes(Empl class), or without hard coding hql query)so that i can retrive specific column values...
class Employee
{
public int EmployeeId { get; set; }
public string EmployeeName { get; set; }
public int ResidingInCountryId { get; set; }
public virtual Country ResidenceCountry { get; set; }
}

If you only want to project a few columns from a query then you can either
a) use a DTO although you mentioned you don't like this for some reason
b) if you REALLY want a list of anonymous objects then simply omit the result transformer which will result in a list of anonymous objects with the projected values. This would be sufficient for databinding.
Also you do not need to use just HQL for projectons, you can use Criteria, QueryOver or NH Linq.

Related

enum as string and number in the database

i have a table
table Foo
(
id,
FooType,
FooTypeName,
)
FooType is an enum with fixed values hence i want to map it to
public virtual FooType FooType { get; set; }
How can i serialize the property in both columns but use only one to read and query?
Which mapping (xml, Mapping by Code, FluentMapping) doesn't matter
You could create FooTypeName as a string property projecting the value of FooType, and map it as readonly.
Alternatively, if you don't need FooTypeName at all, just ignore it in your model and keep it updated with a trigger.
Or, if you feel like coding a lot, create a user type that maps FooType to the two columns.

Conditionally map two columns to one field with Fluent NHibernate

I have an NHibernate entity called Owner that has a SSN column and a TaxID column in the database, and I would like to conditionally map one of those two values to a more generic property on the Owner entity based on the value of a different property, StructureType. So, if the StructureType is "I", I want to map the SSN value to the generic property, and if its "C" I want to map the TaxID value to the generic property. Is this possible using Fluent NHibernate (or even regular NHibernate)? The Owner entity is a read-only entity, nothing will be written back to the database.
I was able to solve this using a Formula in Fluent NHibernate:
Map(x => x.Identification)
.Formula("CASE WHEN StructureType = 'I' THEN SSN ELSE TaxID END");
(In my original post I said it was between 'I' and 'C' but is in fact just between 'I' and every other type)
Why don't you add a readonly property?
public string Identification
{
get
{
string identification = string.Empty;
if (StructureType.Equals("I"))
identification = SSN;
else if (StructureType.Equals("C"))
identification = TaxID;
return identification;
}
}

Mapping items-itemtags-tags tables with NHibernate

Let's say I have this database design:
Items
Id
Name
Desc
ItemTags
ItemId
TagId
Tags
Id
Tag
And I want to map it to the following class
class Item
int Id
string Name
string Desc
IList<string> Tags
Please note that I don't want to declare a Tag class, I just want the Item class to have a list of strings that represent tags.
Is this possible?
Why dont you want a Tag class?it will be strongly typed and alot easier to implement...
Ref:
IList< Tag> Tags;
override the ToString() to write the "Tag" property if that makes it better..

What's the best way to handle "type" tables with LINQ to SQL?

I have some tables that represent various types. They usually just consist of an ID (int), and a name. Ideally I would have this in an enum. Is there a way to map a table like this onto an enum?
EDIT: How would I handle it if there were extra fields other than an ID and Name?
If it's just an id and a name, I generally do this:
public enum FootScent : int
{
Unknown = 0,
Mild = 1,
Sweaty =2,
SteppedInSomething = 3
}
and then on the LINQ entity property:
[Column("foot_scent_id", DbType = "Int NOT NULL")]
public FootScent Scent { get; set; }
For lookup tables with columns other than "id" and "name" that are needed, I usually just make a normal LINQ entity for them, though depending on your implementation it would probably be worth caching these to minimize trips to the DB.

NHibernate Mapping from multiple tables

Consider the following table structure...
Appointment
-----------
ID integer
Description nvarchar
StatusID smallint
Status
------
ID smallint
DisplayText nvarchar
Now, for good or for evil, we want this situation to map to a class that looks like this
class Appointment
{
public int ID {g;s;}
public string Description { g;s; }
public string Status { g; private s; }
}
I.e. we want to pull the normalised status display text straight into the entity.
The obvious answer is to create a Status entity and make the appointment class have a reference to that and map it in the normal way.
Don't create an entity class. Use an enum and EnumStringType as shown here. This is exactly what you want I think.