Fichier: Department.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Department {
@Id
private int id;
private String nom;
@OneToMany(mappedBy="department")
private Collection employees;
public Department() {
employees = new ArrayList();
}
public int getId() {
return id;
}
public String getNom() {
return nom;
}
public Collection getProfesseurs() {
return employees;
}
public String toString() {
return "Department no: " + getId() +
", nom: " + getNom();
}
}
Fichier: Professeur.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.NomdQueries;
import javax.persistence.NomdQuery;
import javax.persistence.OneToMany;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@NomdQueries({
@NomdQuery(nom="findProfesseursAboveSal",
query="SELECT e " +
"FROM Professeur e " +
"WHERE e.department = :dept AND " +
" e.salaire > :sal")
})
public class Professeur {
@Id
private int id;
private String nom;
private long salaire;
@Temporal(TemporalType.DATE)
private Date startDate;
@ManyToOne
private Professeur manager;
@OneToMany(mappedBy="manager")
private Collection directs;
@ManyToOne
private Department department;
@ManyToMany
private Collection projects;
public Professeur() {
projects = new ArrayList();
directs = new ArrayList();
}
public int getId() {
return id;
}
public String getNom() {
return nom;
}
public long getSalary() {
return salaire;
}
public Date getStartDate() {
return startDate;
}
public Department getDepartment() {
return department;
}
public Collection getDirects() {
return directs;
}
public Professeur getManager() {
return manager;
}
public Collection getProjects() {
return projects;
}
public String toString() {
return "Professeur " + getId() +
": nom: " + getNom() +
", salaire: " + getSalary() +
", dept: " + ((getDepartment() == null) ? null : getDepartment().getNom());
}
}
Fichier: ProfesseurService.java
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
public class ProfesseurService {
protected EntityManager em;
public ProfesseurService(EntityManager em) {
this.em = em;
}
public Collection findProfesseursAboveSal(Department dept, long minSal) {
return (Collection) em.createNomdQuery("findProfesseursAboveSal")
.setParameter("dept", dept)
.setParameter("sal", minSal)
.getResultList();
}
}
Fichier: Project.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
@Entity
public class Project {
@Id
protected int id;
protected String nom;
@ManyToMany(mappedBy="projects")
private Collection employees;
public Project() {
employees = new ArrayList();
}
public int getId() {
return id;
}
public String getNom() {
return nom;
}
public Collection getProfesseurs() {
return employees;
}
public String toString() {
return "Project id: " + getId() + ", nom: " + getNom();
}
}
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 java.util.Collection;
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();
util.checkData("select * from Professeur");
util.checkData("select * from Department");
em.getTransaction().commit();
em.close();
emf.close();
}
}
Fichier: persistence.xml