FluentNHibernate: how to Join to table without using primary key - fluent-nhibernate

In the following FluentNHibernate mapping:
public LawbaseAssetMap()
{
Table("PRASSET");
Id(x => x.Id).Column("PRA_RECNUM");
Join("PRSTOCK", m =>
{
m.Fetch.Join();
m.Optional();
m.KeyColumn("PRS_ASSRN");
m.Map(t => t.Certificated).Column("PRS_CERT").CustomType("YesNo");
});
}
I am performing an outer join from the table PRASSET to the table PRSTOCK. The join is between PRSTOCK.PRS_ASSRN and PRASSET.PRA_RECNUM (the primary key of PRASSET).
How would I create the equivalent join, but instead of joining onto PRASSET's primary key, join on to another field instead?

David have a look on following link, as i thing that it might help
Fluent NHibernate - Mapping a property to a column on a joined table
also this might help
ReferencesAny(x => x.Author)
.EntityTypeColumn("Type")
.EntityIdentifierColumn("Id")
.IdentityType<int>();
See more here.
Hope this helps... and now really holliday :) see u

Related

Specify the name of a parent-child join table using EF6 Code-First EntityTypeConfiguration

I'm using code first to create my DB but have to match a given schema.
I have an Org entity (simplified here) an Org can have many children of type org
public class Org
{
public int Id {get;set;}
public virtual IList<Org> Children{get;set;}
}
I want to generate an Org table and a table called OrgRelationship which has two columns ParentId and ChildId. The data is being provided to us in that format but I'd really like EF to expand the table into this model...
Is it possible to generate this join table just with an EntityTypeConfiguration on model builder? Do I have to have an OrgRelationship class?
This doesn't seem to cut it
public class OrgMap : EntityTypeConfiguration<Org>
{
public OrgMap()
{
HasKey(n => n.Id);
HasMany(n => n.Children);
}
}
You can configure the join table using the following calls.
HasMany(n => n.Children).WithMany().Map(
m => m.ToTable("OrgRelationship").MapLeftKey("ParentId").MapRightKey("ChildId"));

Ruby on Rails: Select from database where column equals a value from array

If I have an array of IDs from a table (table1) in my database. Is there a way of querying another table (table2) to select all the records where a column equals a value equal to one of the IDs from table1.
My code so far is:
LabQuestion.where("product_id=#{Product.where(:brand_id => brand_id).pluck(:id)}")
In this code, I am trying to retrieve all the Lab Questions which are linked to all the products from a brand. This code does not work, but I've tried to demonstrate my needs.
Assuming you have setup your relations properly, you can use joins to join the two tables and query them like this:
LabQuestion.joins(:product).where(:products => { :brand_id => brand_id })
You can use includes instead of joins as below
LabQuestion.includes(:product).where(:products => { :brand_id => brand_id })

NHibernate.QueryException: not an association: Id

I wrote a joint query using NHiberNate, but I am getting a NHibernate.QueryException:not an association: Id
This is what it looks like with NHibernate library
TicketRequest ticketAlias = null;
Show showAlias = null;
IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
.JoinAlias(() => ticketAlias.ShowId, () => showAlias.Id)
.Where(() => showAlias.ShowDate >=DateTime.Now)
.List();
return results;
I just want a simple joint statement, and this is what it would have been in SQL
select * from TicketRequest as a join Show as b
on a.Show_id = b.Id
where ShowDate >=GETDATE()
Can someone help and let me know why I am getting a "not an association:Id" error. I have id in the "Show" table, it is a primary key.
Please advise. All helps are greatly appreciated.
You need to specify a many-to-one relation in joins. In your case that is the Show property.
IList<TicketRequest> results = UnitOfWork.CurrentSession.QueryOver<TicketRequest>(() => ticketAlias)
.JoinAlias(() => ticketAlias.Show, () => showAlias)
.Where(() => showAlias.ShowDate >= DateTime.Now)
.List();
PS: You shouldn't map both a many-to-one relation (Show) and an foreign key property (ShowID). Usually you only work with object relations when using an ORM. Only map the plain ID if you really need it for something, but even then only map it as read-only.
You don't have to specify the foreign keys / primary keys when querying with NHibernate. It's an ORM. You write object oriented queries. The keys and relations are specified in the mapping file.
A Join in an NHibernate query is simply specified by the property name with which you navigate to the other property.
That's what the error message means. Id is not an association.

How to build custom PLINQO query by multiple id's?

Here's my table structure
Places
PlaceId PK
Name
...
PlaceCategories
CatId PK
Name
...
PlaceCats
PlaceId PK
CatId PK
Here's my query that pulls Places based on category id (table join)
public static IQueryable<Places> ByPlaceCat(this Table<Places> table, Expression<Func<PlaceCats, bool>> predicate) {
var db = (DataContext)table.Context;
var innerBizBase = db.PlaceCats.Where(predicate);
return db.Places.Join(innerBizBase, a => a.PlaceId, ab => ab.PlaceId, (a, ab) => a);
}
I use it like this:
places = Db.Places.ByPlaceCat(a => a.CatId == 5);
But I want to be able to pull based on a List<int> of category id's. Looking through the generated PLINQO code, a query that pulls by multiple PlaceId's (but not using a joined table) looks like this:
public static IQueryable<Places> ByPlaceId(this IQueryable<Places> queryable, IEnumerable<long> values)
{
return queryable.Where(p => values.Contains(p.PlaceId));
}
How could I essentially merge those two queries, to let me pass in a List<int> of CatId's to query by? This LINQ/PLINQO query is melting my brain. Thanks in advance!
You would need to write a extension method like this:
public static IQueryable<Places> ByPlaceCats(this Table<Places> table, IEnumerable<int> catIds)
{
var db = (TestDataContext)table.Context;
var places = (from placeCat in db.PlaceCats
join place in db.Places on placeCat.PlaceId equals place.PlaceId
where catIds.Contains(placeCat.CatId)
select place);
return places;
}
Please note that the PlaceCats table could be made into a ManyToMany relationship by adding two foreign keys to the proper tables. Once this change has been made than PLINQO will automatically generate the correct code and will create a link between the two tables skipping the intermediary table. So you could get a collection of PlaceCategories associated to the current Places entity by accessing a property on the Places entity.
Please remember to contact us if you have any questions and be sure to check out the community forums located here and PLINQO forums here.
Thanks
-Blake Niemyjski (CodeSmith Support)

Fluent NHibernate - Map 2 tables to one class

I have two tables that I want to map to one class that will looks like:
CUSTOMER_INFO_CLASS.cs
----------------------
Id (CUSTOMER table)
CustomerName (CUSTOMER table)
CustomerTypeDesc (CUSTOMER_TYPE table)
I tried to do it with join, as follows:
Table("CUSTOMER");
Id(x => x.ID).Length(10).Column("CustomerId");
Map(x => x.CustomerName);
Join("CUSTOMER_TYPE", m =>
{
m.Optional();
m.Map(x => x.CustomerTypeDesc);
m.KeyColumn("CustomerType");
});
The problem is that the field with whom I'm trying to link the two tables is not a primary key in any of them. (And by default the join done by the field that defined as ID)
So I found that for the CUSTOMER_TYPE table I can define the field by “KeyColumn”.
How can I define that the related column in the CUSTOMER table will be CustomerTypeCode and not CustomerId? (if I can at all)
At the end the sql query should looks like:
Select Id, CustomerName, CustomerAddress, CustomerTypeDesc
From CUSTOMER t1
Left join CUSTOMER_TYPE t2
On t1.CustomerTypeCode = t2.CustomerType
If the Customer table maps the CustomerType member to the primary key of the CustomerType table, then Hibernate should do the join automatically for you.
Is there a reason why the CustomerType is not linked by a normal foreign key reference?