-->
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.  [ 2 posts ] 
Author Message
 Post subject: join table map
PostPosted: Sat Jan 10, 2009 8:47 am 
Newbie

Joined: Sat Jan 10, 2009 8:19 am
Posts: 4
Hi

I have two tables:

[Person]
id
name
adress
phone

[List]
id
l_name

I need to create by hibernate join table Lists where I can have FK.

[Lists]
id
fk_id_list
fk_id_person

for example:

[Person]
1 | Andy | St. Joan St. | +48 00 000000
2 | Mark | Uknow | +46 00 000000

[List]
1 | Default
2 | Secret
3 | Private

[Lists]
1 | 1 | 1
2 | 1 | 2
3 | 2 | 2

So on list Default I have 2 persons: Andy and Mark, on list Secret only Mark and so on.

How I have to map it in hbm.xml and put get-ters and set-ters in .java files to for example:
View who is on list Default, and if I delete list Default then all persons from list Default should be also deleted.

Is it many-to-one mapping?

thanks a lot for any help
mk


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 12, 2009 1:56 pm 
Newbie

Joined: Sat Jan 10, 2009 8:19 am
Posts: 4
I done it by:

lista.hbm.xlm

<class name="Lista" table="lista">
<id name="id">
<generator class="native"/>
</id>
<property column="nazwa" length="100" name="nazwa" not-null="true"/>
<property column="uwagi" length="255" name="uwagi" not-null="false"/>
<set name="adresaci" table="listy" inverse="true" cascade="all">
<key column="id_lista"/>
<many-to-many column="id_adr" class="Adresaci" not-found="ignore"/>
</set>
</class>
</hibernate-mapping>

adresaci.hbm.xlm

<class name="Adresaci" table="adresaci">
<id name="id">
<generator class="native"/>
</id>
<property column="przedrostek" length="35" name="przedrostek" not-null="false"/>
<property column="nazwa_os" length="65" name="nazwa_os" not-null="false"/>
<property column="nazwa_ins" length="150" name="nazwa_ins" not-null="false"/>
<property column="kod" length="6" name="kod" not-null="true"/>
<property column="poczta" length="58" name="poczta" not-null="true"/>
<property column="ulica" length="80" name="ulica" not-null="true"/>
<property column="przyrostek" length="35" name="przyrostek" not-null="false"/>
<set name="listy" table="listy" lazy="true" cascade="all">
<key column="id_adr"/>
<many-to-many column="id_lista" class="Lista"/>
</set>
</class>

This test class creating new list and adding all persons (table Adresaci)to the List.

Session session=factory.openSession();
Transaction tx=session.beginTransaction();
Lista list = new Lista();
list.setNazwa(jTextField7.getText());
list.setUwagi(jTextField5.getText());
Query q = session.createQuery("from Adresaci");
for (Adresaci adr : (List<Adresaci>) q.list())
{
list.getAdresaci().add(adr);
adr.getListy().add(list);
}

tx.commit();
session.close();

All greate but If I try delete record from Lista, cascade delete not working:

SEVERE: BŁĄD: update or delete on table "lista" violates foreign key constraint "fk623639b6f4a8341" on table "listy"
Szczegóły: Key (id)=(105) is still referenced from table "listy".

What I'm doing wrong?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 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.