I can't make a inner join between two tables in hibernate hql query

南笙酒味 提交于 2020-01-02 16:19:54

问题


I am new with this. Please help me.

My inner join looks like this: select p.idprodus, p.denumire, p.cantitate from Produs p inner join Furnizor f on p.idfurn = f.idfurn

I want to make the inner join on the column idfurn, but I get these errors:

org.hibernate.QueryException: outer or full join must be followed by path expression select p.idprodus, p.denumire, p.cantitate from sakila.entity.Produs p inner join Furnizor f on p.idfurn = f.idfurn

at org.hibernate.hql.classic.FromParser.token(FromParser.java:170)
at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86)
at org.hibernate.hql.classic.PreprocessingParser.token(PreprocessingParser.java:108)
at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:216)
at org.hibernate.hql.classic.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:185)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:72)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1623)

回答1:


In HQL, you use entities, not tables. And entities are linked together by associations (OneToOne, OneToMany, etc.) Joins can anly be done between associated entities.

For example, if you have a ManyToOne association between Product and Provider, the HQL query is:

select p from Product p inner join p.provider provider where ...

The on clause is unnecessary, because Hibernate knows from the mapping of the ManyToOne association that a Product is associated with its provider using the product.id_provider foreign key to the provider.id_provider primary key.

All of this is very well explained, with lots of examples, in the Hibernate documentation.




回答2:


If an association (e.g. OneToMany mapping) does not exist and you need an inner join then use the old cross join notation.



来源:https://stackoverflow.com/questions/10216426/i-cant-make-a-inner-join-between-two-tables-in-hibernate-hql-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!