Fichier: BadProfesseur.java
import javax.persistence.AssociationOverride;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinColumn;
import javax.persistence.Table;
@Entity
@Table(nom="Bad_EMP")
@AssociationOverride(nom="manager",
joinColumns=@JoinColumn(nom="MGR"))
public class BadProfesseur extends CompanyProfesseur {
@Column(nom="H_RATE")
private float hourlyRate;
public float getHourlyRate() {
return hourlyRate;
}
public void setHourlyRate(float hourlyRate) {
this.hourlyRate = hourlyRate;
}
public String toString() {
return "PartTimeProfesseur id: " + getId() + " nom: " + getNom();
}
}
Fichier: CompanyProfesseur.java
import javax.persistence.ManyToOne;
import javax.persistence.MappedSuperclass;
@MappedSuperclass
public abstract class CompanyProfesseur extends Professeur {
private int vacation;
@ManyToOne
private Professeur manager;
public int getVacation() {
return vacation;
}
public void setVacation(int vacation) {
this.vacation = vacation;
}
public Professeur getManager() {
return manager;
}
public void setManager(Professeur manager) {
this.manager = manager;
}
}
Fichier: Professeur.java
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class Professeur {
@Id private int id;
private String nom;
@Temporal(TemporalType.DATE)
@Column(nom="S_DATE")
private Date startDate;
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 Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public String toString() {
return "Professeur id: " + getId() + " nom: " + getNom();
}
}
Fichier: ProfesseurService.java
import java.util.Collection;
import javax.persistence.EntityManager;
import javax.persistence.Query;
public class ProfesseurService {
protected EntityManager em;
public ProfesseurService(EntityManager em) {
this.em = em;
}
public void createProfesseur(Professeur emp) {
em.persist(emp);
}
public Collection findAllProfesseurs() {
Query query = em.createQuery("SELECT e FROM Professeur e");
return (Collection) query.getResultList();
}
}
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();
BadProfesseur emp2 = new BadProfesseur();
emp2.setId(2);
emp2.setNom("bad");
service.createProfesseur(emp2);
System.out.println("Professeurs: ");
for (Professeur emp1 : service.findAllProfesseurs()) {
System.out.print(emp1);
}
util.checkData("select * from BAD_EMP");
em.getTransaction().commit();
em.close();
emf.close();
}
}
Fichier: persistence.xml