@ManyToOne
@JoinColumn(name = "enterprise_id")
private ServiceProvider enterprise;
QServiceProvider serviceProvider2 = new QServiceProvider("serviceProvider2");
QServiceProvider serviceProvider3 = new QServiceProvider("serviceProvider3");
query.from(serviceProvider2)
.innerJoin(serviceProvider3)
.on(serviceProvider2.enterprise.id.eq(serviceProvider3.id))
.where(serviceProvider3.id.eq(enterpriseId))
.list(serviceProvider2.id);
This is what hibernate generates :
select sp1.id
from service_providers sp0
cross join service_providers sp1
inner join service_providers sp2 on (sp1.enterprise_id=sp2.id)
This is a little bit confusing, besides being wrong.
Can anyone tell me what I am doing wrong here?
Thx.
query.from(serviceProvider2)
.innerJoin(serviceProvider2.enterprise, serviceProvider3) // so service3 will be alias for the path serviceProvider2.enterprise
// .on(serviceProvider2.enterprise.id.eq(serviceProvider3.id)) // not necessary, JPA 2.1 will imply it
.where(serviceProvider3.id.eq(enterpriseId))
.list(serviceProvider2.id);
@JoinColumn(name = "enterprise_id")
private ServiceProvider enterprise;
@Column(name = "enterprise_id", updatable = false, insertable = false)
private Integer enterpriseId; // or Long or whatever is needed
Now notice that one of these MUST be updatable/insertable false - it will be filled in when read, but only one of those attributes is relevant for writing, otherwise it would be mess. Now when we have the raw value mapped, we can try something like:
query.from(serviceProvider2)
.innerJoin(serviceProvider3).on(serviceProvider2.enterpriseId.eq(serviceProvider3.id))
.where(serviceProvider3.id.eq(enterpriseId))
.list(serviceProvider2.id);
query.from(serviceProvider2)
.where(serviceProvider2.enterpriseId.eq(enterpriseId)).list(serviceProvider2.id);
query.from(serviceProvider2)
.where(serviceProvider2.enterprise.id.eq(enterpriseId))
.list(serviceProvider2.id);
The second one may result in unnecessary join, but I think Hibernate optimizes this case (I'm lazy to try now, sorry ;-)). JPA in general does not guarantee it will not throw some silly join in.
Hey Richard,
Yes... To everything I guess :-) you are right about all your examples. I wanted to put the enterprise_id in the entity too, but did not like it having there too much. I guess updatable and insertable false with proper documentation makes it very clear. I like this advice a lot btw ;).
Having the dual mapping makes things so much nicer, while without it hibernate will indeed create the cross join and the results will not even be correct.
Again, thx for your time.
Cheers,
Eugene.
--
You received this message because you are subscribed to a topic in the Google Groups "Querydsl" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/querydsl/DmM5MbO0RKA/unsubscribe.
To unsubscribe from this group and all its topics, send an email to querydsl+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.