Forums Register Login

Querying tables with a one-to-many foreign key relationship

+Pie Number of slices to send: Send
Hi everyone,

I am having trouble querying a one to many foreign key relationship. For example one product can have many parts. I would like to retrieve the part name, part number etc relating to a product id. The Product table stores a Set of Parts and the Part table stores the Product object.

Code:


Error:
18:44:50,801 INFO TransactionFactoryFactory:34 - Transaction strategy: org.hibernate.transaction.JDBCTransactionFactory
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: unexpected AST node: ( near line 1, column 101 [from hibernate.Product p, hibernate.Part a where a.getProduct().getId()=p.id]

If you can help please post a response I have tried many different variations including inner join etc.

Thanks

Alyssa
+Pie Number of slices to send: Send
Or could you point me towards a good reference?

Cheers Alyssa
+Pie Number of slices to send: Send
Hi,

I think You query should be



Since there is already an association between Part and product on the basis on id, you dont need to put it in the where clause.
just "a.product p"should generate the corresponding sql for you.
+Pie Number of slices to send: Send
Thanks for the response. I really appreciate your help. I'm really not sure how to fix this.

I tried the following, it still has an error. It says Product is not mapped, I assumed the following portion of the parts mapping file would define this.





I eventually with this query want to return the product name as well.

Below are the mapping files:

Product.hbm.xml



Part.hbm.xml


Cheers Alyssa
+Pie Number of slices to send: Send
Is there a work around for this?
I will open the floodgates of his own worst nightmare! All in a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1413 times.
Similar Threads
Hibernate - Using Join table - one-to-many association - Lazy Exception
Simple mapping issue for Hibernate newbie
Problem in hibernate join result
HQL Query
help regarding EJB-QL
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 20:33:48.