Fichier: Professeur.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Professeur {
@Id private int id;
private String nom;
@OneToMany(mappedBy="employee")
private Collection assignments;
public Professeur() {
assignments = 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 Collection getAssignments() {
return assignments;
}
public String toString() {
return "Professeur id: " + getId() + " nom: " + getNom();
}
}
Fichier: ProfesseurService.java
import java.util.Collection;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.Query;
public class ProfesseurService {
protected EntityManager em;
public ProfesseurService(EntityManager em) {
this.em = em;
}
public Project createProject(String nom, int id) {
Project emp = new Project();
emp.setId(id);
emp.setNom(nom);
em.persist(emp);
return emp;
}
public Professeur createProfesseur(String country, int id, String nom) {
Professeur emp = new Professeur();
emp.setId(id);
emp.setNom(nom);
em.persist(emp);
return emp;
}
public void addProfesseurProject(int id, int projId) {
Professeur emp = findProfesseur(id);
Project proj = em.find(Project.class, projId);
ProjectAssignment assign = new ProjectAssignment(emp, proj);
em.persist(assign);
emp.getAssignments().add(assign);
proj.getAssignments().add(assign);
}
public Professeur findProfesseur(int id) {
return em.find(Professeur.class, id);
}
public Collection findAllProfesseurs() {
Query query = em.createQuery("SELECT e FROM Professeur e");
return (Collection) query.getResultList();
}
public Collection findAllProjects() {
Query query = em.createQuery("SELECT p FROM Project p");
return (Collection) query.getResultList();
}
}
Fichier: Project.java
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.OneToMany;
@Entity
public class Project {
@Id private int id;
private String nom;
@OneToMany(mappedBy="project")
private Collection assignments;
public Project() {
assignments = new ArrayList();
}
public int getId() {
return id;
}
public void setId(int projectNo) {
this.id = projectNo;
}
public String getNom() {
return nom;
}
public void setNom(String projectNom) {
this.nom = projectNom;
}
public Collection getAssignments() {
return assignments;
}
public String toString() {
return getClass().getNom().substring(getClass().getNom().lastIndexOf('.')+1) +
" no: " + getId() +
", nom: " + getNom();
}
}
Fichier: ProjectAssignment.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Table(nom = "EMP_PROJECT")
@IdClass(ProjectAssignmentId.class)
public class ProjectAssignment {
@Id
@Column(nom = "EMP_ID", insertable = false, updatable = false)
private int empId;
@Id
@Column(nom = "PROJECT_ID", insertable = false, updatable = false)
private int projectId;
@ManyToOne
@JoinColumn(nom = "EMP_ID")
Professeur employee;
@ManyToOne
@JoinColumn(nom = "PROJECT_ID")
Project project;
public ProjectAssignment() {
}
public ProjectAssignment(Professeur emp, Project proj) {
this.empId = emp.getId();
this.employee = emp;
this.projectId = proj.getId();
this.project = proj;
}
public int getEmpId() {
return empId;
}
public Professeur getProfesseur() {
return employee;
}
public int getProjectId() {
return projectId;
}
public Project getProject() {
return project;
}
}
Fichier: ProjectAssignmentId.java
import java.io.Serializable;
public class ProjectAssignmentId implements Serializable {
private int empId;
private int projectId;
public ProjectAssignmentId() {}
public ProjectAssignmentId(int empId, int projectId) {
this.empId = empId;
this.projectId = projectId;
}
public int getEmpId() {
return empId;
}
public int getProjectId() {
return projectId;
}
public boolean equals(Object o) {
return ((o instanceof ProjectAssignmentId) &&
empId == ((ProjectAssignmentId)o).getEmpId() &&
projectId == ((ProjectAssignmentId) o).getProjectId());
}
public int hashCode() {
return empId + projectId;
}
}
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.createProject("proj", 1);
service.createProfesseur("country", 1, "nom");
service.addProfesseurProject(1, 1);
System.out.println("Professeurs:");
for (Professeur emp : service.findAllProfesseurs()) {
System.out.print(emp + " with " + emp.getAssignments().size() + " assignments");
}
System.out.println("Projects:");
for (Project p : service.findAllProjects()) {
System.out.print(p + " with " + p.getAssignments().size() + " assignments");
}
util.checkData("select * from Project");
em.getTransaction().commit();
em.close();
emf.close();
}
}
Fichier: persistence.xml