-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Records in table disappear
PostPosted: Wed Nov 15, 2006 7:15 pm 
Newbie

Joined: Wed Nov 15, 2006 7:00 pm
Posts: 3
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]


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 4:34 am 
Expert
Expert

Joined: Tue Dec 28, 2004 7:02 am
Posts: 573
Location: Toulouse, France
When do you lose all your records ? Each time you run your test class ? I guess you're hbm2ddl parameter in your hibernate.cfg.xml, which you did not provide here.

What's containing this file, so (hibernate.cfg.xml) ?

_________________
Baptiste
PS : please don't forget to give credits below if you found this answer useful :)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 16, 2006 6:05 am 
Newbie

Joined: Wed Nov 15, 2006 7:00 pm
Posts: 3
The most important code line in this file is:

Code:
<!-- <property name="hbm2ddl.auto">create</property> -->


This line is comment, so the problem I think is in other place :-p

I lose my records every time when I run this application.
(The records disappear only in OSDO table, other tables are OK).

And I have one question more: I add a not-null constraint in line (Osoba.hbm.xml file):
Code:
<element type="string" column="EMAIL" not-null="true"/>

and do something like this:
Code:
Osoba os = new Osoba();
os.setImie(imie);
os.setNazwisko(nazwisko);
os.setWiek(wiek);
os.getEmail().add(null);

session.save(os);


I though that I will get an exception, because I try to add null-value to column which have not-null constraint. But I can do this, and in my table OSOBA-EMAIL I have null values in EMAIL column. What is wrong with this code?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.