Hibernate. 4. díl
authorFrantišek Kučera <franta-hg@frantovo.cz>
Thu, 11 Feb 2010 10:06:39 +0100
changeset 280f037986bb6b
parent 27 88f3012c60a2
child 29 48dedc9654f2
Hibernate.
java/nekurak.net-ear/nbproject/project.properties
java/nekurak.net-ejb/src/conf/Podnik.hbm.xml
java/nekurak.net-ejb/src/conf/persistence.xml
java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikHibernateDAO.java
java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java
java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikHibernateDAORemote.java
java/nekurak.net-web/src/java/cz/frantovo/nekurak/dto/Podnik.java
java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/PodnikyHibernateWeb.java
java/nekurak.net-web/web/hibernate.jsp
     1.1 --- a/java/nekurak.net-ear/nbproject/project.properties	Thu Feb 11 08:52:50 2010 +0100
     1.2 +++ b/java/nekurak.net-ear/nbproject/project.properties	Thu Feb 11 10:06:39 2010 +0100
     1.3 @@ -4,7 +4,7 @@
     1.4  client.module.uri=nekurak.net-web
     1.5  client.urlPart=
     1.6  debug.classpath=${javac.classpath}::${jar.content.additional}:${run.classpath}
     1.7 -display.browser=true
     1.8 +display.browser=false
     1.9  dist.dir=dist
    1.10  dist.jar=${dist.dir}/${jar.name}
    1.11  endorsed.classpath=\
    1.12 @@ -30,4 +30,5 @@
    1.13  reference.nekurak_net-ejb.dist-ear=${project.nekurak_net-ejb}/dist/nekurak.net-ejb.jar
    1.14  reference.nekurak_net-web.dist-ear=${project.nekurak_net-web}/dist/nekurak.net-web.war
    1.15  resource.dir=setup
    1.16 +run.classpath=
    1.17  source.root=.
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/java/nekurak.net-ejb/src/conf/Podnik.hbm.xml	Thu Feb 11 10:06:39 2010 +0100
     2.3 @@ -0,0 +1,9 @@
     2.4 +<?xml version="1.0"?>
     2.5 +<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
     2.6 +"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
     2.7 +<hibernate-mapping>
     2.8 +    <class name="cz.frantovo.nekurak.dto.Podnik" table="podnik">
     2.9 +	<id name="id" column="id" type="integer"/>
    2.10 +	<property name="nazev" column="nazev"/>
    2.11 +    </class>
    2.12 +</hibernate-mapping>
    2.13 \ No newline at end of file
     3.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.2 +++ b/java/nekurak.net-ejb/src/conf/persistence.xml	Thu Feb 11 10:06:39 2010 +0100
     3.3 @@ -0,0 +1,17 @@
     3.4 +<?xml version="1.0" encoding="UTF-8"?>
     3.5 +<persistence version="1.0" 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_1_0.xsd">
     3.6 +  <persistence-unit name="nekurak.net-PU" transaction-type="JTA">
     3.7 +    <provider>org.hibernate.ejb.HibernatePersistence</provider>
     3.8 +    <jta-data-source>jdbc/nekurak</jta-data-source>
     3.9 +    <exclude-unlisted-classes>false</exclude-unlisted-classes>
    3.10 +    <properties>
    3.11 +      <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
    3.12 +      <property name="hibernate.hbm2ddl.auto" value="validate"/>
    3.13 +      <property name="hibernate.max_fetch_depth " value="3"/>
    3.14 +      <property name="hibernate.default_batch_fetch_size" value="16"/>
    3.15 +      <property name="hibernate.order_updates" value="true"/>
    3.16 +      <property name="hibernate.order_inserts" value="true"/>
    3.17 +      <property name="hibernate.show_sql" value="false"/>
    3.18 +    </properties>
    3.19 +  </persistence-unit>
    3.20 +</persistence>
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikHibernateDAO.java	Thu Feb 11 10:06:39 2010 +0100
     4.3 @@ -0,0 +1,31 @@
     4.4 +package cz.frantovo.nekurak.ejb;
     4.5 +
     4.6 +import cz.frantovo.nekurak.dto.Podnik;
     4.7 +import java.util.Collection;
     4.8 +import javax.ejb.Stateless;
     4.9 +import javax.persistence.EntityManager;
    4.10 +import javax.persistence.PersistenceContext;
    4.11 +import javax.persistence.Query;
    4.12 +
    4.13 +/**
    4.14 + *
    4.15 + * @author fiki
    4.16 + */
    4.17 +@Stateless
    4.18 +public class PodnikHibernateDAO implements PodnikHibernateDAORemote {
    4.19 +
    4.20 +    private static final String PU = "nekurak.net-PU";
    4.21 +
    4.22 +    @PersistenceContext(unitName = PU)
    4.23 +    private EntityManager em;
    4.24 +
    4.25 +    public Collection<Podnik> getPodniky() {
    4.26 +	Query dotaz = em.createQuery("FROM " + t(Podnik.class) + " o ORDER BY nazev");
    4.27 +        return dotaz.getResultList();
    4.28 +    }
    4.29 +
    4.30 +    
    4.31 +    private static String t(Class trida) {
    4.32 +        return trida.getSimpleName();
    4.33 +    }
    4.34 +}
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java	Thu Feb 11 10:06:39 2010 +0100
     5.3 @@ -0,0 +1,38 @@
     5.4 +package cz.frantovo.nekurak.dto;
     5.5 +
     5.6 +import java.io.Serializable;
     5.7 +
     5.8 +/**
     5.9 + *
    5.10 + * @author fiki
    5.11 + */
    5.12 +public class Podnik implements Serializable {
    5.13 +
    5.14 +    private int id;
    5.15 +    private String nazev;
    5.16 +
    5.17 +    public Podnik() {
    5.18 +    }
    5.19 +
    5.20 +    public Podnik(int id, String nazev) {
    5.21 +	this.id = id;
    5.22 +	this.nazev = nazev;
    5.23 +    }
    5.24 +
    5.25 +    public int getId() {
    5.26 +	return id;
    5.27 +    }
    5.28 +
    5.29 +    public void setId(int id) {
    5.30 +	this.id = id;
    5.31 +    }
    5.32 +
    5.33 +    public String getNazev() {
    5.34 +	return nazev;
    5.35 +    }
    5.36 +
    5.37 +    public void setNazev(String nazev) {
    5.38 +	this.nazev = nazev;
    5.39 +    }
    5.40 +
    5.41 +}
     6.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     6.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikHibernateDAORemote.java	Thu Feb 11 10:06:39 2010 +0100
     6.3 @@ -0,0 +1,16 @@
     6.4 +package cz.frantovo.nekurak.ejb;
     6.5 +
     6.6 +import cz.frantovo.nekurak.dto.Podnik;
     6.7 +import java.util.Collection;
     6.8 +import javax.ejb.Remote;
     6.9 +
    6.10 +/**
    6.11 + *
    6.12 + * @author fiki
    6.13 + */
    6.14 +@Remote
    6.15 +public interface PodnikHibernateDAORemote {
    6.16 +
    6.17 +    public Collection<Podnik> getPodniky();
    6.18 +    
    6.19 +}
     7.1 --- a/java/nekurak.net-web/src/java/cz/frantovo/nekurak/dto/Podnik.java	Thu Feb 11 08:52:50 2010 +0100
     7.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     7.3 @@ -1,38 +0,0 @@
     7.4 -package cz.frantovo.nekurak.dto;
     7.5 -
     7.6 -import java.io.Serializable;
     7.7 -
     7.8 -/**
     7.9 - *
    7.10 - * @author fiki
    7.11 - */
    7.12 -public class Podnik implements Serializable {
    7.13 -
    7.14 -    private int id;
    7.15 -    private String nazev;
    7.16 -
    7.17 -    public Podnik() {
    7.18 -    }
    7.19 -
    7.20 -    public Podnik(int id, String nazev) {
    7.21 -	this.id = id;
    7.22 -	this.nazev = nazev;
    7.23 -    }
    7.24 -
    7.25 -    public int getId() {
    7.26 -	return id;
    7.27 -    }
    7.28 -
    7.29 -    public void setId(int id) {
    7.30 -	this.id = id;
    7.31 -    }
    7.32 -
    7.33 -    public String getNazev() {
    7.34 -	return nazev;
    7.35 -    }
    7.36 -
    7.37 -    public void setNazev(String nazev) {
    7.38 -	this.nazev = nazev;
    7.39 -    }
    7.40 -
    7.41 -}
     8.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     8.2 +++ b/java/nekurak.net-web/src/java/cz/frantovo/nekurak/web/PodnikyHibernateWeb.java	Thu Feb 11 10:06:39 2010 +0100
     8.3 @@ -0,0 +1,38 @@
     8.4 +package cz.frantovo.nekurak.web;
     8.5 +
     8.6 +import cz.frantovo.nekurak.dto.Podnik;
     8.7 +import cz.frantovo.nekurak.ejb.PodnikHibernateDAORemote;
     8.8 +import java.io.Serializable;
     8.9 +import java.util.Collection;
    8.10 +import java.util.logging.Level;
    8.11 +import java.util.logging.Logger;
    8.12 +import javax.naming.Context;
    8.13 +import javax.naming.InitialContext;
    8.14 +import javax.naming.NamingException;
    8.15 +
    8.16 +/**
    8.17 + *
    8.18 + * @author fiki
    8.19 + */
    8.20 +public class PodnikyHibernateWeb implements Serializable {
    8.21 +
    8.22 +    private PodnikHibernateDAORemote podnikDAO;
    8.23 +    private static final Logger log = Logger.getLogger(PodnikyHibernateWeb.class.getSimpleName());
    8.24 +
    8.25 +    public Collection<Podnik> getPodniky() {
    8.26 +	return lookupCilDAO().getPodniky();
    8.27 +    }
    8.28 +
    8.29 +    private PodnikHibernateDAORemote lookupCilDAO() {
    8.30 +	if (podnikDAO == null) {
    8.31 +	    try {
    8.32 +		Context c = new InitialContext();
    8.33 +		podnikDAO = (PodnikHibernateDAORemote) c.lookup("cz.frantovo.nekurak.ejb.PodnikHibernateDAORemote");
    8.34 +	    } catch (NamingException e) {
    8.35 +		log.log(Level.SEVERE, "Chyba při hledání PodnikHibernateDAO", e);
    8.36 +		throw new RuntimeException(e);
    8.37 +	    }
    8.38 +	}
    8.39 +	return podnikDAO;
    8.40 +    }
    8.41 +}
     9.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     9.2 +++ b/java/nekurak.net-web/web/hibernate.jsp	Thu Feb 11 10:06:39 2010 +0100
     9.3 @@ -0,0 +1,42 @@
     9.4 +<?xml version="1.0" encoding="UTF-8"?>
     9.5 +<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
     9.6 +	  xmlns:c="http://java.sun.com/jsp/jstl/core"
     9.7 +	  version="2.0">
     9.8 +    <jsp:directive.page contentType="application/xhtml+xml"/>
     9.9 +    <jsp:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    9.10 +		doctype-root-element="html"
    9.11 +		omit-xml-declaration="false"/>
    9.12 +
    9.13 +    <html xmlns="http://www.w3.org/1999/xhtml">
    9.14 +	<head>
    9.15 +	    <title>Nekuřák.net</title>
    9.16 +	    <link href="styl.css" type="text/css" rel="StyleSheet"/>
    9.17 +	</head>
    9.18 +	<body>
    9.19 +	    <h1>Nekuřák.net – Hibernate:</h1>
    9.20 +	    <p>Načítáme údaje z databáze.</p>
    9.21 +
    9.22 +	    <p>Seznam podniků načtený pomocí Hibernate:</p>
    9.23 +	    <!-- Vytvoříme si instanci JavaBeany -->
    9.24 +	    <jsp:useBean id="podnikyWeb" class="cz.frantovo.nekurak.web.PodnikyHibernateWeb" scope="request"/>
    9.25 +	    <table>
    9.26 +		<thead>
    9.27 +		    <tr>
    9.28 +			<td>Číslo</td>
    9.29 +			<td>Název</td>
    9.30 +		    </tr>
    9.31 +		</thead>
    9.32 +		<tbody>
    9.33 +		    <c:forEach var="p" items="${podnikyWeb.podniky}">
    9.34 +			<tr>
    9.35 +			    <td><c:out value="${p.id}"/></td>
    9.36 +			    <td><c:out value="${p.nazev}"/></td>
    9.37 +			</tr>
    9.38 +		    </c:forEach>
    9.39 +		</tbody>
    9.40 +	    </table>
    9.41 +
    9.42 +	</body>
    9.43 +    </html>
    9.44 +
    9.45 +</jsp:root>