Hi! I am very new to Hiberbate. I made simple application. But in this simple application, every time when I want to list all values from table, this table is truncated. Here is my code:
Osoba.java:
Code:
package dokumenty;
import java.util.*;
public class Osoba {
private Long id;
private int wiek;
private String imie;
private String nazwisko;
private Set dokumenty = new HashSet();
private Set email = new HashSet();
public Set getEmail() {
return email;
}
public void setEmail(Set email) {
this.email = email;
}
public Set getDokumenty() {
return dokumenty;
}
public void setDokumenty(Set dokumenty) {
dokumenty = dokumenty;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getImie() {
return imie;
}
public void setImie(String imie) {
this.imie = imie;
}
public String getNazwisko() {
return nazwisko;
}
public void setNazwisko(String nazwisko) {
this.nazwisko = nazwisko;
}
public int getWiek() {
return wiek;
}
public void setWiek(int wiek) {
this.wiek = wiek;
}
public Osoba() {}
}
Dokument.java:
Code:
package dokumenty;
import java.util.*;
public class Dokument {
private Long id;
private String title;
private String number;
//private Set wlasciciel = new HashSet();
public Dokument() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Set getWlasciciel() {
return wlasciciel;
}
public void setWlasciciel(Set wlasciciel) {
this.wlasciciel = wlasciciel;
}
}
Dokument.hbm.xml:
Code:
<hibernate-mapping schema="dokumenty">
<class mutable="false" name="dokumenty.Dokument" table="DOKUMENT">
<id name="id" column="DOKUMENT_ID">
<generator class="native"/>
</id>
<property name="title" column="NAZWA"/>
<property name="number" column="NUMER"/>
<set name="wlasciciel" table="OSDO" inverse="true">
<key column="DOKUMENT_ID"/>
<many-to-many column="OSOBA_ID" class="dokumenty.Osoba"/>
</set>
</class>
</hibernate-mapping>
Osoba.hbm.xml:
Code:
<hibernate-mapping schema="dokumenty">
<class name="dokumenty.Osoba" table="OSOBA">
<id name="id" column="OSOBA_ID">
<generator class="native"/>
</id>
<property name="wiek"/>
<property name="imie"/>
<property name="nazwisko"/>
<set name="dokumenty" table="OSDO">
<key column="OSOBA_ID"/>
<many-to-many column="DOKUMENT_ID" class="dokumenty.Dokument"/>
</set>
<set name="email" table="OSOBA_EMAIL">
<key column="OSOBA_ID"/>
<element type="string" column="EMAIL"/>
</set>
</class>
</hibernate-mapping>
Then, first I create instance of class Osoba, then instance of class Dokument, then I add Dokument instace to Osoba instance:
Code:
private void addOsobaToDokument(Long eventId, Long personId) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
Osoba os = (Osoba) session.load(Osoba.class, personId);
Dokument dok = (Dokument) session.load(Dokument.class, eventId);
os.getDokumenty().add(dok);
session.getTransaction().commit();
}
Everything is ok. But, when I do this:
Code:
private List listDoks() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List result = session.createQuery("from Osoba").list();
session.getTransaction().commit();
return result;
}
then all my record from "OSDO" table are lost. Why? And how can i avoid this?
Please, help.
Best regards![/code]