2

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)
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
alin
  • 145
  • 1
  • 2
  • 11

2 Answers2

3

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • I don't understand why you wrote p.provider in the Hql query, why you wrote that p? Sorry, but I am a beginner and I have small problems in understanding this. – alin Apr 19 '12 at 20:38
  • For example I want to print all the columns from the entity Product and when I try with the query from Product, the columns that are foreign keys in this entity don't show the information, they contain. It appears sakila.entity.HistoryProduct sakila.entity.Provider sakila.entity.Category instead of the values. Please give me some suggestions. Thank you a lot! – alin Apr 19 '12 at 21:03
  • I don't have your code, and I have no magic cristal ball, so it's impossible for me to comment on it. But I think you just missed a huge part of Hibernate: associations between entities. Read the documentation. I wrote p.provider because a product has a provider. In Java, I would call `p.getProvider()`. In HQL, I write p.provider. – JB Nizet Apr 21 '12 at 06:27
1

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

Community
  • 1
  • 1
Sarah Phillips
  • 923
  • 11
  • 30