Hi at all,
i have a problem with my application. There is a ManyToMany Connection between table Quelle and Autor. I would like to print these contents in a pdf document by using hibernate and spring. The problem is, that i could'nt print the <Autor>-List (e.g. name) in the pdf. Here is the Code of the Java Beans.
Code:
package om;
// imports
import java.util.HashSet;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import org.apache.jasper.tagplugins.jstl.core.ForEach;
@Entity
@Table(name = "tbl_quelle")
public class Quelle {
// declarations
@Id
@GeneratedValue
@Column(name = "pk_quelle")
private int id;
private String typ;
private String titel_URL;
private int erscheinungsjahr;
private String verlag;
@OneToMany(mappedBy = "quelle")
private Set<Zitat> zitat;
@ManyToMany(targetEntity = om.Standort.class,
// cascade={CascadeType.PERSIST},
fetch = FetchType.LAZY)
@JoinTable(
name="tbl_quelle_standort",
joinColumns={@JoinColumn(name="quell_id")},
inverseJoinColumns={@JoinColumn(name="standort_id")})
private Set<Standort> standort;
@ManyToMany(targetEntity = om.Autor.class,
// cascade={CascadeType.PERSIST},
fetch = FetchType.EAGER)
@JoinTable(
name="tbl_quelle_autor",
joinColumns={@JoinColumn(name="quell_id")},
inverseJoinColumns={@JoinColumn(name="autor_id")})
private Set<Autor> autor;
//constructors
public Quelle() {}
public Quelle(String n, String k, int s, String v, Set<Autor> autor) {
typ = n;
titel_URL = k;
erscheinungsjahr = s;
verlag = v;
setAutor(autor);
}
public Quelle(String n, String k, int s, String v) {
typ = n;
titel_URL = k;
erscheinungsjahr = s;
verlag = v;
}
public Quelle(String n) {
typ = n;
}
// methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTyp() {
return typ;
}
public void setTyp(String typ) {
this.typ = typ;
}
public String getTitel_URL() {
return titel_URL;
}
public void setTitel_URL(String titelURL) {
titel_URL = titelURL;
}
public int getErscheinungsjahr() {
return erscheinungsjahr;
}
public void setErscheinungsjahr(int erscheinungsjahr) {
this.erscheinungsjahr = erscheinungsjahr;
}
public String getVerlag() {
return verlag;
}
public void setVerlag(String verlag) {
this.verlag = verlag;
}
public Set<Zitat> getZitat() {
return zitat;
}
public void setZitat(Set<Zitat> zitat) {
this.zitat = zitat;
}
public Set<Standort> getStandort() {
return standort;
}
public void setStandort(Set<Standort> standort) {
this.standort = standort;
}
public Set<Autor> getAutor() {
return autor;
}
public void setAutor(Set<Autor> autor) {
this.autor = autor;
}
}
and the Autor bean:
Code:
package om;
//imports
import java.util.Date;
import java.util.Set;
import javax.persistence.*;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
@Entity
@Table(name = "tbl_autor")
public class Autor {
// declarations
@Id
@GeneratedValue
@Column(name = "pk_autor")
private int id;
private String name;
private String vorname;
private String ort;
// @OneToMany(mappedBy = "autor")
// private Set<Zitat> zitate;
@ManyToMany(mappedBy = "autor", fetch = FetchType.LAZY)
private Set<Quelle> quelle;
//constructors
public Autor(String nn, String vn, String o) {
name = nn;
vorname = vn;
ort = o;
}
public Autor() {
}
public Autor(String nn) {
name = nn;
}
// methods
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getVorname() {
return vorname;
}
public void setVorname(String vorname) {
this.vorname = vorname;
}
public String getOrt() {
return ort;
}
public void setOrt(String ort) {
this.ort = ort;
}
}
i'm using a data transfer obect:
Code:
package dto;
import java.util.Iterator;
import om.Autor;
import om.Quelle;
public class QuelleDTO implements Comparable<QuelleDTO>{
private int typ;
private String zeile1;
private String zeile2;
private String typS;
private String autorS;
@Override
public int compareTo(QuelleDTO o) {
// return this.getTypS().compareTo(o.getTypS());
return this.zeile1.compareTo(o.zeile1);
}
public QuelleDTO(Quelle aq) {
typ = aq.getId();
typS = aq.getTyp();
zeile1 = aq.getAutor() + aq.getTitel_URL() + " (" + aq.getErscheinungsjahr() + ")";
zeile2 = aq.getVerlag();
}
}
public String getAutorS() {
return autorS;
}
public void setAutorS(String autorS) {
this.autorS = autorS;
}
public QuelleDTO(){
}
public int getTyp() {
return typ;
}
public void setTyp(int typ) {
this.typ = typ;
}
public String getZeile1() {
return zeile1;
}
public void setZeile1(String zeile1) {
this.zeile1 = zeile1;
}
public String getZeile2() {
return zeile2;
}
public void setZeile2(String zeile2) {
this.zeile2 = zeile2;
}
public String getTypS() {
return typS;
}
public void setTypS(String typS) {
this.typS = typS;
}
}
and a dao object to print the it on pdf.
Code:
package dao;
import om.Quelle;
import om.Autor;
import dao.QuelleDao;
import dto.FahrtPersonenDTO;
import dto.QuelleDTO;
import feature.DB2PDF;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Set;
import java.util.TreeSet;
import java.util.List;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.pdf.PdfWriter;
public class PDFDao extends HibernateDaoSupport {
public QuelleDao quelledao;
public List<Quelle> alteQuelle;
// public AutorDao autordao;
public TreeSet<QuelleDTO> dtoquellen;
public QuelleDTO hilf;
// public static final String RESULT = "/Verzeichnis.pdf";
public final static Logger LOGGER = Logger
.getLogger(DB2PDF.class.getName());
// helping variables für Gruppenwechsel
public int h1 = 0;
public int h2 = 0;
public int h3 = 0;
public String autortest;
public PDFDao() {
}
public void setQuelledao(QuelleDao quelledao) {
this.quelledao = quelledao;
}
public void erzeugepdf(String pfad) {
alteQuelle = quelledao.findAll();
dtoquellen = new TreeSet<QuelleDTO>();
for (Quelle q : alteQuelle) {
hilf = new QuelleDTO(q);
dtoquellen.add(hilf);
}
// step 1
Document document = new Document();
try {
// step 2
PdfWriter.getInstance(document, new FileOutputStream(pfad));
// step 3
document.open();
// step 4
Paragraph p, d;
String s, t;
Chunk c;
p = new Paragraph(40);
d = new Paragraph(20);
// Formatierung und Inhalt des Verzeichnisses
Font bold = new Font(Font.HELVETICA, 11, Font.BOLD);
Font italic = new Font(Font.HELVETICA, 17, Font.ITALIC);
Font header = new Font(Font.HELVETICA, 20, Font.BOLD);
// Header
document.add(new Paragraph(50, "Literaturverzeichnis", header));
// Auslesen der Quellen mit Gruppenwechsellogik
for (QuelleDTO q : dtoquellen) {
if (q.getTypS().equals("Buch")) {
switch (h1) {
case 0:
document.add(new Paragraph(50, "Bücher", italic));
h1 = 1;
break;
case 1:
// do nothing
break;
}
s = q.getZeile1();
document.add(new Paragraph(autortest));
document.add(new Paragraph(30,s, bold));
t = q.getZeile2();
document.add(new Paragraph(20,t));
}
if (q.getTypS().equals("Fachzeitschrift")) {
switch (h2) {
case 0:
document.add(new Paragraph(50, "Fachzeitschrift",
italic));
h2 = 1;
break;
case 1:
// do nothing
break;
}
s = q.getZeile1();
document.add(new Paragraph(30, s, bold));
t = q.getZeile2();
document.add(new Paragraph(20,t));
}
if (q.getTypS().equals("Internetquelle")) {
switch (h3) {
case 0:
document.add(new Paragraph(50, "Internetquelle", italic));
h3 = 1;
break;
case 1:
// do nothing
break;
}
s = q.getZeile1();
document.add(new Paragraph(30, s, bold));
t = q.getZeile2();
document.add(new Paragraph(20,t));
}
}
document.close();
} catch (IOException e) {
LOGGER.error("IOException: ", e);
} catch (DocumentException e) {
LOGGER.error("DocumentException: ", e);
}
// try // try statement
// {
// Runtime.getRuntime().exec(
// "rundll32 url.dll,FileProtocolHandler "
// + "c:\\temp/Verzeichnis.pdf");
// } catch (Exception e) // catch any exceptions here
// {
// System.out.println("Error" + e); // print the error
// }
}
}
The pdf is successfully generated with the Quellen. But now i want to integrate the author name (Autor) to the corresponding books. How can i realize this? I tried a lot. E. g. (aq.getAutor())
But the problem is that the output in the pdf file is like: om.autor@8492. I have already tried it with to iterate the list. But it does'nt fix.
Thanks for Help!