@OneToMany dan @ManyToOne dalam hubungannya dengan script database

Apa hubungannya anotasi @OneToMany dan @ManyToOne dengan script database ?

q> Ketika saya membuat entity menggunakan @OneToMany dan @ManyToOne antara tabel MstStok <-> MstBarang,
q> dan MstStok <-> MstRak

q> Ketika menggunakan dua anotasi tersebut script database yang terbentuk adalah ::
q> Tabel MST_STOK

DROP TABLE IF EXISTS `mst_stok`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mst_stok` (
`ID_BARANG` varchar(6) NOT NULL,
`ID_RAK` varchar(6) NOT NULL,
`qty` int(11) NOT NULL,
PRIMARY KEY (`ID_BARANG`,`ID_RAK`),
KEY `FKFA18822EC3D9BE36` (`ID_BARANG`),
KEY `FKFA18822E86378CD0` (`ID_RAK`),
CONSTRAINT `FKFA18822E86378CD0` FOREIGN KEY (`ID_RAK`) REFERENCES `mst_rak` (`ID_RAK`),
CONSTRAINT `FKFA18822EC3D9BE36` FOREIGN KEY (`ID_BARANG`) REFERENCES `mst_barang` (`ID_BARANG`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

q> Tabel Mst_Barang

DROP TABLE IF EXISTS `mst_barang`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mst_barang` (
`ID_BARANG` varchar(255) NOT NULL,
`HARGA` int(11) DEFAULT NULL,
`NAMA_BARANG` varchar(255) DEFAULT NULL,
PRIMARY KEY (`ID_BARANG`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

q> Tabel MST_RAK

DROP TABLE IF EXISTS `mst_rak`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `mst_rak` (
`ID_RAK` varchar(6) NOT NULL,
`NAMA_RAK` varchar(20) NOT NULL,
PRIMARY KEY (`ID_RAK`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
/*!40101 SET character_set_client = @saved_cs_client */;

q> Sedangkan Class Entity untuk MstStok, MstBarang dan MstRak adalah sebagai berikut :
q> MstStok ::

package com.saifiahmada.model;

import java.io.Serializable;

import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

/** @author Saifi Ahmada Jan 18, 2013 3:17:42 PM  **/

@Entity
@Table(name="MST_STOK")
public class MstStok implements Serializable{

private static final long serialVersionUID = 1L;

@EmbeddedId
private MstStokPK mstStokPK;

private int qty;

@ManyToOne
@JoinColumn(name="ID_BARANG", referencedColumnName="ID_BARANG", insertable=false, updatable=false)
private MstBarang mstBarang;

@ManyToOne
@JoinColumn(name="ID_RAK", referencedColumnName="ID_RAK", insertable=false, updatable=false)
private MstRak mstRak;

public MstStok() {

}

public MstStok(String idBarang, String idRak) {
this.mstStokPK = new MstStokPK(idBarang, idRak);
}

public MstRak getMstRak() {
return mstRak;
}

public void setMstRak(MstRak mstRak) {
this.mstRak = mstRak;
}

public MstBarang getMstBarang() {
return mstBarang;
}

public void setMstBarang(MstBarang mstBarang) {
this.mstBarang = mstBarang;
}

public MstStokPK getMstStokPK() {
return mstStokPK;
}

public void setMstStokPK(MstStokPK mstStokPK) {
this.mstStokPK = mstStokPK;
}

public int getQty() {
return qty;
}

public void setQty(int qty) {
this.qty = qty;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((mstStokPK == null) ? 0 : mstStokPK.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MstStok other = (MstStok) obj;
if (mstStokPK == null) {
if (other.mstStokPK != null)
return false;
} else if (!mstStokPK.equals(other.mstStokPK))
return false;
return true;
}

@Override
public String toString() {
return "MstStok [mstStokPK=" + mstStokPK + "]";
}

}

q> MstBarang ::

package com.saifiahmada.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** @author Saifi Ahmada Jan 15, 2013 10:11:35 PM  **/

@Entity
@Table(name="MST_BARANG")
public class MstBarang implements Serializable {

private static final long serialVersionUID = 1L;

@EmbeddedId
private MstBarangPK mstBarangPK;
@Column(name="NAMA_BARANG")
private String namaBarang;
@Column(name="HARGA")
private Integer harga;

@OneToMany(mappedBy="mstBarang")
private Set<DtlPenjualan> dtlPenjualans;

@OneToMany(mappedBy="mstBarang")
private Set<MstStok> mstStoks;

public MstBarang() {

}

public MstBarang(String idBarang) {
this.mstBarangPK = new MstBarangPK(idBarang);
}

public Set<MstStok> getMstStoks() {
return mstStoks;
}

public void setMstStoks(Set<MstStok> mstStoks) {
this.mstStoks = mstStoks;
}

public Set<DtlPenjualan> getDtlPenjualans() {
return dtlPenjualans;
}

public void setDtlPenjualans(Set<DtlPenjualan> dtlPenjualans) {
this.dtlPenjualans = dtlPenjualans;
}

public MstBarangPK getMstBarangPK() {
return mstBarangPK;
}

public void setMstBarangPK(MstBarangPK mstBarangPK) {
this.mstBarangPK = mstBarangPK;
}

public String getNamaBarang() {
return namaBarang;
}

public void setNamaBarang(String namaBarang) {
this.namaBarang = namaBarang;
}

public Integer getHarga() {
return harga;
}

public void setHarga(Integer harga) {
this.harga = harga;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((mstBarangPK == null) ? 0 : mstBarangPK.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MstBarang other = (MstBarang) obj;
if (mstBarangPK == null) {
if (other.mstBarangPK != null)
return false;
} else if (!mstBarangPK.equals(other.mstBarangPK))
return false;
return true;
}

@Override
public String toString() {
return "MstBarang [mstBarangPK=" + mstBarangPK + "]";
}

}

q> MstRak ::

package com.saifiahmada.model;

import java.io.Serializable;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/** @author Saifi Ahmada Jan 18, 2013 3:27:42 PM  **/

@Entity
@Table(name="MST_RAK")
public class MstRak implements Serializable {

private static final long serialVersionUID = 1L;

@EmbeddedId
private MstRakPK mstRakPK;
@Column(name="NAMA_RAK", nullable=false, length=20)
private String namaRak;

@OneToMany(mappedBy="mstRak")
private Set<MstStok> mstStoks;

public MstRak() {

}

public MstRak(String idRak) {
this.mstRakPK = new MstRakPK(idRak);
}

public Set<MstStok> getMstStoks() {
return mstStoks;
}

public void setMstStoks(Set<MstStok> mstStoks) {
this.mstStoks = mstStoks;
}

public MstRakPK getMstRakPK() {
return mstRakPK;
}

public void setMstRakPK(MstRakPK mstRakPK) {
this.mstRakPK = mstRakPK;
}

public String getNamaRak() {
return namaRak;
}

public void setNamaRak(String namaRak) {
this.namaRak = namaRak;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((mstRakPK == null) ? 0 : mstRakPK.hashCode());
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
MstRak other = (MstRak) obj;
if (mstRakPK == null) {
if (other.mstRakPK != null)
return false;
} else if (!mstRakPK.equals(other.mstRakPK))
return false;
return true;
}

@Override
public String toString() {
return "MstRak [mstRakPK=" + mstRakPK + "]";
}

}

q> Sedangkan ketika annotaion @OneToMany dan @ManyToOne pada class entity MstStok <-> MstBarang dan MstStok <-> MstRak DIHILANGKAN maka pada script database yang digenerate oleh hibernate tidak ada constraint pada MstStok,
q> Tidak adanya contraint MstBarang untuk idBarang(ID_BARANG) untuk MstStok.
q> Tidak adanya contraint MstRak untuk idRAk(ID_RAK) untuk MstStok.

q> Selesai😀

Published by

saifiahmada

Begin a big Process with a bit trying

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