Java Persistane: Exemple d’un schéma un à un: Schéma au sens unique.

Author:
       


Fichier: ParkingSpace.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
public class ParkingSpace {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private int lot;
    private String location;

    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }

    public int getLot() {
        return lot;
    }

    public void setLot(int lot) {
        this.lot = lot;
    }
    
    public String getLocation() {
        return location;
    }
    
    public void setLocation(String deptNom) {
        this.location = deptNom;
    }

    public String toString() {
        return "ParkingSpace id: " + getId() + " lot: " + getLot() +
               ", location: " + getLocation();
    }

}


Fichier: Professeur.java



import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;

@Entity
public class Professeur {
    @Id @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String nom;
    private long salaire;
    
    @OneToOne 
    @JoinColumn(nom="PSPACE_ID") 
    private ParkingSpace parkingSpace;

    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 ParkingSpace getParkingSpace() {
        return parkingSpace;
    }
    
    public void setParkingSpace(ParkingSpace parkingSpace) {
        this.parkingSpace = parkingSpace;
    }

    public String toString() {
        return "Professeur id: " + getId() + " nom: " + getNom() + 
               " with " + getParkingSpace();
    }
}


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) {
    Professeur emp = new Professeur();
    emp.setNom(nom);
    emp.setSalary(salaire);
    em.persist(emp);

    return emp;
  }

  public Professeur setProfesseurParkingSpace(int empId, int spaceId) {
    Professeur emp = em.find(Professeur.class, empId);
    ParkingSpace pSpace = em.find(ParkingSpace.class, spaceId);
    emp.setParkingSpace(pSpace);
    return emp;
  }

  public Collection findAllProfesseurs() {
    Query query = em.createQuery("SELECT e FROM Professeur e");
    return (Collection) query.getResultList();
  }

  public ParkingSpace createParkingSpace(int lot, String location) {
    ParkingSpace space = new ParkingSpace();
    space.setLot(lot);
    space.setLocation(location);
    em.persist(space);

    return space;
  }

  public Collection findAllParkingSpaces() {
    Query query = em.createQuery("SELECT p FROM ParkingSpace p");
    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 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();
    Professeur emp = service.createProfesseur("empNom", 100);
    ParkingSpace pSpace = service.createParkingSpace(1, "location");
    Professeur emp1 = service.setProfesseurParkingSpace(emp.getId(), pSpace.getId());

    Collection emps = service.findAllProfesseurs();
    if (emps.isEmpty()) {
      System.out.println("No Professeurs found ");
    } else {
      System.out.println("Found Professeurs: ");
      for (Professeur emp2 : emps) {
        System.out.println(emp2);
      }
    }

    Collection pSpaces = service.findAllParkingSpaces();
    if (pSpaces.isEmpty()) {
      System.out.println("No ParkingSpaces found ");
    } else {
      System.out.println("Found ParkingSpaces:");
      for (ParkingSpace pSpace1 : pSpaces) {
        System.out.println(pSpace1);
      }
    }

    util.checkData("select * from Professeur");
    util.checkData("select * from ParkingSpace");

    em.getTransaction().commit();
    em.close();
    emf.close();
  }
}




Fichier: persistence.xml


  
    
      
      
      
      
      
      
    
  


           
              

A Voir sur le même Sujet:

  • connection BDD et netbens