mengenal annotaion temporal pada hibernate annotation

Belajar hibernate yuk”

q> @Temporal digunakan untuk tipe data Date (java.util) untuk meyinmpan informasi tanggal dan atau waktu ( datetime).
q> @Temporal mempunyai 3 tipe yaitu DATE, TIMESTAMP dan TIME
q>@Temporal(TemporalType.DATE) untuk menyimpan tanggal saja

q>@Temporal(TemporalType.TIMESTAMP) untuk menyimpan tanggal dan waktu
q>@Temporal(TemporalType.TIME) untuk menyimpan  waktu

q> Membuat bean siswa

package com.ahmada;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
public class Siswa implements Serializable {

/**
*
*/
private static final long serialVersionUID = 1L;

@Id
private String nis;

@Column(name = "nama", length = 30, nullable = false, unique = true)
private String nama;

@Column(name = "alamat", length = 30)
private String alamat;

@Column(name="tgl_lahir")
@Temporal(TemporalType.DATE)
private Date tglLahir;

public Siswa() {

}

public Siswa(String nis) {
this.nis = nis;
}

public String getNis() {
return nis;
}

public void setNis(String nis) {
this.nis = nis;
}

public String getNama() {
return nama;
}

public void setNama(String nama) {
this.nama = nama;
}

public String getAlamat() {
return alamat;
}

public void setAlamat(String alamat) {
this.alamat = alamat;
}

public Date getTglLahir() {
return tglLahir;
}

public void setTglLahir(Date tglLahir) {
this.tglLahir = tglLahir;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((nis == null) ? 0 : nis.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;
Siswa other = (Siswa) obj;
if (nis == null) {
if (other.nis != null)
return false;
} else if (!nis.equals(other.nis))
return false;
return true;
}

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

}

q> Ngetes bean Siswa

package com.ahmada.test;

import java.util.Date;

import org.hibernate.Session;

import com.ahmada.Siswa;
import com.mkyong.util.HibernateUtil;

public class TestSiswa {

public static void main (String [] args){
System.out.println("Mulai tes siswa");
Session ses = HibernateUtil.getSessionFactory().openSession();

ses.beginTransaction();

Siswa siswa = new Siswa("0034");

siswa.setAlamat("Daha Selatan");
siswa.setNama("Ahmada");
siswa.setTglLahir(new Date());

ses.save(siswa);

ses.getTransaction().commit();
System.out.println("Commit sukses");
}

}

q> Hasil untuk @TemporalType.DATE :: 2013-01-09

q> Hasil untuk @TemporalType.TIMESTAMP :: 2013-01-09 15:11:51.0

q> Hasil untuk @TemporalType.TIME :: 15:13:50

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