Home > Java > Hibernate One To Many Annotation Mapping

Hibernate One To Many Annotation Mapping

Berikut ini contoh asosiasi One To Many mapping.

1. Table
Table club

Table Club

Table Club

Table player

Table Player

Table Player

2. Entity Class

Player class

package pojo;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@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;

public int getIdPlayer() {
return idPlayer;
}

public void setIdPlayer(int idPlayer) {
this.idPlayer = idPlayer;
}

public String getPlayerName() {
return playerName;
}

public void setPlayerName(String playerName) {
this.playerName = playerName;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}

public Club getClub() {
return club;
}

public void setClub(Club club) {
this.club = club;
}

}

Club class

package pojo;

import java.io.Serializable;
import java.util.List;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@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;

@OneToMany(mappedBy="club")
private List<Player> player;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

public String getClubName() {
return clubName;
}

public void setClubName(String clubName) {
this.clubName = clubName;
}

public String getCountry() {
return country;
}

public void setCountry(String country) {
this.country = country;
}

public List<Player> getPlayer() {
return player;
}

public void setPlayer(List<Player> player) {
this.player = player;
}

}

3. Hibernate Configuration File

<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/jointrain?zeroDateTimeBehavior=convertToNull</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.show_sql">true </property>
<mapping/>
<mapping/>
</session-factory>
</hibernate-configuration>

4. Hibernate Utility Class

package onetomany;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

public class HibernateUtil {
private static final SessionFactory sessionFactory = buildSessionFactory();

private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
return new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
static SessionFactory getSessionFactory() {
return sessionFactory;
}
}

5. Main Class

package onetomany;

import java.util.ArrayList;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import pojo.Club;
import pojo.Player;

/**
*
* @author Kukuh Utama
*/
public class OneToMany {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
SessionFactory sf = HibernateUtil.getSessionFactory();
Session session = sf.openSession();
session.beginTransaction();

Club club = new Club();
club.setClubName("Inter Milan");
club.setCountry("Italy");

Player playerSatu = new Player();
playerSatu.setPlayerName("Kovacic");
playerSatu.setPosition("Midfield");
playerSatu.setCountry("Crotia");
playerSatu.setClub(club);

Player playerDua = new Player();
playerDua.setPlayerName("Rolando");
playerDua.setPosition("Back");
playerDua.setCountry("Portugal");
playerDua.setClub(club);

club.setPlayer(new ArrayList<Player>());
club.getPlayer().add(playerDua);
club.getPlayer().add(playerSatu);

session.save(club);
session.save(playerDua);
session.save(playerSatu);

session.getTransaction().commit();
session.close();
}
}

6. Project Structure

Project Structure

Project Structure

 

Semoga bermanfaat.

Referensi : dari berbagai sumber.

  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: