• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JPA - A Simple Query with Relationships

 
Ping An
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

SELECT DISTINCT p
FROM Player p, IN(p.teams) t

Data retrieved: All players who belong to a team.

Description: The FROM clause declares two identification variables: p and t. The p variable represents the Player entity, and the t variable represents the related Team entity. The declaration for t references the previously declared p variable. The IN keyword signifies that teams is a collection of related entities. The p.teams expression navigates from a Player to its related Team. The period in the p.teams expression is the navigation operator.

Questions:
1. Please show me the structure & data examples for two tables Player and Team? and what the results?
2. why teams? not team?

Thank you in advance!

Reference: http://java.sun.com/javaee/5/docs/tutorial/doc/bnbtl.html
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
man of spider please check your private messages.
 
James Sutherland
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will basically get every Player back, except the ones that have no teams at all.

It is "teams" in the example because that is the field/property name of the Player class, JPQL always uses the attribute names.

I don't think you need to have the IN either, just "Player p join p.teams t" should also work.

See also,
http://en.wikibooks.org/wiki/Java_Persistence/JPQL_BNF
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic