java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/SeznamCilu.java
author František Kučera <franta-hg@frantovo.cz>
Sat Mar 14 23:15:33 2009 +0100 (2009-03-14)
changeset 4 58552ccfe6c8
permissions -rw-r--r--
Hibernate JPA funguje! Na webu se zobrazuje seznam cílů načtený z databáze: JSP → Webová beana → EJB → JPA → Hibernate → PostgreSQL
Jen pozor na správné typy: typ v mapovacím .hbm.xml souboru musí odpovídat typu v databázi, jinak by se aplikace ani nedeploynula
a zároveň musíme mít stejný typ v DTO – jinak aplikace padá (při otevření stránky).
franta-hg@4
     1
package cz.frantovo.hibernateDemo1;
franta-hg@4
     2
franta-hg@4
     3
import cz.frantovo.hibernateDemo1.CilDAORemote;
franta-hg@4
     4
import cz.frantovo.hibernateDemo1.dto.Cil;
franta-hg@4
     5
import java.util.Collection;
franta-hg@4
     6
import java.util.logging.Level;
franta-hg@4
     7
import java.util.logging.Logger;
franta-hg@4
     8
import javax.naming.Context;
franta-hg@4
     9
import javax.naming.InitialContext;
franta-hg@4
    10
import javax.naming.NamingException;
franta-hg@4
    11
franta-hg@4
    12
/**
franta-hg@4
    13
 * Webová Beana pro zobrazení seznamu cílů.
franta-hg@4
    14
 * Volá EJB, které tento seznam získá z databáze
franta-hg@4
    15
 * @author fiki
franta-hg@4
    16
 */
franta-hg@4
    17
public class SeznamCilu {
franta-hg@4
    18
franta-hg@4
    19
    private CilDAORemote cilDAO;
franta-hg@4
    20
    private static final Logger log = Logger.getLogger(SeznamCilu.class.getSimpleName());
franta-hg@4
    21
franta-hg@4
    22
    public Collection<Cil> getCile() {
franta-hg@4
    23
        return lookupCilDAO().getCile();
franta-hg@4
    24
    }
franta-hg@4
    25
franta-hg@4
    26
    private CilDAORemote lookupCilDAO() {
franta-hg@4
    27
        if (cilDAO == null) {
franta-hg@4
    28
            try {
franta-hg@4
    29
                Context c = new InitialContext();
franta-hg@4
    30
                cilDAO = (CilDAORemote) c.lookup("cz.frantovo.hibernateDemo1.CilDAORemote");
franta-hg@4
    31
            } catch (NamingException e) {
franta-hg@4
    32
                log.log(Level.SEVERE, "Chyba při hledání CilDAO", e);
franta-hg@4
    33
                throw new RuntimeException(e);
franta-hg@4
    34
            }
franta-hg@4
    35
        }
franta-hg@4
    36
        return cilDAO;
franta-hg@4
    37
    }
franta-hg@4
    38
}