using class in where-condition of HQL-Query - nhibernate

I have a Entity Contact which has three joined-subclass (Person, Company, Branch).
When I do a select-query on Contact where I should only get the Person's, Companies or Branches, I can do
select from Contact a where a.class = Person
this is working correct in the case of Person and Branch. But because Branch is himself referencing Company (with CompanyId), and when I do now
select from Contact a where a.class = Company
it will replace Company with CompanyId (from the Branch-Entity).
Does someone have an Idea, how I can prevent HQL from replacing the class-value with a real column?

Try to use fully qualified name of the class (including the namespace):
select from Contact a where a.class = Your.Namespace.Company
This should help NHibernate to differentiate class name from property name.

Related

Multi Table Query Access Comparing Data

If I have three tables, one called Person, one called Owner and the other called Tenant. All three have SSN as one of the fields. What I want to do is compare the SSN from Person (that's the whole list) to see which ones do not show up in either OWner or Tenant so I can see which people in the database have never owned a unit or leased a unit. Then i would like to be able to delete these people out of the person table.
Thanks
One easy way to do this is using not in:
select p.*
from persons as p
where p.ssn not in (select ssn from owner) and
p.ssn not in (select ssn from tenant);

Retrieve count of related records

I currently have two tables in my data source that I'm referencing in this instance.
Firstly, to explain the context, I have a windows form program in VB.NET (Visual Studio 2013).
In the first table Trainer, I have the following fields : ID, First Name, Surname, Contact, Class.
In the second table Member, I have the following fields : ID, First Name, Surname, Contact, Type, TrainerID.
I have enforced referential integrity between the two tables using Trainer.ID as PK and Member.TrainerID as FK with a 1:m relationship. I'm trying to retrieve the count of related records, to the specified ID of the trainer. I want to retrieve the count of related records. So, for example on the form I click Search and provide a trainer ID, I'd like to return the amount of customers he/she has belonging to them.
I need that, so that I can work out their salary based on commission + base amount.
I've looked around a lot, read up a lot but I just can't seem to get it. Any help whatsoever will be appreciated.
If you have the trainer id, can't you just do:
select count(*) as cnt
from member m
where m.trainerid = #TheTrainerId;
select count(m.id) as count_members
from trainer t
left join member m on m.trainerid = t.id
where t.surname = 'watson'

Select statement between two table that don't have a similar attribute in SQL?

Say that I have an object named person which has (id, name, address, phone_number) as its attributions and an object named dog which has (ID, year_of_birth, breed). Then I have a relationship between the two objects named owner. Owner has the following attributions: (owner_id, dog_id).
On an E-R diagram owner would be a triangle connecting dog and person which are both rectangular. I'm giving this info just so you understand the relationship. my goal is to list the dogs ID's who's owners are NOT from Spain.
I've tried the following and dozens of different combinations like this:
select id from owner where address <> '% Spain%';
Nothing seems to work. I've been stuck on this for a few good hours now.
You need to use LIKE when using a wildcard comparison
select id
from owner
where address NOT LIKE '% Spain%'
For details see the manual: http://docs.oracle.com/cd/E11882_01/server.112/e41084/conditions007.htm#SQLRF52142
This will give the dog_ids for all dogs that have at least one owner who is not in spain:
select distinct o.dog_id
from owner o
inner join
person p
on o.owner_id = p.id
where not p.address like '% Spain%';

What is the normalized database form for these 3 tables?

So I have three tables and subsequently three models as well:
Company (table: companies)
Person (table: people)
Address (table: addresses)
How to connect these together, considering that each Person or Company can have multiple addressess, but since the addresses are in the same format I'd like to use one single 'addresses' table, so dealing/managin addresses can be done trough a single model?
Because this structure:
companies(id, name, ...)
people(id,name, ... )
addresses(id, foreign_id, foreign_model, ...)
Seems a bit out of place for me. basically this is the current sructure now. The 'foreign_model' field is a string which specifies which model the address belongs to (i.e. Company or Person).
All this under Cakephp, but I have many problems with this structure, what should I use instead?
how abt this:
companies(id, name, address_id,..)
people (id,name, address_id,... )
addresses(id,... )
Both companies and people point to the address table for their address.
Here you dont have to store any duplicate addresses if any
EDIT: If both companies and people can have many addresses
Then keep a mapping table
companies(id, name,..)
people (id,name, ... )
addresses(id,... )
Address_map(id,address_id,type)
Where type says whether its a company or a people
id --> either companyid or poepleid

Weird many to many and one to many relationship

I know I'm gonna get down votes, but I have to make sure if this is logical or not.
I have three tables A, B, C. B is a table used to make a many-many relationship between A and C. But the thing is that A and C are also related directly in a 1-many relationship
A customer added the following requirement:
Obtain the information from the Table B inner joining with A and C, and in the same query relate A and C in a one-many relationship
Something like:
alt text http://img247.imageshack.us/img247/7371/74492374sa4.png
I tried doing the query but always got 0 rows back. The customer insists that I can accomplish the requirement, but I doubt it. Any comments?
PS. I didn't have a more descriptive title, any ideas?
UPDATE:
Thanks to rcar, In some cases this can be logical, in order to have a history of all the classes a student has taken (supposing the student can only take one class at a time)
UPDATE:
There is a table for Contacts, a table with the Information of each Contact, and the Relationship table. To get the information of a Contact I have to make a 1:1 relationship with Information, and each contact can have like and an address book with; this is why the many-many relationship is implemented.
The full idea is to obtain the contact's name and his address book.
Now that I got the customer's idea... I'm having trouble with the query, basically I am trying to use the query that jdecuyper wrote, but as he warns, I get no data back
This is a doable scenario. You can join a table twice in a query, usually assigning it a different alias to keep things straight.
For example:
SELECT s.name AS "student name", c1.className AS "student class", c2.className as "class list"
FROM s
JOIN many_to_many mtm ON s.id_student = mtm.id_student
JOIN c c1 ON s.id_class = c1.id_class
JOIN c c2 ON mtm.id_class = c2.id_class
This will give you a list of all students' names and "hardcoded" classes with all their classes from the many_to_many table.
That said, this schema doesn't make logical sense. From what I can gather, you want students to be able to have multiple classes, so the many_to_many table should be where you'd want to find the classes associated with a student. If the id_class entries used in table s are distinct from those in many_to_many (e.g., if s.id_class refers to, say, homeroom class assignments that only appear in that table while many_to_many.id_class refers to classes for credit and excludes homeroom classes), you're going to be better off splitting c into two tables instead.
If that's not the case, I have a hard time understanding why you'd want one class hardwired to the s table.
EDIT: Just saw your comment that this was a made-up schema to give an example. In other cases, this could be a sensible way to do things. For example, if you wanted to keep track of company locations, you might have a Company table, a Locations table, and a Countries table. The Company table might have a 1-many link to Countries where you would keep track of a company's headquarters country, but a many-to-many link through Locations where you keep track of every place the company has a store.
If you can give real information as to what the schema really represents for your client, it might be easier for us to figure out whether it's logical in this case or not.
Perhaps it's a lack of caffeine, but I can't conceive of a legitimate reason for wanting to do this. In the example you gave, you've got students, classes and a table which relates the two. If you think about what you want the query to do, in plain English, surely it has to be driven by either the student table or the class table. i.e.
select all the classes which are attended by student 1245235
select all the students which attend class 101
Can you explain the requirement better? If not, tell your customer to suck it up. Having a relationship between Students and Classes directly (A and C), seems like pure madness, you've already got table B which does that...
Bear in mind that the one-to-many relationship can be represented through the many-to-many, most simply by adding a field there to indicate the type of relationship. Then you could have one "current" record and any number of "history" ones.
Was the customer "requirement" phrased as given, by the way? I think I'd be looking to redefine my relationship with them if so: they should be telling me "what" they want (ideally what, in business domain language, their problem is) and leaving the "how" to me. If they know exactly how the thing should be implemented, then I'd be inclined to open the source code in an editor and leave them to it!
I'm supposing that s.id_class indicates the student's current class, as opposed to classes she has taken in the past.
The solution shown by rcar works, but it repeats the c1.className on every row.
Here's an alternative that doesn't repeat information and it uses one fewer join. You can use an expression to compare s.id_class to the current c.id_class matched via the mtm table.
SELECT s.name, c.className, (s.id_class = c.id_class) AS is_current
FROM s JOIN many_to_many AS mtm ON (s.id_student = mtm.id_student)
JOIN c ON (c.id_class = mtm.id_class);
So is_current will be 1 (true) on one row, and 0 (false) on all the other rows. Or you can output something more informative using a CASE construct:
SELECT s.name, c.className,
CASE WHEN s.id_class = c.id_class THEN 'current' ELSE 'past' END AS is_current
FROM s JOIN many_to_many AS mtm ON (s.id_student = mtm.id_student)
JOIN c ON (c.id_class = mtm.id_class);
It doesn't seem to make sense. A query like:
SELECT * FROM relAC RAC
INNER JOIN tableA A ON A.id_class = RAC.id_class
INNER JOIN tableC C ON C.id_class = RAC.id_class
WHERE A.id_class = B.id_class
could generate a set of data but inconsistent. Or maybe we are missing some important part of the information about the content and the relationships of those 3 tables.
I personally never heard a requirement from a customer that would sound like:
Obtain the information from the Table
B inner joining with A and C, and in
the same query relate A and C in a
one-many relationship
It looks like that it is what you translated the requirement to.
Could you specify the requirement in plain English, as what results your customer wants to get?