Reputation: 27455
I'm using hibernate-core:3.3.1.GA
I have three mappings:
class PlayerAccount
:
@Entity
@Table(name = "player_account")
public class PlayerAccount {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne(targetEntity = Player.class, fetch = FetchType.EAGER)
@JoinColumn(name="player_id")
private Player player;
//GET, SET
}
class PlayerAchievements
:
@Entity
@Table(name="player_achievement")
public class PlayerAchievement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@ManyToOne(targetEntity = Player.class, fetch = FetchType.EAGER)
@JoinColumn(name = "player_id")
private Player player;
//GET, SET
}
and class Player
@Entity
@Table(name = "player")
@Inheritance(strategy = InheritanceType.JOINED)
public class Player {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;
@Column(name = "ps_id")
private String psId;
@Column(name = "login")
private String login;
@Column(name = "password")
private String password;
@Column(name = "email")
private String email;
//GET, SET
}
Issue: When I try to write a simple criteria query like the following:
Criteria criteria = getSession().createCriteria(PlayerAccount.class);
criteria.add(Restrictions.eq("playerId", playerId));
criteria.list();
The resulting sql-query contains joins
have the following view:
SELECT
-- columns to select
FROM player_account this_
LEFT OUTER JOIN player player2_
ON this_.player_id=player2_.id
LEFT OUTER JOIN external_partner_player player2_1_
ON player2_.id=player2_1_.player_id
LEFT OUTER JOIN player_achievement achievemen3_
ON player2_.id=achievemen3_.player_id
WHERE player_id = 2362189
The thing is player_achievements may contain more than one row with the same player_id
value. Since we probably have a duplicated result in that criteria query. How to fix that using hibernate whithout the writing an sql
-query?
Upvotes: 0
Views: 53
Reputation: 3263
Because the Fetch Types are eager. That means that you always want the entities loaded when you load the main entity. You can use FetchType.LAZY for best perform. Here explains better: https://howtoprogramwithjava.com/hibernate-eager-vs-lazy-fetch-type/
Upvotes: 1
Reputation: 576
You need to do SELECT DISTINCT, like this:
criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Upvotes: 1