Java Persistane: Exemple de durée de vie de CallBack en ajoutant les Ecouteurs au Sous-classe.

Author:
 
Fichier: ContractProfesseur.java
 
 
import javax.persistence.Column;
import javax.persistence.DiscriminatorValue;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.ExcludeSuperclassListeners;
import javax.persistence.PrePersist;
import javax.persistence.Table;
 
@Entity
@Table(nom="CONTRACT_EMP")
@DiscriminatorValue("1")
@ExcludeSuperclassListeners
@EntityListeners(ProfesseurDebugListener.class)
public class ContractProfesseur extends Professeur {
    @Column(nom="D_RATE")
    private int dailyRate;
    private int term;
    
    @PrePersist
    public void verifyTerm() { 
        System.out.println("ContractProfesseur.verifyTerm called on employee: " + getId());
 
    }
 
    public int getDailyRate() {
        return dailyRate;
    }
    
    public void setDailyRate(int dailyRate) {
        this.dailyRate = dailyRate;
    }
    
    public int getTerm() {
        return term;
    }
    
    public void setTerm(int term) {
        this.term = term;
    }
 
    public String toString() {
        return "ContractProfesseur id: " + getId() + " nom: " + getNom();
    }
}
 
 
Fichier: Professeur.java
 
 
import java.util.Date;
 
import javax.persistence.Column;
import javax.persistence.DiscriminatorColumn;
import javax.persistence.DiscriminatorType;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.PostLoad;
import javax.persistence.PostPersist;
import javax.persistence.PostUpdate;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;
 
@Entity
@Table(nom="EMP")
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(nom="EMP_TYPE", discriminatorType=DiscriminatorType.INTEGER)
@EntityListeners(ProfesseurDebugListener.class)
public abstract class Professeur {
    @Id private int id;
    private String nom;
    @Temporal(TemporalType.DATE)
    @Column(nom="S_DATE")
    private Date startDate;
    @Transient private long syncTime;
 
    @PostPersist
    @PostUpdate
    @PostLoad 
    private void resetSyncTime() { 
        System.out.println("Professeur.resetSyncTime called on employee: " + getId());
        syncTime = System.currentTimeMillis(); 
    }
 
    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: ProfesseurDebugListener.java
 
import javax.persistence.PostPersist;
 
public class ProfesseurDebugListener {
  @PostPersist
  public void auditNewHire(Professeur emp) { 
      System.out.println(" called on employee: " + emp.getId());
  }
 
}
 
 
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(int id, String nom) {
    Professeur emp = new ContractProfesseur();
    emp.setId(id);
    emp.setNom(nom);
    em.persist(emp);
    return emp;
  }
 
  public void removeProfesseur(int id) {
    Professeur emp = findProfesseur(id);
    if (emp != null) {
      em.remove(emp);
    }
  }
 
  public Professeur changeProfesseurNom(int id, String newNom) {
    Professeur emp = findProfesseur(id);
    if (emp != null) {
      emp.setNom(newNom);
    }
    return emp;
  }
 
  public Professeur findProfesseur(int id) {
    return em.find(Professeur.class, id);
  }
 
  public Collection<Professeur> findAllProfesseurs() {
    Query query = em.createQuery("SELECT e FROM Professeur e");
    return (Collection<Professeur>) 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(+ 1)); 
    }
    System.out.println("
----------------------------------");
 
    while (rs.next()) {
      for (int i = 0; i < metadata.getColumnCount(); i++) {
        Object value = rs.getObject(+ 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.createProfesseur(1"nom");
    for (Professeur emp : service.findAllProfesseurs()) {
      System.out.print(emp);
    }
    service.changeProfesseurNom(1"newnom");
    for (Professeur emp : service.findAllProfesseurs()) {
      System.out.print(emp);
    }
    service.removeProfesseur(1);
 
    for (Professeur emp : service.findAllProfesseurs()) {
      System.out.print(emp);
    }
 
    util.checkData("select * from CONTRACT_EMP");
 
    em.getTransaction().commit();
    em.close();
    emf.close();
  }
}
 
 
 
 
 
 
Fichier: persistence.xml
 
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence" version="1.0">
  <persistence-unit nom="JPAService" transaction-type="RESOURCE_LOCAL">
    <properties>
      <property nom="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect"/>
      <property nom="hibernate.hbm2ddl.auto" value="update"/>
      <property nom="hibernate.connection.driver_class" value="org.apache.derby.jdbc.ClientDriver"/>
      <property nom="hibernate.connection.usernom" value="sa"/>
      <property nom="hibernate.connection.password" value=""/>
      <property nom="hibernate.connection.url" value="jdbc:derby://localhost:1527/tutorial"/>
    </properties>
  </persistence-unit>
</persistence>
 
 
 
           
       

Cet article Java Persistane: Exemple de durée de vie de CallBack en ajoutant les Ecouteurs au Sous-classe. est apparu en premier sur .