NHibernate does not filter data base on COORECT TYPE of meta data - nhibernate

I have an interface (IContactable) which is realizing by 3 classes : Person, Department, RestUnit
public interface IContactable
{
Contact Contact { get; set; }
string Title { get; }
int? Id { get; set; }
}
public class Person:IContactable
public class Department:IContactable
public class RestUnit:IContactable
There is another class, Contact, which should maintain which one of these objects are the owner of the contact entity.
A part of Contact mapping which does the job is:
ReferencesAny(p => p.Contactable)
.EntityTypeColumn("ContactableType")
.EntityIdentifierColumn("ContactableId")
.IdentityType<int>()
.AddMetaValue<Person>("Person")
.AddMetaValue<Department>("Department")
.AddMetaValue<RestUnit>("RestUnit");
So that Contact records in database would be like (The types are being saved as string):
X Y ContactableType ContactableId
... ... Person 123
... ... Person 124
... ... Department 59879
... ... RestUnit 65
... ... Person 3333
... ... Department 35564
Everything works just fine but filtering data. When I want to get some particular Contacts, say with Department type, I would write something like :
var contacts = Repository<Contact>.Find(p=>p is Department);
Nhibernate tries to filter data based on ContactableType field with an integer value but the ContactableType column is nvarchar
Generated query by NHibernate :
select .......... from contact.[Contact] where ContactableType=1
Expected query:
select .......... from contact.[Contact] where ContactableType='Department'
So NHibernate kinda using a wrong type. int instead of string.
I think NH is using the index of the object in list which AddMetaValue("Department") has added department type into...
I hope the explanation would be clear enough
I'm using NH3....
any idea?

Have you tried to add an extra line:
ReferencesAny(p => p.Contactable)
.MetaType<string>()

Related

Breeze Query: compare two columns on Related entity

Using Breeze, how can I compare two columns on a related property of an entity?
public class TableA{
public ICollection<TableB> TableBEntity {get; set;}
}
public class TableB{
public TableC TableCEntity {get; set;}
}
public class TableC {
public string columnA { get; set;}
public string columnB { get; set;}
}
var subpredicate = Predicate.create('TableCEntity.columnA', FilterQueryOp.Equals, 'TableCEntity.columnB');
var predicate = Predicate.create('TableBEntity', FilterQueryOp.Any, subpredicate);
var query1 = EntityQuery.from('TableB')
.where(subpredicate);
var query2 = EntityQuery.from('TableA')
.where(predicate );
query1 above executes without error. However query2 gives me the error:
The query specified in the URI is not valid. Could not find a property named 'TableCEntity' on type 'TableA'.
It seems as the subpredicate is not properly evaluated and it searches for the property TableCEntity on TableA instead of on TableB. If I change the subpredicate to
var subpredicate = Predicate.create('TableCEntity.columnA', FilterQueryOp.Equals, 'asamplevalue');
then, query2 works find. It just does not work the right side of the predicate is a record's column.
Am i doing something wrong or is this a bug?
The breeze 'any' and 'all' operators require that the property returned by the first expression ( "orders" in the example below) be a nonscalar navigation property. i.e. a navigation property that returns a collection. The subpredicate is then is just a simple predicate against the type returned by the first expression.
var query = EntityQuery.from("Employees")
.where("orders", "any", "freight", ">", 950);
with predicates this would be expressed as follows
var p2 = Predicate.create("freight", ">", 950);
var p1 = Predicate.create("orders", "any", p2);
var query = EntityQuery.from("Employees").where(p1);
In your case, i'm not sure what you are trying to do with your 'query2'. 'query2' appears to be a query over a collection of 'TableB' entities returned from 'TableA', but the 'subpredicate' deals with 'TableC' entities.
If this doesn't help, it might be easier to assist if you could restate your TableA, B and C names with table names that are a bit more intuitive and pluralize the collection property names.
Hope this helps.

Fluent Nhibernate join on non foreign key property

I can't find this anywhere, but it seems pretty trivial. So, please excuse if this is a duplicate.
I have something like:
public class Doctor : Entity
{
...some other properties here...
public virtual string Email { get; set; }
}
public class Lawyer : Entity
{
...some other properties here...
public virtual string Email { get; set; }
}
I want to return all doctors where there is no email match in the Lawyers table like:
select * from Doctors d
where d.Email not in
(select l.Email from Lawyers l where l.Email is not null)
or using a join:
select d.* from Doctors d
left join Lawyers l on l.Email = d.Email
where l.Email is null
The problem is that the Email is of course not set up as a foreign key. I have no mapped property on the Doctor entity that maps to Lawyer.
What I've tried so far:
ICriteria criteria = Session.CreateCriteria(typeof(Doctor))
.CreateAlias("Lawyers.Email", "LawyerEmail", JoinType.LeftOuterJoin)
.Add(Restrictions.IsNull("LawyerEmail"));
return criteria.List<Doctor>();
But, I get a "cannot resolve property Lawyer of MyPlatform.MyNamespace.Doctor" error. Any ideas how to set up my DoctorMap and adjust the criteria tomfoolery to achieve this?
NHibernate for the loss........Entity Framework for the win....
We can achieve that with a feature called subquery:
// a inner SELECT to return all EMAILs from Lawyer table
var subQuery = DetachedCriteria.For<Lawyer>()
.SetProjection(Projections.Property("Email"));
// the root SELECT to get only these Doctors
var criteria = session.CreateCriteria<Doctor>();
// whos email is not in the sub SELECT
criteria.Add(Subqueries.PropertyNotIn("Email", subQuery));
// get first 10
var result = criteria
.SetMaxResults(10)
.SetFirstResult(0) // paging
.List<Doctor>();

grails: show field of object in gsp

In my grails project I create instances of an object called Patient, that has got a field city.
class Patient {
String name;
String surname;
String cf;
String address;
String cap;
String city;
String province;
String country;
String phone_1;
String phone_2;
String email;
String vat;
}
When I create a Patient, I store city by its id, but I want that, in show() and list() methods, I see the name of the related city.
The Cities domain class is linked to a table in my db as follows
class Cities {
String cityName;
String capOfCity;
String provinceOfCity;
static constraints = {
}
static mapping = {
table 's_cap'
id column: 'idcap'
cityName column: 'comune'
capOfCity column: 'cap'
provinceOfCity column: 'prov'
version false
}
}
I suppose that I must perform a query to db to get its name by id, but how can I do it in gsp?
With your current approach you can do
def show(){
// look up your patient object
def patient = Patient.get(123)
def patientCityObject = Cities.findByCityName(patient.city)
[patientCityObject: patientCityObject ]
}
GSP:
<p>${patientCityObject.cityName}</p>
However,
If you define the association between your domains then Grails will load your city when you are accessing it. To access you city object associated with Patient, you can define it as follow:
class Patient {
...
Cities city;
...
}
Then when you have the patient object, you can easily access its property city.
def patient = Patient.get(123)
patient.city.cityName
This will give you the city object associated with your patient. Then you can pass that into your GSP and use it.
To learn more about GORM and object relation you can read Object Relational Mapping

Fluent NHibernate join table mapping

Reverse engineering an existing database to map with N-Hibernate using Fluent N-Hibernate.
How can I map this?
Address table
Id
Address1
Address2
Person table
Id
First
Last
Types
Id
TypeName
PersonAddress table (A person can have home, business etc addresses)
Id
PersonId (Id from person table)
AddressId (Id from address table)
TypeId (Id from types lookup table HOME, BUSINESS etc..)
Any help would be great. Thanks
Here's another tricky one in addition to above mapping. Don't know how easy it would be to map it.
Party Table
Id
Person Id points to Person
Identifiers Tables
Id
Party Id
Type Id
Identifier value
Employee table
Employee Id No party or person table has foreign key to this table. The employee id is stored in the
identifiers table. so for e.g. The identifier table is used store values for different types. The identifiers for a given party could DriverLicense, EmployeeId, SSN, Credit Card numeber etc, this could be many values.
Sample identifier data
Id, PartyId, TypeId, IdentifierValue
1 , 1, 1, EMPLID-1234
2 , 2, 1, EMPLID-4567
3 , 3, 1, EMPLID-34354
I am trying to get my head around this and just can't get it to mapped.
// this answer assumes you have functional Address, Person, Type, and PersonAddress objects.
public class AddressMap : ClassMap<Address>
{
public AddressMap()
{
Id(x=>x.Id);
Map(x=>x.Address1);
Map(x=>x.Address2);
}
}
public class PersonMap : ClassMap<Person>
{
public PersonMap()
{
Id(x=>x.Id);
Map(x=>x.First);
Map(x=>x.Last);
}
}
public class TypeMap : ClassMap<Type>
{
public TypeMap()
{
Id(x=>x.Id);
Map(x=>x.TypeName);
}
}
public class PersonAddressMap : ClassMap<PersonAddress>
{
public PersonAddressMap()
{
Id(x=>x.Id);
References(x=>x.Person, "PersonId");
References(x=>x.Address, "AddressId");
References(x=>x.Type, "TypeId");
}
}

NHibernate Polymorphic Query on a Collection

I'm trying to write a query in NHibernate. I don't really care if I use the Criteria API or HQL, I just can't figure out how to write the query.
Here's my model:
public class LogEntry { public DateTime TimeCreated { get; set; } }
public class Note : LogEntry { public string Content { get; set; } }
public class Workflow { public IList<LogEntry> Log { get; set; } }
I want the query to return all Workflows that which contain a Note with specific words in the Content of the note.
In pseudo-SQL, I'd write this like:
select w.*
from Workflow w
join w.Log l where l is class:Note
where (Note)l.Content like '%keyword%'
I'm not sure about the Criteria API, but HQL seems to handle polymorphic queries quite well, even when searching on a property that only exists in a specific sub-class. I would expect the following to work:
from Workflow w join w.Log l where l.class = Note and l.Content like '%keyword%'
I don't know if there is a better way, but I use subqueries for this:
from Workflow w
join w.Log l
where l in (
select n
from Note n
where n.Content like '%keyword%'
)
(if this doesn't work, write l.id in (select n.id...)
In criteria, you can directly filter properties that are only available on a subclass, but you shouldn't, because it only filters for the first subtype it finds where this property is defined.
I use subqueries as well:
DetachedCriteria subquery = DetachedCriteria.For<Note>("n")
.Add(Expression.Like("n.Content", "%keyword%"))
.SetProjection(Projections.Property("n.id"));
IList<Workflow> workflows = session.CreateCriteria<Workflow>("w")
.CreateCriteria("w.Log", "l")
.Add(Subqueries.PropertyIn("l.id", subquery))
.List<Workflow>();