FetchMode Join di Hibernate

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

FetchMode Join di Hibernate

Ren-14
Dear rekan,
Saat ini saya lagi belajar fetching strategy di hibernate. Saya bingung
untuk bagian fetching denga FetchMode.JOIN. Berikut ini saya sertakan
sebagian code yang digunakan ( menggunakan OneToMany asosiasi )

Club Entity

@Entity
@Table(name="club")
public class Club implements Serializable {

    @Id
    @GeneratedValue
    @Column(name="id")
    private int id;

    @Column(name="club_name")
    private String clubName;
    @Column(name="country")
    private String country;

    @Fetch(FetchMode.JOIN)
    @OneToMany(mappedBy="club")
    private List<Player> player;

//getter setter method
}

Player Entity.

@Entity
@Table(name="player")
public class Player {
    @Id
    @GeneratedValue
    @Column(name="id_player")
    private int idPlayer;
    @Column(name="player_name")
    private String playerName;
    @Column(name="country")
    private String country;
    @Column(name="position")
    private String position;

    @ManyToOne
    @JoinColumn(name="fk_id_club", referencedColumnName="id")
    private Club club;

//getter setter method

}

Main class untuk retrieve record entity Player dan Club.

public class OneToMany {
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Query query = session.createQuery(“from Club”);
List listClub = query.list();

Iterator iterator = listClub.iterator();
while (iterator.hasNext()){
Club club = (Club) iterator.next();
System.out.println(“Club Name:”+ club.getClubName());
System.out.println(“Country: “+ club.getCountry());
Iterator pIterator = club.getPlayer().iterator();
System.out.println(“*****”);
while(pIterator.hasNext()){
Player player = (Player)pIterator.next();
System.out.println(“Player Name: “+player.getPlayerName());
System.out.println(“Player Position: “+ player.getPosition());
System.out.println(“Player Country: “+player.getCountry());
}
System.out.println(“—–”);
}
session.getTransaction().commit();
session.close();
}
}

Saat run program, Hibernate menghasilkan 5 query sebagai berikut ;

Hibernate: select club0_.id as id0_, club0_.club_name as club2_0_,
club0_.country as country0_ from club club0_
Hibernate: select player0_.fk_id_club as fk5_1_, player0_.id_player as
id1_1_, player0_.id_player as id1_1_0_, player0_.fk_id_club as fk5_1_0_,
player0_.country as country1_0_, player0_.player_name as player3_1_0_,
player0_.position as position1_0_ from player player0_ where
player0_.fk_id_club=?
Hibernate: select player0_.fk_id_club as fk5_1_, player0_.id_player as
id1_1_, player0_.id_player as id1_1_0_, player0_.fk_id_club as fk5_1_0_,
player0_.country as country1_0_, player0_.player_name as player3_1_0_,
player0_.position as position1_0_ from player player0_ where
player0_.fk_id_club=?
Hibernate: select player0_.fk_id_club as fk5_1_, player0_.id_player as
id1_1_, player0_.id_player as id1_1_0_, player0_.fk_id_club as fk5_1_0_,
player0_.country as country1_0_, player0_.player_name as player3_1_0_,
player0_.position as position1_0_ from player player0_ where
player0_.fk_id_club=?
Hibernate: select player0_.fk_id_club as fk5_1_, player0_.id_player as
id1_1_, player0_.id_player as id1_1_0_, player0_.fk_id_club as fk5_1_0_,
player0_.country as country1_0_, player0_.player_name as player3_1_0_,
player0_.position as position1_0_ from player player0_ where
player0_.fk_id_club=?


Setelah saya liat query yang dihasilkan tidak seperti outer join, malah
lebih mirip query yang dihasilkan FetchMode.SELECT. di doc-nya
FetchMode.JOIN itu menggunakan outer join dan menurut saya hibernate
seharusnya hanya menghasilkan 1 query saja. Pertanyaannya kenapa bisa
begitu ya? Apa code saya salah?

Terima Kasih

     Regards,
 Kukuh Utama

 Mobile: +62 9656587199