how to map a bag/list/set in a class from a table which is got an indirect link to a table - nhibernate

I know the title might sound a bit weird but I've attached a screen of the database design. I have two master tables with a many-to-many relationship in between. But we're using the primary key from the many-to-many table to refer another table called ResourceAllcoation table with the foreign key (ProjectResourceID)
Now what would be the best way to get a bag of ResourceAllocation in the Resource entity? Is there a direct way to do this in nhibernate?
Currently my nhibernate mapping has a one-to-many bag to the ProjectResource table which is then referring the ResourceAllocation table which I feel might not be the best approach.
Please forgive my ignorance. Any thoughts is appreciated.
#kalki, when I implemented your mapping, I figured that the sql query getting generated is
SELECT
*
FROM PROJECTRESOURCE P
LEFT OUTER JOIN RESOURCEALLOCATION R
ON P.PROJECTRESOURCEID=R.ID WHERE P.RESOURCEID=1
but that doesn't work since ProjectResource doesn't have a PROJECTRESOURCEID column
If the query generated was
SELECT *
FROM PROJECTRESOURCE P
LEFT OUTER JOIN RESOURCEALLOCATION R
ON P.ID = R.PROJECTRESOURCEID
WHERE P.RESOURCEID=1
it would work.

You can map this using a Many-to-Many in NHibernate and an IdBag

Related

SQL JOIN from other JOIN

So, this is probably gonna sound like a weird or dumb question.
I have this application that I'm writing, which consists of four tables. Two are 'connected' to one main table, and one last one is connected to one of those two tables. I've included a database diagram to give you a better idea of what I mean.
Now, my goal is to have 'Bedrijfsnaam' from the 'Bedrijven' table into the 'Samenwerkingen' table. Problem is: I can't add more than two foreign keys, so I was assuming that I would have to create a FK in'Contactpersonen' table and pick it from the 'Bedrijven' table. It would basically mean I'd have a JOIN in 'Contactpersonen' table to my 'Bedrijven' table. And then the 'Samenwerkingen' table has a JOIN to the 'Contactpersonen' table and accesses the column from 'Bedrijven'.
Does that make any sense? Hope it does, because I could really use some help making this possible. xD
Since Samenwerkingen has a foreign key to Contactpersonen which is itself related to Bedrijven, you don't need an additional constraint: your data integrity is guaranteed and you can join the two tables.
Your query should look like :
select s.*, b.* from Samenwerkingen s
inner join Contactpersonen c on s.Contactpersonen = c.Contactpersonen
inner join Bedrijven b on c.Bedrijfnaam = b.Bedrijfnaam

Access: Updatable join query with 2 primary key fields that are both also foreign keys

In MS Access, I am trying to implement a many-to-many table that will store 2-way relationships, similar to Association between two entries in SQL table. This table stores info such as "Person A and Person B are coworkers" "C and D are friends", etc. The table is like this:
ConstitRelationships
LeftId (number, primary key, foreign key to Constituents.ConstitId)
RightId (number, primary key, foreign key to Constituents.ConstitId)
Description (text)
Note that the primary key is a composite of the two Id fields.
Also the table has constraints:
[LeftId]<>[RightId] AND [LeftId]<[RightId]
The table is working ok in my Access project, except that I cannot figure out how to make an updateable query that I want to use as a datasheet subform so users can easily add/delete records and change the descriptions. I currently have a non-updatable query:
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) OR (Constituents.ConstituentId =
ConstitRelationships.LeftId);
If I ignore the possibility that the constituentId I want is in the leftId column, I can do this, which is updatable. So the OR condition in the inner join above is what's messing it up.
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName, Constituents.LastName,
ConstitRelationships.Description, ConstitRelationships.LeftId,
ConstitRelationships.RightId
FROM ConstitRelationships INNER JOIN Constituents ON
(Constituents.ConstituentId =
ConstitRelationships.RightId) ;
I also tried this wacky iif thing to collapse the two LeftId and RightId fields into FriendId, but it was not updateable either.
SELECT Constituents.ConstituentId, Constituents.FirstName,
Constituents.MiddleName,
Constituents.LastName, subQ.Description
FROM Constituents
INNER JOIN (
SELECT Description, Iif([Forms]![Constituents Form]![ConstituentId] <>
ConstitRelationships.LeftId, ConstitRelationships.LeftId,
ConstitRelationships.RightId) AS FriendId
FROM ConstitRelationships
WHERE ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.RightId)
OR ([Forms]![Constituents Form]![ConstituentId] =
ConstitRelationships.LeftId)
) subQ
ON (subQ.FriendId = Constituents.ConstituentId)
;
How can I make an updatable query on ConstitRelationships, including a JOIN with the Constituent.FirstName MiddleName LastName fields?
I am afraid that is not possible. Because you use joins in your query over three tables it is not updatable. There is no way around this.
Here some detailed information about the topic: http://www.fmsinc.com/Microsoftaccess/query/non-updateable/index.html
As mentioned in the linked article one possible solution and in my opinion best solution for you would be the temporary table. It is a load of work compared to the easy "bind-form-to-a-query"-approach but it works best.
The alternative would be to alter your datascheme in that way that you do not need joins. But then denormalized data and duplicates would go rampage which makes the temporary table a favorable choice.

How to join Two tables of two non relashinship defined columns using Nhibernate QueryOver

Using NHibernate QueryOver, I want to join two tables using two columns which are not defined in the mapping as a relationship.
E.g. This is not my exact scenario but this can explain that
Tables:
Employee(Id, Name, DepartmentId, SomeCode,Address)
Department (Id, Name, ,Code)
Select
SELECT *
FROM EMPLOYEE E
JOIN DEPARTMENT D
ON D.Code = E.SomeCode
Can someone please tell me how to do this query using NHibernate QueryOver. Note that "SomeCode" in Employee and "Code" in Department are not defined as a relationship. DepartmentId is the foreign key and I can join these using JoinAlias but I want it in a somewhat different way.
There are similar questions, e.g.
NHibernate (+ FluentNhibernate) : Join two detached tables
Hibernate Criteria Projection without mapped association of tables
And the answer is:
either use HQL and CROSS JOIN (with WHERE)
There is NO way how to do that with QueryOver/Criteria)
See the doc:
14.2. The from clause
Multiple classes may appear, resulting in a cartesian product or "cross" join.
from Formula, Parameter
from Formula as form, Parameter as param
So using HQL in your case, we can do it like this:
SELECT ...
FROM EMPLOYEE E, DEPARTMENT D
WHERE D.Code = E.SomeCode
...
BUT I would suggest: create that mapping in the code. Simply introduce many-to-one relation. It will be loaded lazily (only if used) and will nicely serve to our needs - be used in QueryOver as a relation for Join
If there is such relation, if this exist in the business object domain, we should not be scared to use it. We can hide it via security etc...

hibernate define class from multiple columns in multiple tables

i have a class with some datafields:
class Call{
roomId
roomDisplay
roomLocation
typeId
name
staffAidId
}
now i would like to map this using hibernate annotations in java
but the original sql query that i used was something like this:
SELECT
c.roomId,
r.display,
r.location,
c.typeId,
c.staffAidId,
s.firstname,
s.lastname
FROM callalert c
JOIN staffmember s
LEFT JOIN roomGroup g ON g.groupId = s.roomGroupId
LEFT JOIN room r ON r.roomId = g.roomId
WHERE s.staffId = 4444 AND c.roomId = g.roomId
is there a way to map this
in other words:
is there a way to map columns from different tables to 1 custom class?
the class is no direct representation of 1 table
and from all the tables that are involved, not all columns are used
edit
I tried the view solution:
created a view with the above query
then I try to generate the hibernate classes using Netbeans
and I generates 2 classes:
the class Calls and an Embeddable class CallId
after searching the Internet this happens because hibernate needs a primary key so it creates one himself
how can I make sure only 1 class gets generated?
how do I give a view a primary key (a good primary key would be 1 of the primary keys of the underlying tables). how do i set this?
You should be able to do that if you create a database view for that query. It would work, but it has the downside that you can't auto-generate the ddl schema from the database models.
Check out this article

NHibernate Mapping: Simple Join on a Foreign Key

The Tables
Currencies
----------
CurrencyId INT IDENTITY PK
IsoCode4217 CHAR(3) -- "USD", "GBP", "EUR", etc
Users
----------
UserId INT IDENTITY PK
CurrencyId FK REFERENCES Currencies (CurrencyId)
The Mapping
The current application has a Currency object that needs the IsoCode4217 value. A Currency is not an entity in this application, it's its own weird hard-coded thing where I need to pass the IsoCode4217 value to a static function and get a Currency instance back. The database Currencies table is more of an enumeration table. So I figured I'd just implement an IUserType and off I'll go, but this does not work as I expect:
[hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"]
[class
name="BlahBlah.UserProfile, BlahBlah.Model"
schema="Core"
table="Users"
lazy="false"]
[id name="Identity" access="field.camelcase" column="UserId"]
[generator class="native" /]
[/id]
[join
schema="Core"
table="Currencies"
fetch="join"]
[key column="CurrencyId" property-ref="Currency" /]
[property
name="Currency"
column="IsoCode4217"
type="BlahBlah.CurrencyUserType, BlahBlah.Model" /]
[/join]
[/class]
[/hibernate-mapping]
(I changed the angle brackets to regular brackets as I don't feel like thinking about what this little Markdown editor is going to do.)
The Ugly
The SQL that I would expect is
SELECT
u.UserId,
c.IsoCode4217
FROM Users AS u
INNER JOIN Currencies AS c
ON u.CurrencyId = c.CurrencyId
but instead NHibernate stubbornly gives me
SELECT
u.UserId,
c.IsoCode4217
FROM Users AS u
INNER JOIN Currencies AS c
ON u.UserId = c.CurrencyId -- DANG IT!
Is this really not supported, or am I overlooking something obvious? Is it possible to just "loop in" an extra column from another table for the purpose of a single entity's mapping?
Thanks!
Join mappings are a PITA! The problem here is that Hibernate expects a join mapping to map from a Parent table to a Child table, whereas you're joining from the child table to the parent table.
Essentially, join mappings were designed to make it easy to join on tables that have a one-to-one relationship and where the parent table is joining to a child table.
I think in your situation you want to have a many-to-one mapping rather than a join