Skip navigation


*Maaf baru posting setelah sekian lama*

Sekarang saya akan mencoba membuat kelas Entity Persistence pada project yang pernah diposting sebelum ini. Pada proyek itu telah dibuat Persistence Unitnya.

Untuk membuat kelas Entity, cukup klik kanan pada package tempat kelas ingin ditaruh. Pilih “Entity Classes From Database

1

Lalu pilih databasenya dan pilih tabel-tablenya.

2

Setelah itu ketik Finish meski sebenarnya perlu ada beberapa konfigurasi tambahan.

3

Dapat dilihat pada Persistence Unit, kelas yang ditambahkan sudah ditambahkan  secara otomatis ke Persistence Unit tersebut.

4

Di akhir postingan ini adalah kode Entity yang sudah dibangkitkan. Lihat juga bahwa pada kelas Examination, properti untuk dokter dapat diambil berupa kelas Dokter. Hal ini dikarenakan adanya Foreign Key pada tabel Examinations yang mengacu pada
dokter. Begitupula dengan properti Patient.

Postingan selanjutnya akan membicarakan tentang bagaimana manipulasi obyek kelas-kelas entitas. (semoga bisa ditulis secepatnya, hehehe)

/*
 * File: Doctor.java
 */

package medik.models;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Petra Barus
 */
@Entity
@Table(name = "doctors")
@NamedQueries({@NamedQuery(name = "Doctor.findAll", query = "SELECT d FROM Doctor d"), @NamedQuery(name = "Doctor.findById", query = "SELECT d FROM Doctor d WHERE d.id = :id")})
public class Doctor implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Lob
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "doctorId")
    private Collection<Examination> examinationCollection;

    public Doctor() {
    }

    public Doctor(Integer id) {
        this.id = id;
    }

    public Doctor(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<Examination> getExaminationCollection() {
        return examinationCollection;
    }

    public void setExaminationCollection(Collection<Examination> examinationCollection) {
        this.examinationCollection = examinationCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Doctor)) {
            return false;
        }
        Doctor other = (Doctor) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "medik.models.Doctor[id=" + id + "]";
    }

}
/*
 * File: Examination.java
 */

package medik.models;

import java.io.Serializable;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 *
 * @author Petra Barus
 */
@Entity
@Table(name = "examinations")
@NamedQueries({@NamedQuery(name = "Examination.findAll", query = "SELECT e FROM Examination e"), @NamedQuery(name = "Examination.findById", query = "SELECT e FROM Examination e WHERE e.id = :id"), @NamedQuery(name = "Examination.findByDate", query = "SELECT e FROM Examination e WHERE e.date = :date")})
public class Examination implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    @JoinColumn(name = "patient_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Patient patientId;
    @JoinColumn(name = "doctor_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Doctor doctorId;

    public Examination() {
    }

    public Examination(Integer id) {
        this.id = id;
    }

    public Examination(Integer id, Date date) {
        this.id = id;
        this.date = date;
    }

    public Integer getId() {
        return id;
    }

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

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public Patient getPatientId() {
        return patientId;
    }

    public void setPatientId(Patient patientId) {
        this.patientId = patientId;
    }

    public Doctor getDoctorId() {
        return doctorId;
    }

    public void setDoctorId(Doctor doctorId) {
        this.doctorId = doctorId;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Examination)) {
            return false;
        }
        Examination other = (Examination) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "medik.models.Examination[id=" + id + "]";
    }

}
/*
 * Patient.java
 */

package medik.models;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 * @author Petra Barus
 */
@Entity
@Table(name = "patients")
@NamedQueries({@NamedQuery(name = "Patient.findAll", query = "SELECT p FROM Patient p"), @NamedQuery(name = "Patient.findById", query = "SELECT p FROM Patient p WHERE p.id = :id")})
public class Patient implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "id")
    private Integer id;
    @Basic(optional = false)
    @Lob
    @Column(name = "name")
    private String name;
    @OneToMany(cascade = CascadeType.ALL, mappedBy = "patientId")
    private Collection<Examination> examinationCollection;

    public Patient() {
    }

    public Patient(Integer id) {
        this.id = id;
    }

    public Patient(Integer id, String name) {
        this.id = id;
        this.name = name;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Collection<Examination> getExaminationCollection() {
        return examinationCollection;
    }

    public void setExaminationCollection(Collection<Examination> examinationCollection) {
        this.examinationCollection = examinationCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Patient)) {
            return false;
        }
        Patient other = (Patient) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "medik.models.Patient[id=" + id + "]";
    }

}

One Comment

  1. woow..
    thx infonya..
    saya mahasiswa politeknik telkom semester akhir yg sedang menyelesaikan project akhir ttg sistem rawat inap di sebuah rumah askit..
    dosen pembimbing saya menginginkan aplikasi saya menggunakan entity class..
    saya rada bingung ttg entity class…
    mohon bimbingannya..😀


Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s

%d blogger menyukai ini: