Hibernate: jednoduché mapování podniků a fotek (1:N).
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/java/nekurak.net-ejb/src/conf/Fotka.hbm.xml Sun Mar 14 01:05:35 2010 +0100
1.3 @@ -0,0 +1,13 @@
1.4 +<?xml version="1.0"?>
1.5 +<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
1.6 +"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
1.7 +<hibernate-mapping>
1.8 + <class name="cz.frantovo.nekurak.dto.Fotka" table="fotka">
1.9 + <id name="id" column="id" type="integer">
1.10 + <generator class="sequence">
1.11 + <param name="sequence">fotka_seq</param>
1.12 + </generator>
1.13 + </id>
1.14 + <property name="popis" column="popis"/>
1.15 + </class>
1.16 +</hibernate-mapping>
1.17 \ No newline at end of file
2.1 --- a/java/nekurak.net-ejb/src/conf/Podnik.hbm.xml Sat Mar 13 23:51:43 2010 +0100
2.2 +++ b/java/nekurak.net-ejb/src/conf/Podnik.hbm.xml Sun Mar 14 01:05:35 2010 +0100
2.3 @@ -16,5 +16,10 @@
2.4 <property name="cisloPopisne" column="cislo_popisne"/>
2.5 <property name="mesto" column="mesto"/>
2.6 <property name="spravce" column="spravce"/>
2.7 + <set name="fotky" table="fotka" lazy="true">
2.8 + <key column="podnik" foreign-key="id"/>
2.9 + <one-to-many class="cz.frantovo.nekurak.dto.Fotka"/>
2.10 + </set>
2.11 +
2.12 </class>
2.13 </hibernate-mapping>
2.14 \ 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/java/cz/frantovo/nekurak/dao/DAO.java Sun Mar 14 01:05:35 2010 +0100
3.3 @@ -0,0 +1,10 @@
3.4 +package cz.frantovo.nekurak.dao;
3.5 +
3.6 +public class DAO {
3.7 +
3.8 + public static final String PU = "nekurak.net-PU";
3.9 +
3.10 + public static String t(Class trida) {
3.11 + return trida.getSimpleName();
3.12 + }
3.13 +}
4.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Sat Mar 13 23:51:43 2010 +0100
4.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/PodnikDAO.java Sun Mar 14 01:05:35 2010 +0100
4.3 @@ -17,12 +17,11 @@
4.4 @LocalBean
4.5 public class PodnikDAO {
4.6
4.7 - private static final String PU = "nekurak.net-PU";
4.8 - @PersistenceContext(unitName = PU)
4.9 + @PersistenceContext(unitName = DAO.PU)
4.10 private EntityManager em;
4.11
4.12 public Collection<Podnik> getPodniky() {
4.13 - Query dotaz = em.createQuery("FROM " + t(Podnik.class) + " o ORDER BY datum DESC");
4.14 + Query dotaz = em.createQuery("FROM " + DAO.t(Podnik.class) + " o ORDER BY datum DESC");
4.15 return dotaz.getResultList();
4.16 }
4.17
4.18 @@ -41,8 +40,4 @@
4.19
4.20 em.merge(p);
4.21 }
4.22 -
4.23 - private static String t(Class trida) {
4.24 - return trida.getSimpleName();
4.25 - }
4.26 }
5.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/UzivatelDAO.java Sat Mar 13 23:51:43 2010 +0100
5.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/dao/UzivatelDAO.java Sun Mar 14 01:05:35 2010 +0100
5.3 @@ -15,8 +15,7 @@
5.4 @LocalBean
5.5 public class UzivatelDAO {
5.6
5.7 - private static final String PU = "nekurak.net-PU";
5.8 - @PersistenceContext(unitName = PU)
5.9 + @PersistenceContext(unitName = DAO.PU)
5.10 private EntityManager em;
5.11
5.12 public void zaloz(Uzivatel u) {
6.1 --- a/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Sat Mar 13 23:51:43 2010 +0100
6.2 +++ b/java/nekurak.net-ejb/src/java/cz/frantovo/nekurak/ejb/PodnikEJB.java Sun Mar 14 01:05:35 2010 +0100
6.3 @@ -23,6 +23,12 @@
6.4
6.5 public Collection<Podnik> getPodniky() {
6.6 Collection<Podnik> vysledek = podnikDAO.getPodniky();
6.7 +
6.8 + for (Podnik p : vysledek) {
6.9 + System.out.println("FOTKY:" + p.getFotky());
6.10 + }
6.11 +
6.12 +
6.13 return vysledek;
6.14 }
6.15
7.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
7.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Fotka.java Sun Mar 14 01:05:35 2010 +0100
7.3 @@ -0,0 +1,24 @@
7.4 +package cz.frantovo.nekurak.dto;
7.5 +
7.6 +public class Fotka {
7.7 +
7.8 + /** číslo odpovídá názvu souboru na disku (+přípona) */
7.9 + private int id;
7.10 + private String popis;
7.11 +
7.12 + public int getId() {
7.13 + return id;
7.14 + }
7.15 +
7.16 + public void setId(int id) {
7.17 + this.id = id;
7.18 + }
7.19 +
7.20 + public String getPopis() {
7.21 + return popis;
7.22 + }
7.23 +
7.24 + public void setPopis(String popis) {
7.25 + this.popis = popis;
7.26 + }
7.27 +}
8.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java Sat Mar 13 23:51:43 2010 +0100
8.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/dto/Podnik.java Sun Mar 14 01:05:35 2010 +0100
8.3 @@ -2,6 +2,8 @@
8.4
8.5 import java.io.Serializable;
8.6 import java.util.Date;
8.7 +import java.util.HashSet;
8.8 +import java.util.Set;
8.9
8.10 /**
8.11 * Podnik – hospoda, bar, kavárna…
8.12 @@ -18,6 +20,7 @@
8.13 private int cisloPopisne;
8.14 private String mesto;
8.15 private String spravce;
8.16 + private Set<Fotka> fotky = new HashSet<Fotka>();
8.17
8.18 public Podnik() {
8.19 }
8.20 @@ -98,4 +101,12 @@
8.21 public void setSpravce(String spravce) {
8.22 this.spravce = spravce;
8.23 }
8.24 +
8.25 + public Set<Fotka> getFotky() {
8.26 + return fotky;
8.27 + }
8.28 +
8.29 + public void setFotky(Set<Fotka> fotky) {
8.30 + this.fotky = fotky;
8.31 + }
8.32 }
9.1 --- a/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Sat Mar 13 23:51:43 2010 +0100
9.2 +++ b/java/nekurak.net-lib/src/cz/frantovo/nekurak/ejb/PodnikRemote.java Sun Mar 14 01:05:35 2010 +0100
9.3 @@ -16,5 +16,5 @@
9.4 public void zalozPodnik(Podnik p);
9.5
9.6 public void upravPodnik(Podnik p);
9.7 -
9.8 +
9.9 }
10.1 --- a/java/nekurak.net-web/web/WEB-INF/casti/uvod.jsp Sat Mar 13 23:51:43 2010 +0100
10.2 +++ b/java/nekurak.net-web/web/WEB-INF/casti/uvod.jsp Sun Mar 14 01:05:35 2010 +0100
10.3 @@ -35,6 +35,11 @@
10.4 <!-- Hlasování a graf -->
10.5 <div class="hlasovani">
10.6 <nk:hlasovani podnik="${p.id}"/>
10.7 +
10.8 + <c:forEach var="fotka" items="${p.fotky}">
10.9 + <p>${fotka.id} | ${fotka.popis}</p>
10.10 + </c:forEach>
10.11 +
10.12 </div>
10.13
10.14 <!-- Fotky podniku -->