Simple Hibernate/JPA menggunakan Eclipse Link
Sesuai janji saya, saya akan membuat tutorial hibernate
^_^ langsung saja ya :D
Pertama buat dulu kelas entity dari database
package entiti;
import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.UniqueConstraint;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author matt
*/
@Entity
@Table(catalog = "apotekku", schema =
"", uniqueConstraints = {
@UniqueConstraint(columnNames = {"hargajual"}),
@UniqueConstraint(columnNames = {"hargabeli"})})
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Dataobat.findAll", query = "SELECT
d FROM Dataobat d"),
@NamedQuery(name = "Dataobat.findByKodeobat", query =
"SELECT d FROM Dataobat d WHERE d.kodeobat = :kodeobat"),
@NamedQuery(name = "Dataobat.findByNamaobat", query =
"SELECT d FROM Dataobat d WHERE d.namaobat = :namaobat"),
@NamedQuery(name = "Dataobat.findByJenisObat", query =
"SELECT d FROM Dataobat d WHERE d.jenisObat = :jenisObat"),
@NamedQuery(name = "Dataobat.findByHargabeli", query =
"SELECT d FROM Dataobat d WHERE d.hargabeli = :hargabeli"),
@NamedQuery(name
= "Dataobat.findByHargajual", query = "SELECT d FROM Dataobat
d WHERE d.hargajual = :hargajual"),
@NamedQuery(name = "Dataobat.findByStok", query =
"SELECT d FROM Dataobat d WHERE d.stok = :stok"),
@NamedQuery(name = "Dataobat.deleteRecord",query =
"DELETE FROM Dataobat d WHERE d.namaobat = :namaobat")
})
public class Dataobat implements Serializable {
private static
final long serialVersionUID = 1L;
@Id
@Basic(optional
= false)
@Column(nullable = false, length = 5)
private String
kodeobat;
@Column(length
= 222)
private String
namaobat;
@Column(name =
"jenis_obat", length = 222)
private String
jenisObat;
@Column(length
= 222)
private String
hargabeli;
@Column(length
= 222)
private String
hargajual;
@Column(length
= 222)
private String
stok;
public
Dataobat() {
}
public
Dataobat(String kodeobat) {
this.kodeobat = kodeobat;
}
public String
getKodeobat() {
return
kodeobat;
}
public void
setKodeobat(String kodeobat) {
this.kodeobat = kodeobat;
}
public String
getNamaobat() {
return
namaobat;
}
public void
setNamaobat(String namaobat) {
this.namaobat = namaobat;
}
public String
getJenisObat() {
return
jenisObat;
}
public void
setJenisObat(String jenisObat) {
this.jenisObat = jenisObat;
}
public String
getHargabeli() {
return
hargabeli;
}
public void
setHargabeli(String hargabeli) {
this.hargabeli = hargabeli;
}
public String
getHargajual() {
return
hargajual;
}
public void
setHargajual(String hargajual) {
this.hargajual = hargajual;
}
public String
getStok() {
return
stok;
}
public void
setStok(String stok) {
this.stok =
stok;
}
@Override
public int
hashCode() {
int hash =
0;
hash +=
(kodeobat != null ? kodeobat.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 Dataobat)) {
return
false;
}
Dataobat other = (Dataobat) object;
if
((this.kodeobat == null && other.kodeobat != null) || (this.kodeobat
!= null && !this.kodeobat.equals(other.kodeobat))) {
return
false;
}
return
true;
}
@Override
public String toString() {
return
"entiti.Dataobat[ kodeobat=" + kodeobat + " ]";
}
}
|
Kalo sudah buat persistence.xml
<?xml version="1.0"
encoding="UTF-8"?>
<persistence version="2.0"
xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit
name="EclipseLinkDemoPU"
transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>entiti.Dataobat</class>
<properties>
<property
name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/apotekku"/>
<property
name="javax.persistence.jdbc.password" value="root"/>
<property
name="javax.persistence.jdbc.driver"
value="com.mysql.jdbc.Driver"/>
<property
name="javax.persistence.jdbc.user" value="root"/>
<property
name="eclipselink.ddl-generation"
value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
|
Lalu selanjutnya buat
control
/*
* To change this
template, choose Tools | Templates
* and open the
template in the editor.
*/
package caseJPA;
import entiti.Dataobat;
import java.util.Iterator;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;
import javax.persistence.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
/**
*
* @author matt
*/
public class control
{
public void insert(String hargabeli,String
hargajual,String jenisobat, String kodeobat , String namaobat , String stok )
{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EclipseLinkDemoPU");
EntityManager em = emf.createEntityManager();
try {
EntityTransaction entr = em.getTransaction();
entr.begin();
Dataobat stu = new Dataobat();
stu.setHargabeli(hargabeli);
stu.setHargajual(hargajual);
stu.setJenisObat(jenisobat);
stu.setKodeobat (kodeobat );
stu.setNamaobat (namaobat );
stu.setStok
(stok );
em.persist(stu);
entr.commit();
System.out.println("Successfully added into database");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
em.close();
}
}
public void selectAll()
{
EntityManagerFactory emf =
Persistence.createEntityManagerFactory("EclipseLinkDemoPU");
EntityManager em = emf.createEntityManager();
try {
EntityTransaction entr = em.getTransaction();
entr.begin();
Query query =
em.createNamedQuery("Dataobat.findAll");
List stList = query.getResultList();
Iterator stIterator = stList.iterator();
while (stIterator.hasNext()) {
Dataobat stu = (Dataobat) stIterator.next();
System.out.print("harga beli:" +
stu.getHargabeli());
System.out.print(" Hargajual :"+
stu.getHargajual());
System.out.print(" JenisObat :"
+stu.getJenisObat());
System.out.print(" Kodeobat :"+ stu.getKodeobat());
System.out.print(" Namaobat :"+ stu.getNamaobat());
System.out.print(" stok" + stu.getStok());
System.out.println();
}
entr.commit();
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {
em.close();
}
}
}
|
selanjutnya download aja projectnya di @ EclipseLink
Komentar
Posting Komentar