0

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?

St.Antario
  • 26,175
  • 41
  • 130
  • 318
  • Show us your external_partner_player table. What exactly you want to fetch, PlayerAccount with all its associations or just PlayerAccount's properties? – Petar Butkovic Oct 31 '14 at 14:46

2 Answers2

1

You need to do SELECT DISTINCT, like this:

criteria.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY);
Lord
  • 576
  • 6
  • 8
1

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/

Sertage
  • 3,123
  • 1
  • 19
  • 26