Nama : ELIN NUGROHO NIM : 085610038 Jurusan : SI-S1 ORM MODUL 3

bawltherapistSoftware and s/w Development

Dec 13, 2013 (3 years and 4 months ago)

80 views

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3

Prakt
1

Membuat database

mysql> show tables;

+
------------------
+

| Tables_in_modul3 |

+
------------------
+

| lockers |

| student |

+
------------------
+

2 rows in set (0.00 sec)


mysql> select * lockers;

mysql> desc lockers;

+
---------
+
-------------
+
------
+
-----
+
---------
+
-------
+

| Field | Type | Null | Key | Default | Extra |

+
---------
+
-------------
+
------
+
-----
+
---------
+
-------
+

| id | bigint(20) | NO | PRI | | |

| name | varchar(10) | NO

| | | |

| student | bigint(20) | NO | | | |

+
---------
+
-------------
+
------
+
-----
+
---------
+
-------
+

3 rows in set (0.00 sec)


mysql> desc student;

+
---------
+
--------------
+
------
+
-----
+
---------
+
-------
+

| Field

| Type | Null | Key | Default | Extra |

+
---------
+
--------------
+
------
+
-----
+
---------
+
-------
+

| id | bigint(20) | NO | PRI | | |

| name | varchar(50) | NO | | | |

| address | varchar(150) | NO
| | | |

+
---------
+
--------------
+
------
+
-----
+
---------
+
-------
+

3 rows in set (0.00 sec)


Prakt 2

package modul3;


import java.io.Serializable;

import javax.persistence.Basic;

import javax.persistence.Column;

import javax.persistence.En
tity;

import javax.persistence.Id;

import javax.persistence.NamedQueries;

import javax.persistence.NamedQuery;

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3

import javax.persistence.Table;


@Entity

@Table(name = "student")

@NamedQueries({@NamedQuery(name = "Student.findAll", query = "SELECT s FROM




public Student() {


}



public Student(Long id) {


this.id = id;


}



public Student(Long id, String name, String address) {


this.id = id;


this.name = name;


this.address = address;


}



public Long getId() {


return id;


}



public void setId(Long id) {


this.id = id;


}



public String getName() {


return name;


}



public void setName(String name) {


this.name = name;


}



public S
tring getAddress() {


return address;


}

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3



public void setAddress(String address) {


this.address = address;


}



@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 Student)) {


return false;


}



Student other = (Student) 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 "modul3.Student[id=" + id + "]";


}


}


Prakt 3

package modul3;


import java.io.Serializable;

import javax.persistence.Basic;

import javax.persistence.Column;

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3

import javax.persistence.Entity;

import javax.persistence.Id;

import
javax.persistence.NamedQueries;

import javax.persistence.NamedQuery;

import javax.persistence.Table;


@Entity

@Table(name = "lockers")

@NamedQueries({@NamedQuery(name = "Lockers.findAll", query = "SELECT l FROM



public Lockers() {


}



public Lo
ckers(Long id) {


this.id = id;


}



public Lockers(Long id, String name, long student) {


this.id = id;


this.name = name;


this.student = student;


}



public Long getId() {


return id;


}



public void setId(Long id) {


this.id = id;


}



public String getName() {


return name;


}



public void setName(String name) {


this.name = name;


}

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3



public long getStudent() {


return student;


}




public void setStudent(long student) {


this.student = student;


}



@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 Lockers)) {


return false;


}


Lockers other = (Lockers) 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 "modul3.Lockers[id=" + id + "]";


}



void setStudent(Student std) {


throw new UnsupportedOperationException("Not yet implemented");


}


Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3

}

Prakt 4

<?xml version="1.0" encoding="UTF
-
8"?>

<!DOCTYPE hibernate
-
configuration PUBLIC "
-
//Hibernate/Hibernate Configuration DTD
3.0//EN" "http://hibernate.sourceforge.net/hibernate
-
configuration
-
3.0.dtd">

<hibernate
-
configuration>


<session
-
factory>


<property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>


<property
name="hibernate.connection.driver_
class">org.apache.derby.jdbc.ClientDriver</property>


<property name="hibernate.connection.url">jdbc:derby://localhost:1527/sample</property>


<property name="hibernate.connection.username">app</property>


<property name="hibernate.connection.pass
word">app</property>


</session
-
factory>

</hibernate
-
configuration>


Prakt 5

package modul3;


import java.util.*;

import org.hibernate.*;

import javax.persistence.*;


public class OneToOne {


public static void main(String[] args){


EntityManage
r em = null;


EntityManagerFactory emf = null;


try{


emf = Persistence.createEntityManagerFactory("onetoone", new HashMap());


em = emf.createEntityManager();


em.getTransaction();


Student std = new Student();


Lockers lkr = new Lockers();


std.setName("Zakky Ahmad Aditya");


std.setAddress("Griya Purwa Asri H
-
304");


lkr.setName("LKR
-

02");


lkr.setStudent(std);



em.persist(lkr);

Nama

:

ELIN
NUGROHO

NIM

:

085610038

Jurusan

:

SI
-
S1

ORM MODUL 3


em.getTransaction().commit();


em.getTransaction().begin();


Student std1 = new Student();


Lockers lkr1 = new Lockers();


std1.setName("Bambang PDP");


std1.setAddress("GPA H
-
304");


lkr.setName("LKR
-

01");


lkr.setStudent(std1);


em.persist(lkr1);


em.getTransaction().commit();




System.out.println("Done Inserted Data");





EntityManager newEm = emf.createEntityManager();


EntityTransaction newTx = newEm.getTransaction();


newTx.begin();




List lockers = newEm.createQuery("from locker").getResultList();



System.out.println( lockers.size() + " locker (s) found" );


for(Object 1 : lockers){


Lockers loadedLkr = (Lockers) l;


System.out.println(loadedLkr.getName() + " is use by " +
loadedLkr.getStudent().getName());


}




newTx.commit();


newEm.close();


}catch(Exception e){



System.out.println(e.getMessage());


}finally{


em.close();


emf.close();


}


}


}