Hi, How to do view a relation table one to many ?
Example:
hibernate.cfg.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/databasefilm</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
<property name="connection.pool_size">1</property>
<property name="show_sql">true</property>
<mapping resource="com/dungino/databasefilm/Books.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Autenticazione.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/DettVideo.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Film.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Genere.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Profilo.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Utenti.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Video.hbm.xml"/>
<mapping resource="com/dungino/databasefilm/Visti.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Film.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 12-feb-2012 16.14.26 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.dungino.databasefilm.Film" table="film" catalog="databasefilm">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<property name="titolo" type="string">
<column name="titolo" length="80" not-null="true" unique="true" />
</property>
<property name="attore1" type="string">
<column name="attore1" length="40" />
</property>
<property name="attore2" type="string">
<column name="attore2" length="40" />
</property>
<property name="attore3" type="string">
<column name="attore3" length="40" />
</property>
<property name="trama" type="string">
<column name="trama" not-null="true" />
</property>
<property name="regia" type="string">
<column name="regia" length="40" />
</property>
<property name="durata" type="int">
<column name="durata" not-null="true" />
</property>
<property name="anno" type="java.lang.Integer">
<column name="anno" />
</property>
<property name="disponibile" type="string">
<column name="disponibile" length="5" not-null="true" />
</property>
<property name="immagine" type="string">
<column name="immagine" length="50" />
</property>
<property name="mini" type="string">
<column name="mini" length="50" />
</property>
<property name="dataIns" type="timestamp">
<column name="data_ins" length="19" not-null="true" />
</property>
<property name="genere" type="int">
<column name="genere" not-null="true" />
</property>
<set name="videos" table="video" inverse="true" lazy="true" fetch="select">
<key>
<column name="film" not-null="true" unique="true" />
</key>
<one-to-many class="com.dungino.databasefilm.Video" />
</set>
</class>
</hibernate-mapping>
POJO File
Film.java
Code:
package com.dungino.databasefilm;
// Generated 12-feb-2012 16.14.25 by Hibernate Tools 3.4.0.CR1
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
/**
* Film generated by hbm2java
*/
public class Film implements java.io.Serializable {
private int id;
private String titolo;
private String attore1;
private String attore2;
private String attore3;
private String trama;
private String regia;
private int durata;
private Integer anno;
private String disponibile;
private String immagine;
private String mini;
private Date dataIns;
private int genere;
private Set<Video> videos = new HashSet<Video>(0);
public Film() {
}
public Film(String titolo, String trama, int durata, String disponibile,
Date dataIns, int genere) {
this.titolo = titolo;
this.trama = trama;
this.durata = durata;
this.disponibile = disponibile;
this.dataIns = dataIns;
this.genere = genere;
}
public Film(String titolo, String attore1, String attore2, String attore3,
String trama, String regia, int durata, Integer anno,
String disponibile, String immagine, String mini, Date dataIns,
int genere, Set<Video> videos) {
this.titolo = titolo;
this.attore1 = attore1;
this.attore2 = attore2;
this.attore3 = attore3;
this.trama = trama;
this.regia = regia;
this.durata = durata;
this.anno = anno;
this.disponibile = disponibile;
this.immagine = immagine;
this.mini = mini;
this.dataIns = dataIns;
this.genere = genere;
this.videos = videos;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getTitolo() {
return this.titolo;
}
public void setTitolo(String titolo) {
this.titolo = titolo;
}
public String getAttore1() {
return this.attore1;
}
public void setAttore1(String attore1) {
this.attore1 = attore1;
}
public String getAttore2() {
return this.attore2;
}
public void setAttore2(String attore2) {
this.attore2 = attore2;
}
public String getAttore3() {
return this.attore3;
}
public void setAttore3(String attore3) {
this.attore3 = attore3;
}
public String getTrama() {
return this.trama;
}
public void setTrama(String trama) {
this.trama = trama;
}
public String getRegia() {
return this.regia;
}
public void setRegia(String regia) {
this.regia = regia;
}
public int getDurata() {
return this.durata;
}
public void setDurata(int durata) {
this.durata = durata;
}
public Integer getAnno() {
return this.anno;
}
public void setAnno(Integer anno) {
this.anno = anno;
}
public String getDisponibile() {
return this.disponibile;
}
public void setDisponibile(String disponibile) {
this.disponibile = disponibile;
}
public String getImmagine() {
return this.immagine;
}
public void setImmagine(String immagine) {
this.immagine = immagine;
}
public String getMini() {
return this.mini;
}
public void setMini(String mini) {
this.mini = mini;
}
public Date getDataIns() {
return this.dataIns;
}
public void setDataIns(Date dataIns) {
this.dataIns = dataIns;
}
public int getGenere() {
return this.genere;
}
public void setGenere(int genere) {
this.genere = genere;
}
public Set<Video> getVideos() {
return this.videos;
}
public void setVideos(Set<Video> videos) {
this.videos = videos;
}
}
Video.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 12-feb-2012 16.14.26 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping>
<class name="com.dungino.databasefilm.Video" table="video" catalog="databasefilm">
<id name="id" type="int">
<column name="id" />
<generator class="identity" />
</id>
<many-to-one name="film" class="com.dungino.databasefilm.Film" fetch="select">
<column name="film" not-null="true" unique="true" />
</many-to-one>
<property name="path" type="string">
<column name="path" length="128" not-null="true" />
</property>
<set name="dettvideos" table="dettvideo" inverse="true" lazy="true" fetch="select">
<key>
<column name="id" not-null="true" />
</key>
<one-to-many class="com.dungino.databasefilm.Dettvideo" />
</set>
</class>
</hibernate-mapping>
POJO File
Video.java
Code:
package com.dungino.databasefilm;
// Generated 12-feb-2012 16.14.25 by Hibernate Tools 3.4.0.CR1
import java.util.HashSet;
import java.util.Set;
/**
* Video generated by hbm2java
*/
public class Video implements java.io.Serializable {
private int id;
private Film film;
private String path;
private Set<Dettvideo> dettvideos = new HashSet<Dettvideo>(0);
public Video() {
}
public Video(Film film, String path) {
this.film = film;
this.path = path;
}
public Video(Film film, String path, Set<Dettvideo> dettvideos) {
this.film = film;
this.path = path;
this.dettvideos = dettvideos;
}
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public Film getFilm() {
return this.film;
}
public void setFilm(Film film) {
this.film = film;
}
public String getPath() {
return this.path;
}
public void setPath(String path) {
this.path = path;
}
public Set<Dettvideo> getDettvideos() {
return this.dettvideos;
}
public void setDettvideos(Set<Dettvideo> dettvideos) {
this.dettvideos = dettvideos;
}
}
Main: HibernateLaunch.java
Code:
package main;
import java.util.Iterator;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import com.dungino.databasefilm.Autenticazione;
import com.dungino.databasefilm.Books;
import com.dungino.databasefilm.Film;
import com.dungino.databasefilm.Video;
import com.dungino.databasefilm.util.HibernateUtil;
public class HibernateLanch {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
listFilm();
}
public static void listFilm()
{
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction transaction = null;
Video video1= new Video();
try {
transaction = session.beginTransaction();
List Film = session.createQuery("from Film").list();
for (Iterator iterator =Film.iterator(); iterator.hasNext();)
{
Film film= (Film) iterator.next();
System.out.println(film.getTitolo());
System.out.println(film.getAttore1());
System.out.println(film.getAttore2());
System.out.println(film.getAttore3());
System.out.println(film.getAnno());
System.out.println(film.getTrama());
System.out.println(film.getVideos());
}
transaction.commit();
} catch (HibernateException e) {
transaction.rollback();
e.printStackTrace();
} finally {
session.close();
}
}
Output:
/*****************************************OUTPUT****************************************/
UNITED 93
Lewis Alsamari
Cheyenne Jackson
Trish Gates
2006
11 Settembre 2001. In questo tragico giorno i terroristi sconvolsero il mondo dirottando una serie di aerei e facendoli schiantare contro obiettivi prefissati. Solo uno non raggiunse il bersaglio.., questa é la storia di quel volo..
Hibernate: select videos0_.film as film1_, videos0_.id as id1_, videos0_.id as id7_0_, videos0_.film as film7_0_, videos0_.path as path7_0_ from databasefilm.video videos0_ where videos0_.film=?
[com.dungino.databasefilm.Video@bd09e8]
/****************************************************************************************/
how to do view table video ?