hi,
I have problem with bidirectional association. In my batabase are two tree tables adress, beleg and belegpos and fallowing association :
adress 1: n beleg
beleg 1: n belegpos (bidirectional )
i have method with give me list with adress, beleg and belegpos fields back. but i canno't recive fields from belegpos. here is my code and mapping files:
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="model">
<class
name="Beleg"
table="beleg"
>
<meta attribute="sync-DAO">false</meta>
<id
name="belegid"
type="string"
column="belegid"
>
<generator class="native"/>
</id>
<property
name="Adressid"
column="adressid"
type="string"
not-null="true"
length="6"
/>
<property
name="Datum"
column="datum"
type="date"
not-null="false"
/>
<property
name="Netto"
column="netto"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Mwst"
column="mwst"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Rabatt"
column="rabatt"
type="integer"
not-null="false"
length="10"
/>
<property
name="Zbid"
column="zbid"
type="integer"
not-null="false"
length="10"
/>
<property
name="Vortext"
column="vortext"
type="binary"
not-null="false"
/>
<property
name="Nachtext"
column="nachtext"
type="binary"
not-null="false"
/>
<property
name="Bemerkung"
column="bemerkung"
type="string"
not-null="false"
length="255"
/>
<property
name="Ausgewaehlt"
column="ausgewaehlt"
type="integer"
not-null="false"
length="10"
/>
<property
name="Bezahlt"
column="bezahlt"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Oztyp"
column="oztyp"
type="string"
not-null="false"
length="12"
/>
<property
name="Status"
column="status"
type="integer"
not-null="true"
length="10"
/>
<set name="belegpos"
inverse="true">
<key column="belegid"/>
<one-to-many class="Belegpos"/>
</set>
<many-to-one name="adressen"
column="adressid"
class="Adressen"
insert="false"
update="false">
</many-to-one>
</class>
</hibernate-mapping>
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="model">
<class
name="Belegpos"
table="belegpos"
>
<meta attribute="sync-DAO">false</meta>
<id
name="Id"
type="integer"
column="id"
>
<generator class="native"/>
</id>
<property
name="Belegid"
column="belegid"
type="string"
not-null="true"
length="8"
/>
<property
name="Zeile"
column="zeile"
type="integer"
not-null="true"
length="10"
/>
<property
name="Oz"
column="oz"
type="string"
not-null="false"
length="15"
/>
<property
name="Postyp"
column="postyp"
type="string"
not-null="false"
length="3"
/>
<property
name="Wg"
column="wg"
type="string"
not-null="false"
length="3"
/>
<property
name="Artnr"
column="artnr"
type="string"
not-null="false"
length="25"
/>
<property
name="Menge"
column="menge"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Kurz"
column="kurz"
type="string"
not-null="false"
length="255"
/>
<property
name="Epreis"
column="epreis"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Vpreis"
column="vpreis"
type="java.lang.Float"
not-null="false"
length="12"
/>
<property
name="Steuer"
column="steuer"
type="integer"
not-null="false"
length="10"
/>
<property
name="Me"
column="me"
type="string"
not-null="false"
length="6"
/>
<property
name="Pe"
column="pe"
type="integer"
not-null="false"
length="10"
/>
<property
name="Rabattpos"
column="rabattpos"
type="integer"
not-null="false"
length="10"
/>
<many-to-one name="beleg"
column="belegid"
class="Beleg"
insert="false"
update="false">
</many-to-one>
</class>
</hibernate-mapping>
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Beleg generated by hbm2java
*/
public class Beleg implements java.io.Serializable {
private String belegid;
private String adressid;
private Date datum;
private Float netto;
private Float mwst;
private Integer rabatt;
private Integer zbid;
private byte[] vortext;
private byte[] nachtext;
private String bemerkung;
private Integer ausgewaehlt;
private Float bezahlt;
private String oztyp;
private int status;
private Adressen adressen;
private Belegpos bp;
//beleg 1 : n Belegpos
private Set<Belegpos> belegpos=new HashSet<Belegpos>();
//private List<Belegpos> belegpos = new ArrayList<Belegpos>();
public Beleg() {
}
public Beleg(String belegid, String adressid, int status) {
this.belegid = belegid;
this.adressid = adressid;
this.status = status;
}
public Beleg(String belegid, String adressid, Date datum, Float netto,
Float mwst, Integer rabatt, Integer zbid, byte[] vortext,
byte[] nachtext, String bemerkung, Integer ausgewaehlt,
Float bezahlt, String oztyp, int status) {
this.belegid = belegid;
this.adressid = adressid;
this.datum = datum;
this.netto = netto;
this.mwst = mwst;
this.rabatt = rabatt;
this.zbid = zbid;
this.vortext = vortext;
this.nachtext = nachtext;
this.bemerkung = bemerkung;
this.ausgewaehlt = ausgewaehlt;
this.bezahlt = bezahlt;
this.oztyp = oztyp;
this.status = status;
}
//getters , setters
public class Belegpos implements java.io.Serializable {
private Integer id;
private String belegid;
private int zeile;
private String oz;
private String postyp;
private String wg;
private String artnr;
private Float menge;
private String kurz;
private Float epreis;
private Float vpreis;
private Integer steuer;
private String me;
private Integer pe;
private Integer rabattpos;
private Beleg beleg;
public Belegpos() {
}
public Belegpos(String belegid, int zeile) {
this.belegid = belegid;
this.zeile = zeile;
}
public Belegpos(String belegid, int zeile, String oz, String postyp,
String wg, String artnr, Float menge, String kurz,
Float epreis, Float vpreis, Integer steuer, String me, Integer pe,
Integer rabattpos) {
this.belegid = belegid;
this.zeile = zeile;
this.oz = oz;
this.postyp = postyp;
this.wg = wg;
this.artnr = artnr;
this.menge = menge;
this.kurz = kurz;
this.epreis = epreis;
this.vpreis = vpreis;
this.steuer = steuer;
this.me = me;
this.pe = pe;
this.rabattpos = rabattpos;
}
//getters,setters
try{
Query q= getSession().createQuery("select b from Beleg b join b.adressen join b.belegpos where b.belegid='RA004805'");
for(int i=0; i<q.list().size();i++){
Beleg beleg= (Beleg)q.list().get(i);
Belegpos bp = new Belegpos();
bp.setBeleg(beleg);
beleg.getBelegpos().add(bp);
System.out.println(beleg.getBelegid());
System.out.println(beleg.getAdressid());
System.out.println(beleg.getDatum());
System.out.println(bp.getArtnr());
System.out.println(bp.getKurz());
System.out.println(beleg.getAdressen().getStrasse());
System.out.println(beleg.getAdressen().getPlz());
System.out.println(beleg.getAdressen().getOrt());
}
}catch(HibernateException e){
rollback();
throw new HWException("Fehler" ,e);
}
by System.out.println(bp.getArtnr());
System.out.println(bp.getKurz()); I recive null. what I do wrong?