I am new to persistence world 🙂 May be my question is very silly and may be my approach is incorrect but that is what I want to ask some expert. So my problem is about writting a query for an entity that is related to another entity.
@Entity
public class Team {
String teameName,
int teamId,
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "team_id", referencedColumnName = "teamId")
private Set<Player> players;
/**
more code for getters and setter for all fields
*/
}
@Entity
public class Player {
String PlayerName,
String address
/**
*more code for getters and setter for everything
*/
}
So multiple player can belong to one team but every player must have a unique Team which is done with making teamId as a foreign key to Player.
As far as I can think this should be correct.But now comes the problem.
Now I want to make a query to find all the players with “playerName=X” and “teamId =Y”
And I have no idea how to write such query.
Please can anyone suggest me something.
Thanks in advance.
Jeena
You want to read about HQL, the Hibernate Query Language. It’s kind of like SQL.
Basically you define you hql string, and then execute the query. Something like