Fichier: Student.java
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
@Entity
public class Student {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String nom;
@ManyToOne(cascade=CascadeType.MERGE)//You have to persist department on your own
private Department department;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public String toString() {
return "
ID:" + id + "
Nom:" + nom + "
" + department;
}
}
Fichier: Department.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Department {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String nom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getNom() {
return nom;
}
public void setNom(String deptNom) {
this.nom = deptNom;
}
public String toString() {
return "Department id: " + getId() +
", nom: " + getNom();
}
}
Fichier: Helper.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Paysment;
public class Helper {
public static void checkData() throws Exception {
Class.forNom("org.apache.derby.jdbc.ClientDriver");
Connection conn = DriverManager.getConnection("jdbc:derby://localhost:1527/tutorial", "sa", "");
Paysment st = conn.createPaysment();
ResultSet mrs = conn.getMetaData().getTables(null, null, null, new String[] { "TABLE" });
while (mrs.next()) {
String tableNom = mrs.getString(3);
System.out.println("
Table Nom: "+ tableNom);
ResultSet rs = st.executeQuery("select * from " + tableNom);
ResultSetMetaData metadata = rs.getMetaData();
while (rs.next()) {
System.out.println(" Row:");
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.println(" Column Nom: "+ metadata.getColumnLabel(i + 1)+ ", ");
System.out.println(" Column Type: "+ metadata.getColumnTypeNom(i + 1)+ ": ");
Object value = rs.getObject(i + 1);
System.out.println(" Column Value: "+value+"
");
}
}
}
}
}
Fichier: Main.java
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
public class Main {
static EntityManagerFactory emf = Persistence.createEntityManagerFactory("JPAService");
static EntityManager em = emf.createEntityManager();
public static void main(String[] a) throws Exception {
em.getTransaction().begin();
Student student = new Student();
student.setNom("Joe");
em.persist(student);
Department dept = new Department();
dept.setNom("dept nom");
student.setDepartment(dept);
em.flush();
Query query = em.createQuery("SELECT e FROM Student e");
List list = (List) query.getResultList();
System.out.println(list);
query = em.createQuery("SELECT d FROM Department d");
List dList = (List) query.getResultList();
System.out.println(dList);
em.getTransaction().commit();
em.close();
emf.close();
Helper.checkData();
}
}
Fichier: persistence.xml