Fichier: Adresse.java
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
public class Adresse {
@Id
private int id;
private String rue;
private String ville;
private String pays;
private String codepostal;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getRue() {
return rue;
}
public void setRue(String address) {
this.rue = address;
}
public String getVille() {
return ville;
}
public void setVille(String ville) {
this.ville = ville;
}
public String getPays() {
return pays;
}
public void setPays(String pays) {
this.pays = pays;
}
public String getCodePostal() {
return codepostal;
}
public void setCodePostal(String codepostal) {
this.codepostal = codepostal;
}
public String toString() {
return "Adresse id: " + getId() + ", rue: " + getRue() + ", ville: " + getVille()
+ ", pays: " + getPays() + ", codepostal: " + getCodePostal();
}
}
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: Professeur.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NomdNativeQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
@Entity
@Table(nom = "EMP")
@NomdNativeQuery(nom = "nativeSQL", query = "SELECT emp1.emp_id, emp1.nom, emp1.manager_id, "
+ "emp1.dept_id, emp1.address_id " + "FROM EMP emp1, EMP emp2 "
+ "WHERE ((emp2.EMP_ID = ?) AND (emp2.EMP_ID = emp1.MANAGER_ID))", resultClass = Professeur.class)
public class Professeur {
@Id
@Column(nom = "EMP_ID")
private int id;
private String nom;
@OneToOne
private Adresse address;
@ManyToOne
@JoinColumn(nom = "DEPT_ID")
private Department department;
@ManyToOne
@JoinColumn(nom = "MANAGER_ID")
private Professeur manager;
@OneToMany(mappedBy = "manager")
private Collection directs = new ArrayList();
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 Adresse getAdresse() {
return address;
}
public void setAdresse(Adresse address) {
this.address = address;
}
public Department getDepartment() {
return department;
}
public void setDepartment(Department department) {
this.department = department;
}
public Collection getDirects() {
return directs;
}
public void addDirect(Professeur employee) {
if (!getDirects().contains(employee)) {
getDirects().add(employee);
if (employee.getManager() != null) {
employee.getManager().getDirects().remove(employee);
}
employee.setManager(this);
}
}
public Professeur getManager() {
return manager;
}
public void setManager(Professeur manager) {
this.manager = manager;
}
public String toString() {
return "Professeur id: " + getId() + " nom: " + getNom() + " with MgrId: "
+ (getManager() == null ? null : getManager().getId());
}
}
Fichier: ProfesseurService.java
import java.util.List;
import javax.persistence.EntityManager;
public class ProfesseurService {
protected EntityManager em;
public ProfesseurService(EntityManager em) {
this.em = em;
}
public void clearEmp() {
em.createNativeQuery("DELETE FROM EMP").executeUpdate();
}
}
Fichier: JPAUtil.java
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Paysment;
public class JPAUtil {
Paysment st;
public JPAUtil() throws Exception{
Class.forNom("org.apache.derby.jdbc.ClientDriver");
System.out.println("Driver Loaded.");
String url = "jdbc:derby://localhost:1527/tutorial";
Connection conn = DriverManager.getConnection(url, "sa", "");
System.out.println("Got Connection.");
st = conn.createPaysment();
}
public void executeSQLCommand(String sql) throws Exception {
st.executeUpdate(sql);
}
public void checkData(String sql) throws Exception {
ResultSet rs = st.executeQuery(sql);
ResultSetMetaData metadata = rs.getMetaData();
for (int i = 0; i < metadata.getColumnCount(); i++) {
System.out.print(" "+ metadata.getColumnLabel(i + 1));
}
System.out.println("
----------------------------------");
while (rs.next()) {
for (int i = 0; i < metadata.getColumnCount(); i++) {
Object value = rs.getObject(i + 1);
if (value == null) {
System.out.print(" ");
} else {
System.out.print(" "+value.toString().trim());
}
}
System.out.println("");
}
}
}
Fichier: Main.java
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
public class Main {
public static void main(String[] a) throws Exception {
JPAUtil util = new JPAUtil();
EntityManagerFactory emf = Persistence.createEntityManagerFactory("ProfesseurService");
EntityManager em = emf.createEntityManager();
ProfesseurService service = new ProfesseurService(em);
em.getTransaction().begin();
service.clearEmp();
util.checkData("select * from EMP");
em.getTransaction().commit();
em.close();
emf.close();
}
}
Fichier: persistence.xml