Hibernate JPA funguje! Na webu se zobrazuje seznam cílů načtený z databáze: JSP → Webová beana → EJB → JPA → Hibernate → PostgreSQL
authorFrantišek Kučera <franta-hg@frantovo.cz>
Sat, 14 Mar 2009 23:15:33 +0100
changeset 458552ccfe6c8
parent 3 769816825525
child 5 cd25a845d256
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).
java/HibernateDemo1/HibernateDemo1-lib/src/cz/frantovo/hibernateDemo1/dto/Cil.java
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady.properties
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady_cs.properties
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady_en.properties
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/SeznamCilu.java
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady.properties
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady_cs.properties
java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady_en.properties
java/HibernateDemo1/HibernateDemo1-war/web/WEB-INF/casti/seznamCilu.jspx
java/HibernateDemo1/nbproject/project.properties
     1.1 --- a/java/HibernateDemo1/HibernateDemo1-lib/src/cz/frantovo/hibernateDemo1/dto/Cil.java	Sat Mar 14 22:55:11 2009 +0100
     1.2 +++ b/java/HibernateDemo1/HibernateDemo1-lib/src/cz/frantovo/hibernateDemo1/dto/Cil.java	Sat Mar 14 23:15:33 2009 +0100
     1.3 @@ -9,16 +9,16 @@
     1.4  public class Cil implements Serializable {
     1.5  
     1.6      private static final long serialVersionUID = 3789654950408517499L;
     1.7 -    private int id;
     1.8 +    private long id;
     1.9      private String nazev;
    1.10      private String url;
    1.11  
    1.12  
    1.13 -    public int getId() {
    1.14 +    public long getId() {
    1.15          return id;
    1.16      }
    1.17  
    1.18 -    public void setId(int id) {
    1.19 +    public void setId(long id) {
    1.20          this.id = id;
    1.21      }
    1.22  
     2.1 --- a/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady.properties	Sat Mar 14 22:55:11 2009 +0100
     2.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.3 @@ -1,1 +0,0 @@
     2.4 -preklady_cs.properties
     2.5 \ No newline at end of file
     3.1 --- a/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady_cs.properties	Sat Mar 14 22:55:11 2009 +0100
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,2 +0,0 @@
     3.4 -nazev=HibernateDemo1
     3.5 -jazyk=\u010De\u0161tina
     4.1 --- a/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo/preklady_en.properties	Sat Mar 14 22:55:11 2009 +0100
     4.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.3 @@ -1,2 +0,0 @@
     4.4 -nazev=HibernateDemo1
     4.5 -jazyk=angli\u010Dtina
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/SeznamCilu.java	Sat Mar 14 23:15:33 2009 +0100
     5.3 @@ -0,0 +1,38 @@
     5.4 +package cz.frantovo.hibernateDemo1;
     5.5 +
     5.6 +import cz.frantovo.hibernateDemo1.CilDAORemote;
     5.7 +import cz.frantovo.hibernateDemo1.dto.Cil;
     5.8 +import java.util.Collection;
     5.9 +import java.util.logging.Level;
    5.10 +import java.util.logging.Logger;
    5.11 +import javax.naming.Context;
    5.12 +import javax.naming.InitialContext;
    5.13 +import javax.naming.NamingException;
    5.14 +
    5.15 +/**
    5.16 + * Webová Beana pro zobrazení seznamu cílů.
    5.17 + * Volá EJB, které tento seznam získá z databáze
    5.18 + * @author fiki
    5.19 + */
    5.20 +public class SeznamCilu {
    5.21 +
    5.22 +    private CilDAORemote cilDAO;
    5.23 +    private static final Logger log = Logger.getLogger(SeznamCilu.class.getSimpleName());
    5.24 +
    5.25 +    public Collection<Cil> getCile() {
    5.26 +        return lookupCilDAO().getCile();
    5.27 +    }
    5.28 +
    5.29 +    private CilDAORemote lookupCilDAO() {
    5.30 +        if (cilDAO == null) {
    5.31 +            try {
    5.32 +                Context c = new InitialContext();
    5.33 +                cilDAO = (CilDAORemote) c.lookup("cz.frantovo.hibernateDemo1.CilDAORemote");
    5.34 +            } catch (NamingException e) {
    5.35 +                log.log(Level.SEVERE, "Chyba při hledání CilDAO", e);
    5.36 +                throw new RuntimeException(e);
    5.37 +            }
    5.38 +        }
    5.39 +        return cilDAO;
    5.40 +    }
    5.41 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady.properties	Sat Mar 14 23:15:33 2009 +0100
     6.3 @@ -0,0 +1,1 @@
     6.4 +preklady_cs.properties
     6.5 \ No newline at end of file
     7.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.2 +++ b/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady_cs.properties	Sat Mar 14 23:15:33 2009 +0100
     7.3 @@ -0,0 +1,2 @@
     7.4 +nazev=HibernateDemo1
     7.5 +jazyk=\u010De\u0161tina
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/HibernateDemo1/HibernateDemo1-war/src/java/cz/frantovo/hibernateDemo1/preklady_en.properties	Sat Mar 14 23:15:33 2009 +0100
     8.3 @@ -0,0 +1,2 @@
     8.4 +nazev=HibernateDemo1
     8.5 +jazyk=angli\u010Dtina
     9.1 --- a/java/HibernateDemo1/HibernateDemo1-war/web/WEB-INF/casti/seznamCilu.jspx	Sat Mar 14 22:55:11 2009 +0100
     9.2 +++ b/java/HibernateDemo1/HibernateDemo1-war/web/WEB-INF/casti/seznamCilu.jspx	Sat Mar 14 23:15:33 2009 +0100
     9.3 @@ -1,6 +1,24 @@
     9.4  <?xml version="1.0" encoding="UTF-8"?>
     9.5 -<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
     9.6 +<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
     9.7 +          xmlns:c="http://java.sun.com/jsp/jstl/core"
     9.8 +          xmlns:f="http://java.sun.com/jsp/jstl/functions"
     9.9 +          version="2.0">
    9.10  
    9.11 -    <p>ahoj</p>
    9.12 +    <jsp:useBean class="cz.frantovo.hibernateDemo1.SeznamCilu" id="seznamCilu" scope="request"/>
    9.13 +
    9.14 +    <ul>
    9.15 +        <c:forEach var="c" items="${seznamCilu.cile}">
    9.16 +            <li>
    9.17 +                <c:choose>
    9.18 +                    <c:when test="${c.url == null}">
    9.19 +                        <c:out value="${c.nazev}"/>
    9.20 +                    </c:when>
    9.21 +                    <c:otherwise>
    9.22 +                        <a href="${f:escapeXml(c.url)}"><c:out value="${c.nazev}"/></a>
    9.23 +                    </c:otherwise>
    9.24 +                </c:choose>
    9.25 +            </li>
    9.26 +        </c:forEach>
    9.27 +    </ul>
    9.28  
    9.29  </jsp:root>
    10.1 --- a/java/HibernateDemo1/nbproject/project.properties	Sat Mar 14 22:55:11 2009 +0100
    10.2 +++ b/java/HibernateDemo1/nbproject/project.properties	Sat Mar 14 23:15:33 2009 +0100
    10.3 @@ -4,7 +4,7 @@
    10.4  client.module.uri=HibernateDemo1-war
    10.5  client.urlPart=
    10.6  debug.classpath=${javac.classpath}::${jar.content.additional}:${run.classpath}
    10.7 -display.browser=true
    10.8 +display.browser=false
    10.9  dist.dir=dist
   10.10  dist.jar=${dist.dir}/${jar.name}
   10.11  j2ee.appclient.mainclass.args=-client ${dist.jar} ${j2ee.appclient.tool.args}
   10.12 @@ -28,4 +28,5 @@
   10.13  reference.HibernateDemo1-ejb.dist-ear=${project.HibernateDemo1-ejb}/dist/HibernateDemo1-ejb.jar
   10.14  reference.HibernateDemo1-war.dist-ear=${project.HibernateDemo1-war}/dist/HibernateDemo1-war.war
   10.15  resource.dir=setup
   10.16 +run.classpath=
   10.17  source.root=.