Fichier: Adresse.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.TableGenerator;
@Entity
public class Adresse {
@TableGenerator(nom = "Adresse_Gen", table = "ID_GEN", pkColumnNom = "GEN_NAME", valueColumnNom = "GEN_VAL", pkColumnValue = "Addr_Gen", initialValue = 10000, allocationSize = 100)
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "Adresse_Gen")
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: Professeur.java
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToOne;
import javax.persistence.TableGenerator;
@Entity
public class Professeur {
@TableGenerator(nom = "Emp_Gen", table = "ID_GEN", pkColumnNom = "GEN_NAME", valueColumnNom = "GEN_VAL")
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "Emp_Gen")
private int id;
private String nom;
private long salaire;
@OneToOne
private Adresse address;
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 long getSalary() {
return salaire;
}
public void setSalary(long salaire) {
this.salaire = salaire;
}
public Adresse getAdresse() {
return address;
}
public void setAdresse(Adresse address) {
this.address = address;
}
public String toString() {
return "Professeur id: " + getId() + " nom: " + getNom() + " salaire: " + getSalary();
}
}
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 Professeur createProfesseur(String nom, long salaire,
String rue, String ville, String pays, String codepostal) {
Professeur emp = new Professeur();
emp.setNom(nom);
emp.setSalary(salaire);
Adresse addr = new Adresse();
addr.setRue(rue);
addr.setVille(ville);
addr.setPays(pays);
addr.setCodePostal(codepostal);
emp.setAdresse(addr);
em.persist(emp);
em.persist(addr);
return emp;
}
public void removeProfesseur(int id) {
Professeur emp = findProfesseur(id);
if (emp != null) {
em.remove(emp);
}
}
public Professeur raiseProfesseurSalary(int id, long raise) {
Professeur emp = em.find(Professeur.class, id);
if (emp != null) {
emp.setSalary(emp.getSalary() + raise);
}
return emp;
}
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();
}
}
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();
Professeur emp = service.createProfesseur("nom", 100,
"rue", "ville", "pays", "codepostal");
em.getTransaction().commit();
System.out.println("Persisted " + emp);
util.checkData("select * from Professeur");
util.checkData("select * from ADDRESS");
util.checkData("select * from ID_GEN");
em.close();
emf.close();
}
}
Fichier: persistence.xml