What would the HQL for this look like?
If Audit only had one reference to AuditType then I could simply do
something like this:
"Select audit from Audit audit where audit.auditType.Id in
(1,2,3,4,etc)"
I have tried this but it fails with a "Path expected for join"
exception:
"Select audit From Audit audit
join audit_auditTypes on audit_auditTypes.auditId = audit.Id
where 0 < (select count(*) from AuditType auditType where auditType.Id
= audit_auditTypes.auditTypeId and auditType.Id IN (:auditTypeIds))"
Here are the mappings:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Entity"
namespace="ClaimAudit.Entity">
<class name="ClaimAudit.Entity.Audit" lazy="false" table="audits">
<id name="Id">
<generator class="native"/>
</id>
<property name="Active" type="Boolean"/>
<property name="DateAudited"/>
<bag name="AuditTypes" table="audit_auditTypes" lazy="false">
<key column="auditId"/>
<many-to-many column="auditTypeId" class="AuditType"/>
</bag>
</class>
</hibernate-mapping>
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
assembly="Entity"
namespace="ClaimAudit.Entity">
<class name="ClaimAudit.Entity.AuditType" lazy="false"
table="audittypes">
<id name="Id">
<generator class="native"/>
</id>
<property name="Active" type="Boolean"/>
<property name="Name"/>
<property name="Description"/>
</class>
</hibernate-mapping>
SELECT au FROM Audit au JOIN au.AuditTypes at
WHERE at.id IN (:ids)
I think
What I really need is HQL to create SQL that looks like the below.
Any more suggestions?
select *
from audits
join audit_audittypes on audit_audittypes.auditId = audits.id
join auditTypes as at on at.id = audit_audittypes.auditTypeId
where at.id IN (1,2,3,4,5,6,7)
Using your mappings (except I use a GUID.COMB generator instead of
Native) My SQL equivalent is:
select audit0_.Id as Id0_, audit0_.Active as Active0_,
audit0_.DateAudited as DateAudi3_0_
from audits audit0_ inner join audit_auditTypes audittypes1_
on audit0_.Id=audittypes1_.auditId
inner join audittypes audittype2_ on
audittypes1_.auditTypeId=audittype2_.Id
where audittype2_.Id in (@p0)
I'm using NH 2.1.2. I seem to recall that NH 1.2 produces different
SQL similar to yours but it still produces the right result as it
JOINs on the WHERE clause instead of via INNER JOINS (don't quote me
though - we still use NH 1.2 and I'm pretty sure the above works but I
haven't looked at it for a while)
The above SQL might well produce duplicates but usually I just add a
DISTINCT clause to the HQL
SELECT DISTINCT au FROM Audit au JOIN au.AuditTypes at WHERE at.id IN
(:ids)
which produces
select distinct audit0_.Id as Id0_, audit0_.Active as Active0_,
audit0_.DateAudited as DateAudi3_0_
from audits audit0_ inner join audit_auditTypes audittypes1_ on
audit0_.Id=audittypes1_.auditId
inner join audittypes audittype2_ on
audittypes1_.auditTypeId=audittype2_.Id
where audittype2_.Id in (@p0)
--
You received this message because you are subscribed to the Google Groups "nhusers" group.
To post to this group, send email to nhu...@googlegroups.com.
To unsubscribe from this group, send email to nhusers+u...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nhusers?hl=en.
So it seems that join does not work for many-to-many. Is that
correct?
> > "nhusers" group.> To post to this group, send email tonh...@googlegroups.com.
> > To unsubscribe from this group, send email to>nhusers+u...@googlegroups.com<nhusers%2Bunsu...@googlegroups.com>
So it seems that join does not work for many-to-many. Is that
correct?
This was the resulting SQL (the in between table WAS referenced, but
the where clause was DROPPED!):
select audit0_.Id as col_0_0_
from audits audit0_ inner join audit_auditTypes audittypes1_ on
audit0_.Id=audittypes1_.auditId
inner join audittypes audittype2_ on
audittypes1_.auditTypeId=audittype2_.Id
As far as I'm concerned, the HQL I provided works fine with your
mappings as I've listed the SQL that is generated. Are you doing
anything wierd? (old version of NH, using NH Fluent or something?)
HQL:
select distinct audit
from Audit audit join audit.auditTypes at
where at.Id IN (:auditTypeIds)
Generated SQL:
select distinct audit0_.Id as col_0_0_
from audits audit0_
inner join audit_auditTypes audittypes1_ on
audit0_.Id=audittypes1_.auditId
inner join audittypes audittype2_ on
audittypes1_.auditTypeId=audittype2_.Id
where audittype2_.Id in (1,2,3,4,5,6,7);