I have the following query in HQL :
public IEnumerable<Player> PlayersNotInTeam(Team team)
{
return Session.CreateQuery("from Player p where p.Sex = :teamSex and p.Visible and p.Id not in (select pit.Player from PlayerInTeam as pit join pit.Roster as roster join roster.Team as team where team = :teamId)")
.SetParameter("teamId", team.Id)
.SetParameter("teamSex", team.Sex)
.Enumerable<Player>();
}
When I run this query with NHibernate, it will return 2 rows.
If I run the SQL script generated by NH in my database browser (SQLite Explorer):
select player0_.Id as Id26_, player0_.Sex as Sex26_, player0_.FirstName as FirstName26_, player0_.LastName as LastName26_, player0_.DefaultNumber as DefaultN5_26_, player0_.Visible as Visible26_, player0_.DefaultPosition_id as DefaultP7_26_
from Players player0_
where player0_.Sex='Male'
and player0_.Visible=1
and (player0_.Id not in
(select playerinte1_.Player_id
from "PlayerInTeam" playerinte1_
inner join "Roster" roster2_ on playerinte1_.Roster_id=roster2_.Id
inner join Teams team3_ on roster2_.Team_id=team3_.Id,
Players player4_
where playerinte1_.Player_id=player4_.Id
and team3_.Id=2));
I have 3 rows, which is what I should have.
Why are my results different?
Thanks in advance
Mike
I have noticed that sometimes the logged SQL is not exactly the same as the one being really used against the database. The last time I had this issue, it was a problem with trimming the Id value, e.g., where the generated SQL has something like and team3_.Id=2, the SQL being used was actually and team3_.Id='2 ' (or perhaps, player_0.Sex='Male '), which would always fail.
I would suggest you try this HQL:
string hql = #"from Player p where p.Sex = 'Male'
and p.Visible and p.Id not in
(select pit.Player from PlayerInTeam as pit join pit.Roster as roster join roster.Team as team where team = 2)";
return Session.CreateQuery(hql).Enumerable<Player>();
If that works, you need to check if your values have spare whitespaces in them.
I've changed my query like this:
return Session.CreateQuery("from Player p where p.Sex = :teamSex and p.Visible and not exists (from PlayerInTeam pit where pit.Player = p and pit.Roster.Team = :teamId)")
.SetParameter("teamId", team.Id)
.SetParameter("teamSex", team.Sex)
.Enumerable<Player>();
And it now works. I had the idea to use "not exists" after I changed my mappings to try to use LINQ, which gave me the hint.
If you ask why I don't keep LINQ, that's because currently I hide the relationships between my entities as private fields, to force the users of the entities to use the helper functions which associate them. But the wrong thing is that in most cases, that forbids me to use LINQ in my repositories.
But I'm wondering if this wouldn't be better to "un-hide" my relationships and expose them as public properties, but keep my helper functions. This would allow me to use LINQ in my queries.
What do you do in your apps using NH?
Do you think this would be an acceptable trade-off to maintain easy mappings and queries (with the use of LINQ), but with the cost of some potential misuses of the entities if the user doesn't use the helper functions which keep the relationships?
Related
I am developing a web app that interfaces with an ERP production database and the data is being retrieved by a complex SQL query with many joins and aliases before being passed to my controllers and views which modify them in different ways.
The result might be, for example, an array of products that contains all relevant information for each purchase such as model, serial number, customer, etc., and in a smaller scale app these would certainly all be stored in the same table.
However because I am pulling from a very complex ERP solution these details are taken from many different tables, but I'd like to treat it as a single table in my model.
Is there any way to accomplish this so that I can use Eloquent as usual? Things like Product::all() do not work, though I can call custom methods like Product::getWhere($serial) but I'd rather do this the Eloquent way since it is easier and makes pagination much nicer to work with.
Here is an idea of what my queries looks like:
SELECT
[PSerials].[ElementID] AS [ProductElementID],
[PSerials].[SerialNumber] AS [SerialNumber],
[PSerials].[SNStatus] as [ProductStatus],
[PSerials].[JobID] AS [JobID],
[Element].[Desc] AS [Description],
[PSerials_UD].[Prog] AS [ProgramNum],
[PSerials_UD].[Elec] AS [ElecPrint],
[PSerials_UD].[SinglePHVolt_c] AS [SinglePHVolt],
[PSerials_UD].[SinglePHAmp_c] AS [SinglePHAmp],
[PSerials_UD].[ThreePHVolt_c] AS [ThreePHVolt],
[PSerials_UD].[ThreePHAmp_c] AS [ThreePHAmp],
[PSerials_UD].[Notes_c] AS [Notes],
[PSerials_UD].[WarrantyExpDate_c] AS [WarrantyExpDate],
[QuoteDtl].[QuoteNum] AS [QuoteNum],
[QuoteDtl].[QuoteComment] AS [QuoteComment],
[OrderDtl].[RequestDate] AS [ShipDate],
[Customer].[CustID] AS [CustomerID],
[Customer].[Name] AS [CustomerName],
[Customer1].[CustID] AS [ShipToCustomerID],
[Customer1].[Name] AS [ShipToCustomerName]
FROM Erp.PSerials AS PSerials
INNER JOIN Erp.Element AS Element ON
PSerials.Company = Element.Company AND PSerials.ElementID = Element.ElementID
AND (Element.ProdCode = 'MACH' AND Element.ClassID = 'MAC')
INNER JOIN Erp.PSerials_UD AS PSerials_UD ON
PSerials_UD.ForeignSysRowID = PSerials.SysRowID
LEFT OUTER JOIN Erp.PProd AS PProd ON PProd.JobID = PSerials.JobID
LEFT OUTER JOIN Erp.OrderDtl AS OrderDtl ON PProd.Company = OrderDtl.Company
AND PProd.OrderNum = OrderDtl.OrderNum AND PProd.OrderLine = OrderDtl.OrderLine
LEFT OUTER JOIN Erp.QuoteDtl AS QuoteDtl ON OrderDtl.QuoteNum = QuoteDtl.QuoteNum
AND OrderDtl.QuoteLine = QuoteDtl.QuoteLine
LEFT OUTER JOIN Erp.Customer AS Customer ON OrderDtl.Company = Customer.Company
AND OrderDtl.CustNum = Customer.CustNum
LEFT OUTER JOIN Erp.Customer AS Customer1 ON PSerials.Company = Customer1.Company
AND PSerials.ShipToCustNum = Customer1.CustNum
I have it sitting in a static variable on my Product model and then have custom functions that simple tack on SQL queries to the end as needed, but this isn't elegant and it isn't fun to work with, here's an example of how I use these queries:
public static function getWhere($serial)
{
$query = DB::select(DB::raw(Self::$baseQuery));
$collection = new \Illuminate\Support\Collection($query);
$product = $collection->where("SerialNumber", $serial)->first();
return $product;
}
Does anyone how I can have my model simply treat queries like that as if they were a simple table, or is there perhaps another way to go about doing this?
Update
Thanks to Zamrony P. Juhara's advice below I was able to successfully create an SQL view and tell my Products model to reference is like a table:
class Product extends Model
{
protected $table = 'dbo.ProductInfo';
}
Now Product::all() works as expected.
Regarding the limitations pointed out by Peh this app is purely for viewing the data that exists in the ERP software and won't need to update or delete any records, so in this case the SQL view is absolutely perfect.
You can turn SQL command into VIEW (CREATE VIEW) then use it just like ordinary table in your model.
here are my tables, im using sql developer oracle
Carowner(Carowner id, carowner-name,)
Car (Carid, car-name, carowner-id*)
Driver(driver_licenceno, driver-name)
Race(Race no, race-name, prize-money, race-date)
RaceEntry(Race no*, Car id*, Driver_licenceno*, finishing_position)
im trying to list to do the query below
which drivers have come second in races from the start of this year.
lncluding race name, driver name, and the name of the car in the output
i have attempted
select r.racename, d.driver-name, c.carowner-name
from race r, driver d, car c, raceentry re
where re.finishing_position = 2 and r.race-date is ...
Something like:
select r.racename, d.driver-name, c.carowner-name
from race r
join raceentry re on r.race_no = re.race_no
join car c on re.car_Id = c.car_id
join driver d on re.driverliscenceNo = d.driverliscenceNo
where re.finishing_position = 2 and r.race-date >='20130101'
This assumes only one car and one driver with a finsih place of 2nd in a particular race. You may need more conditions otherwise. If this is your own table design, you need to start right now learning to be consistent in your nameing between tables. It is important. Fields that are in multiple tables should have the same name and data type. Also you need to stop using implicit syntax. This ia aSQL antipattern and a very poor programming technique. It leads to mistakes such as accidental cross joins and is harder to read and maintain when things get more complex. As you are clearly learning, you need to stop this bad habit right now.
First off, multiple joins in the where clause are hard to get used to when you define more than 3 or 4 tables IMHO.
Do this instead:
Select
a.columnfroma
, b.columnfromb
, c.columnfromc
from tablea a
join tableb b on a.columnAandBShare = b.columnAandBShare
join tablec c on b.columnBandCShare = c.columnBandCShare
This while no one would say is a method you have to use, it is a much more readable method of performing joins.
Otherwise you are doing the joins in the where clause and if you have other predicates with your joins you are going to have to comment out which is which if you ever need to go back and look at it.
I have the following working MySQL query:
SELECT *
FROM bogenantworten a
RIGHT JOIN
bogenfragen f ON f.id = a.bogenfragen_id
AND a.personen_id = 3,
BogenTyp t,
BogenFragenGruppe g
WHERE
t.id = f.fragentyp_id AND
g.id = f.fragengruppen_id AND
t.id = 1
ORDER BY f.sortierung ASC
Now I need this in Doctrine2 DQL or QueryBuilder. I already learned that D2 is forcing me to think in objects, but I couldn't find any advice how to tag my entities to make this work.
So I'd like to either have the above MySQL query working in my Symfony2 app or some help how to annotate my entities right so I have a working right join connection between BogenAntworten and BogenFragen (the 3 and the 1 are parameters, just so you know). I already set the OneToMany and ManyToOne annotations for all my entities, but I need something to make a right/left join working.
If you want to help me with my entity design:
I have persons (table Person) who answers (table BogenAntworten) questions (table BogenFragen), and when I show the list of questions I either get the last answer from that question (need UPDATE when saving) or there is none and I have to create it (INSERT when saving). Questions also are in one of many types (table BogenTyp) and are in one of many groups (table BogenFragenGruppe)
Any Ideas?
OK, found it out myself again. The QueryBuilder of Doctrine2 supports a leftJoin (which is identical to the RIGHT JOIN if you switch the two tables). For those need some code, here is the above SQL statement build with QueryBuilder:
$query = $em->createQueryBuilder()
->select(array('f.id', 'f.frage', 'f.sortierung', 'a.antwort', 'g.name'))
->from('MySuperBundle:BogenFragen', 'f')
->leftJoin('f.bogenantworten', 'a', 'WITH', 'a.personen = :pid')
->from('MySuperBundle:BogenTyp', 't')
->from('MySuperBundle:BogenFragenGruppe', 'g')
->where('t.id = :tid')
->andWhere('t.id = f.bogentypen')
->andWhere('g.id = f.bogenfragengruppe')
->orderBy('f.sortierung', 'ASC')
->setParameter('tid', 1)
->setParameter('pid', 3)
->getQuery();
(The parameters are actually dynamic, but for easier reading I used the numbers of the original SQL statement)
So I've got a SQL query I'd like to duplicate in rails:
select g.*
from gamebox_favorites f
inner join gameboxes g on f.gamebox_id = g.id
group by f.gamebox_id
order by count(f.gamebox_id) desc;
I've been reading over the rails Active Record Query Interface site, but can't quite seem to put this together. I'd like the query to return a collection of Gamebox records, sorted by the number of 'favorites' a gamebox has. What is the cleanest way to do this in rails?
I believe this will work (works on a similarly structured database locally), though I'm not sure I have the proper models in the proper spots for what you're trying to do, so you might need to move a coule things around:
Gamebox.joins(:gamebox_favorites).
group('"gamebox_favorites"."gamebox_id"').
order('count("gamebox_favorites"."gamebox_id")')
On the console, this should compile to (in the case of PostgreSQL on the back end):
SELECT "gameboxes".* FROM "gamebox_favorites"
INNER JOIN "gamebox_favorites"
ON "gamebox_favorites"."gamebox_id" = "gamebox"."id"
GROUP BY "gamebox_favorites"."gamebox_id"
ORDER BY count("gamebox_favorites"."gamebox_id")
...and I'm guessing that you don't want do just wrap it in a find_by_sql call, such as:
Gamebox.find_by_sql("select g.* from gamebox_favorites f
inner join gameboxes g
on f.gamebox_id = g.id
group by f.gamebox_id
order by count(f.gamebox_id) desc")
First of all, please forgive me for my vocabulary is a little limited with NHibernate so I might call something the wrong thing...here is my question:
Result I am looking for is a count of distinct students for a course. I have three classes:
Courses, Students, CourseDates.
Courses contains a HasMany relationship with CourseDates.
CourseDates is a collection of dates on which each class has occurred and contains a HasAndBelongsToMany relationship with the Students class.
What I need to do is a get a distinct count of the Students from all the dates a course has occurred. Here is an example of a SQL statement that I want to replicate. The result is a number (long). This specific example produces the result: 5
SELECT COUNT(DISTINCT dbo.Literacy_Course_DatesStudents.IDStudent) AS StudentCount FROM
Literacy_Course_DatesStudents INNER JOIN Literacy_Course_Dates ON Literacy_Course_DatesStudents.IDDate = Literacy_Course_Dates.IDDate WHERE (Literacy_Course_Dates.IDCourse = 28)
Below is the query written from within a new class that I created specifically for this report...but I keep getting an error: Exception of type 'Antlr.Runtime.MissingTokenException' was thrown. Usually I thought this error was thrown when I didn't have CourseEnrolledCount class imported into the other classes but I have done that.
Dim q As Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)
q = New Castle.ActiveRecord.Queries.SimpleQuery(Of CourseEnrolledCount)(GetType(Literacy.Courses.CourseDates), "select new CourseEnrolledCount(Count(Distinct o.IDStudent in elements(t.Students) as o)) from CourseDates t Where t.Course.IDCourse = :courseid")
Let me know if I need to provide additional information. I hope I am being clear in my question. Thank you in advance for your time.
Here is a HQL query that does what you want:
select count(disctinct student.id) from Course course
inner join course.courseDates courseDate
inner join courseDate.students student
where course.id = :courseId
I'll let you substitute the real names of the associations.
You could even make it shorter (and more similar to your SQL query) by avoiding the join on the course:
select count(disctinct student.id) from CourseDate courseDate
inner join courseDate.students student
where courseDate.course.id = :courseId