-->
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.  [ 4 posts ] 
Author Message
 Post subject: Bi-directional one-to-one relationship, how?
PostPosted: Fri Mar 19, 2004 5:06 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
I have a one-2-one relationship between map a and map b:

Map A:
<hibernate-mapping package="test">
<class name="Artist" table="artisttbl" lazy="true">
<id name="ID" column="artist_id">
<generator class="sequence">
<param name="sequence">artist_seq_id</param>
</generator>
</id>
.............
</class>
</hibernate-mapping>

Map B:
<hibernate-mapping package="test">
<class name="ArtistImages" table="artistimagestbl">
<id name="ID" column="artist_id">
<generator class="foreign">
<param name="property">artist</param>
</generator>
</id>
......................
<one-to-one name="artist" class="Artist" constrained="true" cascade="save-update"/>
</class>
</hibernate-mapping>

With the maps specified as they are now I can:

Artist artist = new Artist();
ArtistImages newArt = new ArtistImages();

1. Add records (with key reference):

newArt.setArtist(artist);
artist.addNewArtist(artist);
newArt.addNewArtistImages(newArt);

2. Delete referenced key records:

newArt.deleteArtistImages(id) - (artistImage and artist record with referenced key deleted);

But what I want to be able to do is delete referenced key records by saying:

artist.deleteArtist(id) - (artist and artistImage record with referenced key deleted)

How do I do this, as I have read and tried many things and none of them has worked!?

I am using Hibernate 2.1.1

Peter


Top
 Profile  
 
 Post subject:
PostPosted: Fri Mar 19, 2004 6:00 am 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
Can you show the real code doing the delete and the way you fill the objects

_________________
Emmanuel


Top
 Profile  
 
 Post subject: Emmanuel
PostPosted: Fri Mar 19, 2004 6:40 am 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
my java code to delete on the ArtistImages side is :

public void deleteArtistImages(int artist_id) {

Integer artistimagesID = new Integer(artist_id);

try {

HibernateUtil Util = new HibernateUtil();
Session session = Util.currentSession();

Transaction transaction = session.beginTransaction();

session.delete("from artistimages in class ArtistImages where artistimages.ID=?",artistimagesID,Hibernate.INTEGER);
transaction.commit();

Util.closeSession();

}
catch (HibernateException e) {
throw new RuntimeException("Exception in Hibernate:: " + e.getMessage(), e);
}
}

The delete code on the Artist side is exactly the same except any reference to ArtistImages is changed to Artist. In my calling code I simply instantiate the ArtistImages object:

ArtistImages newArt = new ArtistImages();

simply calling the delete method, newArt.deleteArtistImages(artist_id), deletes both the artistimeages record and the artist record with the same key. How do I amend my maps so that I can do:

Artist artist = new Artist();
artist.deleteArtist(artist_id);

which would delete both the artist record as well as the artistimages record with the same id?

Peter


Top
 Profile  
 
 Post subject: All...
PostPosted: Fri Mar 19, 2004 12:20 pm 
Regular
Regular

Joined: Mon Jan 19, 2004 10:39 pm
Posts: 84
Location: Nottingham, England
has been resolved now. Thanks for your help.

Peter


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