NHibernate IInterceptor implementation(add properties to DB table that original domain class doesn't have) - database

How is possible to set some special column values when update/insert entities via NHibernate without extending domain classes with special properties?
For example: in my case I would like to get object and just moment before update/insert db add to that object some additional information (like user id or computer name) by using IInterceptor. In other words I would like to add a few columns to DB Table without specifying new properties in original object's class. Do I have to configure/change/add to my Object.hbm.xml or App.config in that case?
The problem is that I can't change my original objects and base classes.So I have to figure out if possible to add information to DB table without changing of the original objects (even no inherit from any base classes)
Example:
Original Object has : FirstName ,LastName,Birthday,Address properties
Customer.hbm.xml has:
<property name="FirstName" column="FirstName" type="string" not-null="true" length="64" />
<property name="LastName" column="LastName" type="string" not-null="true" length="64" />
<property name="Birthday" column="Birthday" type="DateTime" not-null="true" />
<property name="Address" column="Address" type="string" not-null="true" />
My Interceptor class has method:
public bool OnSave(object entity, object id, object[] state, string[] propertyNames, NHibernate.Type.IType[] types)
at that moment or even maybe before save I have to add to the DB Customer table additional 2 columns (Computer Name and User Name for example ) that propertyNames[] and state[] parameters don't have them from the beginning,so that should be done on the fly.
MY DB Customer table has all the columns that I have described above.
Thank you.

I haven't tried it, but you should be able to add the dynamic properties to the Configuration prior to factory creation. Then you can populate those values in your interceptor.
Ex, retrieve the relevant PersistentClass from config.ClassMappings, then add your properties to it.
private void AddProperty(
PersistentClass pc,
string propertyName,
string columnName,
IType dataType)
{
SimpleValue val = new SimpleValue(pc.Table);
Column col = new Column(dataType, 0);
col.Name = columnName;
val.AddColumn(col);
val.Type = dataType;
Property prop = new Property(val);
prop.IsUpdateable = true;
prop.Name = propertyName;
prop.PropertyAccessorName = "nosetter.camelcase";
prop.Cascade = "none";
pc.AddNewProperty(prop);
}

Related

AppEngine composite index without a simple index on the properties

So I am using Google AppEngine's Datastore via Objectify (Java). I can define the simple indexes using the #Indexed annotation (works) and I can define composite indexes in the datastore-indexes.xml.
Assuming I have a class like this
class X{
#Indexed public String a;
#Indexed public String b;
}
and the composite index
<datastore-index kind="X" ancestor="false" source="manual">
<property name="a" direction="asc" />
<property name="b" direction="asc" />
</datastore-index>
would I still need the #Indexed for my members, meaning do I need the simple index in addition to the composite index (assuming I only want to query both members ascending)?
My own tests seem to suggest that, but I'm not sure whether I'm just doing something wrong.
Every property in the composite index must be indexed by itself, which happens automatically. If you look at the low-level Datastore API, you can see that each property is indexed by default (.setProperty()), unless you tell the Datastore not to index it (.setUnindexedProperty()).
If a property is not indexed, the data in this property is not searchable at all.
From the documentation:
An entity is included in the index only if it has an indexed value set
for every property used in the index;

LInq to xml help in silverlight application

I am working on one silverlight application. I need one help regarding LInq to xml.
It is basically ERP system where objects are dynamic and entity creation is dynamic.
I have added SilverlightTable concept with dynamic objects in the applicaiton.
I have one xml like :
<NewDataSet>
<Table>
<knd_entity_Id>1</knd_entity_Id>
<CheckboxCol>0</CheckboxCol>
<kndtbkndnr>4001</kndtbkndnr>
<kndtbkndstatus>1</kndtbkndstatus>
<kndtbkndname1>Fritz & Franz Bikes GmbH</kndtbkndname1>
<kndtbkndname3 />
<kndtbkndplzstr>59321</kndtbkndplzstr>
<kndtbkndname2 />
<kndtbkndstrasse>In der Höh 8</kndtbkndstrasse>
<kndtbkndortstr>Wadersloh</kndtbkndortstr>
<kndtbkndtel>56673-54633</kndtbkndtel>
<kndtbkndfax />
<kndtbkndemail />
<kndtbkndwww>www.3s-erp.de</kndtbkndwww>
<kndtbkndmatchcode>Fritz & Franz Bikes,</kndtbkndmatchcode>
<kndtbkndlandpf>D</kndtbkndlandpf>
<kndtbkndwaehrung>EUR</kndtbkndwaehrung>
<kndtbkndlandstr>D</kndtbkndlandstr>
</Table>
<Table>
<knd_entity_Id>2</knd_entity_Id>
<CheckboxCol>0</CheckboxCol>
<kndtbkndnr>4002</kndtbkndnr>
<kndtbkndstatus>1</kndtbkndstatus>
<kndtbkndname1>Fahrrad Leasing AG</kndtbkndname1>
<kndtbkndname3 />
<kndtbkndplzstr>53622</kndtbkndplzstr>
<kndtbkndname2 />
<kndtbkndstrasse>Auf dem Holz 8</kndtbkndstrasse>
<kndtbkndortstr>Königswinter</kndtbkndortstr>
<kndtbkndtel>0245-98521</kndtbkndtel>
<kndtbkndfax />
<kndtbkndemail />
<kndtbkndwww />
<kndtbkndmatchcode>Fahrrad Leasing AG,</kndtbkndmatchcode>
<kndtbkndlandpf>D</kndtbkndlandpf>
<kndtbkndwaehrung>EUR</kndtbkndwaehrung>
<kndtbkndlandstr>D</kndtbkndlandstr>
</Table>
</NewDataSet>
Where COntents inside the Tables are not fixed. they may very as per the Entity attributes.
I need an Ilist from this XML using LInq to XML.
Kindly provide help.
Thanks and Regards,
Ruchi Patel
Can you try the below code
public class Table
{
public int EntityId {get;set;}
public string CheckboxCol {get;set;}
//TODO: Add rest of the properties
}
XElement element = XElement.Load("Your xml file path"); //replace with xml file path
if (element != null)
{
IList<Table> result = (from e in element.Descendants("Table")
select new Table
{
EntityId = int.Parse(e.Element("knd_entity_Id").Value),
CheckboxCol= e.Element("CheckboxCol").Value //TODO: Add rest of the properties
}).ToList();
}

Delete only the relationship between tables, not the data

I am using NHibernate for DB connection for the following classes:
Class A
{
public int Id{get;set;}
public List<B> InnerElements{get;set;}
}
Class B
{
public int Id{get;set;}
public string Description{get;set;}
...no reference to the parent
}
and in the DB i have As (to class A), Bs(to class b) and ABs tables where the table ABs has an autoincremented id, and the ids of A and B entities.
The configuration files for the classes are:
Class A
...other properties mapped
<bag name="InnerElements" table="ABs" cascade="all" inverse="true">
<key column="AID" />
<many-to-many class="B" column="BID"/>
</bag>
and in the class B i dont have any refferences to class A.
When I remove an B element from the InnerElements from an entity of type A, and try to Save/Update the modified entity back to the DB, the inner ABs table remains unmodified.
How can I change (in the config files or otherwise) to remove the entry from the inner table ? I dont want to remove any A or B entry from DB.
Thanks in advance,
Tamash
Remove the inverse="true". That attribute, which looks copy&pasted, means you are handling the relationship from the other side, which does not exist in this case.

coldfusion 9 orm - how to define relationship

now pretend you have a db structure like this:
table Object
{
id,
name
}
table ObjectRelation
{
id,
parentID, -- points to id in the object table
childID -- points to id in the object table
}
what i'd like to have in my model is the following:
{
property name
property children
property parent
}
how would you guys define the parent property in this case? keep in mind that the root element(s) obviously don't have a parent object.
Is this what you were looking for?
component persistent="true" {
property name="id" ormtype="integer" type="numeric" column="id" fieldtype="id" generator="identity";
property name="name";
property name="children"
fieldtype="one-to-many"
cfc="Object"
linktable="ObjectRelation"
fkcolumn="parentID"
singularname="child"
lazy=true
inversejoincolumn="childID";
property name="parent"
fieldtype="many-to-one"
cfc="Object"
linktable="ObjectRelation"
fkcolumn="childID"
lazy=true
inversejoincolumn="parentID";
}

What Linq to XML do I need to bind XML to Silverlight Datagrid

I was looking at this question and it gave me about 80% of what I needed. The "problem" for me is that my XML is strucutred differently and I am not quite sure how I would go about using Linq to get at what I need.
My XML looks like this (By the way, its generated by the ConvertTo-XMl PowerShell Cmdlet)
<?xml version="1.0"?>
<Objects>
<Object>
<Property Name="Name">CompiledCode.ps1</Property>
<Property Name="LastWriteTime">5/21/2009 6:59:16 PM</Property>
</Object>
<Object>
<Property Name="Name">ComputerDrawing.ps1</Property>
<Property Name="LastWriteTime">1/13/2010 7:52:44 AM</Property>
</Object>
</Objects>
I am trying to bind to a Silverlight DataGrid so that the column names will be "Name" and "LastWriteTime" and the rows will have the vale that is in the Property Tag. For the first one, the name would be Compiled Code.ps1 and LastWriteTime would be "5/21/2009 6:59:16 PM"
The answer to the above mentioned question had this method
private Status GetStatus(XElement el)
{
Status s = new Status();
s.Description = el.Attribute("Description").Value;
s.Date = DateTime.Parse(el.Attribute("Date").Value);
return s;
}
I created my own class called Scripts which has a Name and a Date, but I am trying to figure out what elements or attributes I need to populate my datagrid.
This is the basic gist of what you need:-
private Scripts GetScripts(XElement el)
{
Scripts s = new Scripts()
s.Name = (string)el.Elements("Property")
.Where(e => (string)e.Attribute("Name") == "Name")
.FirstOrDefault();
string lastWriteTime = (string)el.Elements("Property")
.Where(e => (string)e.Attribute("Name") == "LastWriteTime")
.FirstOrDefault();
s.LastWriteTime = DateTime.ParseExact(lastWriteTime, "M/d/yyyy h:m:s tt", CultureInfo.InvariantCulture);
}
You would probably want LastWriteTime to be a DateTime so you might want to place is in an intermediatory string and use a parse exact.

Resources