-->
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.  [ 9 posts ] 
Author Message
 Post subject: Automatic Deletion + Notification
PostPosted: Fri Mar 10, 2006 8:41 pm 
Beginner
Beginner

Joined: Thu Dec 01, 2005 1:09 pm
Posts: 33
Hello,

I have a table IMAGES and several other tables which reference it using many-to-one:



Code:
<class name="Image" table="IMAGES">
    <id name="ID" column="ID" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <property name="Filename" column="FILENAME" type="String" />

    <!-- more properties -->
</class>

<class name="Movie" table="MOVIES">
    <id name="ID" column="ID" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <many-to-one name="Image" column="IMAGE_ID" class="Image" />

    <!-- more properties -->
</class>

<class name="News" table="NEWS">
    <id name="ID" column="ID" type="Int32" unsaved-value="0">
        <generator class="native" />
    </id>

    <many-to-one name="Image" column="IMAGE_ID" class="Image" />

    <!-- more properties -->
</class>

<!-- more classes, including some more that reference Image -->


I've got two questions:

1)
How can I be notified when an Image is about to be deleted, so that I can delete the image from the filesystem?

2)
Usually every Image should only be referenced by exactly one Movie/News object, so I could have the image deleted automatically (using "cascade=delete", if I got that right?).
If it makes things easier and if you tell me how, I could add a database rule for this.

But what if several elements were allowed to reference one image? (I'm not sure yet whether this would be useful at all.) Is there a way to automatically have an image deleted (both from the database and the filesystem) if it is no longer referenced? I don't want other objects to be deleted when I delete an image.

Which of the two options would you suggest? Which is easier to implement?

Thank you in advance.

GD


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 7:53 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Hi,

Frist, in which way do you want to be notified? do you wanna receive an email or a message on you mobil :) ?

You could implement a listerner which does listen to your "removeImage" Method or any other methodes where your image could be deleted. That actually depends on the model you use in your application. But that could be tricky when you use the cascade="delete" function of hibernate....
You can use the build in functionality of your dbms to watch and administrate your references to you image inside the dbms but that depends on your database. MySQL 4 doesn't provide this feature in contrast to the 5.0 version.
One possiblity is not to delete your images rather mark it as deleted so you could recover it for a certain time. And periodically remove the marked images from your db. In this case you could use a Proxy or a observer which observes the changes on your "isMarkedAsDeleted" attribut an could notify you. .....just a suggestion.


simon


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 8:14 am 
Beginner
Beginner

Joined: Thu Dec 01, 2005 1:09 pm
Posts: 33
Hi Simon,

I'm German too, but I'll continue in English since this is the English forum.

simonwillnauer wrote:
You could implement a listerner which does listen to your "removeImage" Method or any other methodes where your image could be deleted.


I won't delete images by hand. I don't quite understand what the listener would listen for.

simonwillnauer wrote:
That actually depends on the model you use in your application.


In what way?

simonwillnauer wrote:
You can use the build in functionality of your dbms to watch and administrate your references to you image inside the dbms but that depends on your database. MySQL 4 doesn't provide this feature in contrast to the 5.0 version.


I use MSSQL.

simonwillnauer wrote:
One possiblity is not to delete your images rather mark it as deleted so you could recover it for a certain time.


I find that kind of unclean.

From you reply I figure it probably would be best to only allow image to have exactly one parent. That would mean "one-to-one", right?
How would I make MSSQL verify this?
Actually the GUI automatically creates a new image for every new object, so practically there should never be more than one reference to an image.

What do you think?

Marvin


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 8:35 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Quote:
I won't delete images by hand. I don't quite understand what the listener would listen for.

Listerner listening for a delete action of a image. but if you use a "cascade" I guess you need to build your listener into hibernate. so you could provide a single interface to delete your images and trigger your filesystem via the interface. Try to check out what the event system can do for you ---> http://www.hibernate.org/hib_docs/v3/reference/en/html_single/#events

Quote:
In what way?


For ex. if you use a dao-pattern you might have a ImageDAO with a method
"public void removeImage(Long id) throws DAOException".

If you use a DomainModel you might have your action methodes in your Entity and you use a cascade="delete"
So it depends on your model.
Quote:
I use MSSQL.

So in MSSql you can put freign key constraint on the references to you images. Just an additional security...
Quote:
Actually the GUI automatically creates a new image for every new object, so practically there should never be more than one reference to an image.

I don't know what you are doing with your images but you can build many-to-man relations to the pictures but you have to figure out if there any references to it before you remove it from your filesystem.

Whats about saving your image right in the database? Is that worth considering?

simon :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 8:57 am 
Beginner
Beginner

Joined: Thu Dec 01, 2005 1:09 pm
Posts: 33
simonwillnauer wrote:
Listerner listening for a delete action of a image.


The link you posted looked very promising!

simonwillnauer wrote:
but if you use a "cascade" I guess you need to build your listener into hibernate.


Why? Listeners aren't notified if the object is deleted by cascading?

simonwillnauer wrote:
For ex. if you use a dao-pattern you might have a ImageDAO with a method
"public void removeImage(Long id) throws DAOException".

If you use a DomainModel you might have your action methodes in your Entity and you use a cascade="delete"
So it depends on your model.


I'm not quite sure what model I use. I use NHibernate, the access to it is encapsulated in a class DataAccess, which again is encapsulated in another class. Clients don't see the DataAccess class, they call the encapsulating service class instead.
The service class uses interfaces, DataAccess uses implementing classes.

simonwillnauer wrote:
So in MSSql you can put freign key constraint on the references to you images. Just an additional security...


Do you happen to know how?

simonwillnauer wrote:
I don't know what you are doing with your images but you can build many-to-man relations to the pictures but you have to figure out if there any references to it before you remove it from your filesystem.


I won't need many-to-many, it's usually one-to-one (one object, one image), many-to-one (several objects reference one image). The latter is very unlikely to happen.

simonwillnauer wrote:
Whats about saving your image right in the database? Is that worth considering?


This is commonly discouraged. Also I don't want to change all my code now.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 9:09 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Quote:
Why? Listeners aren't notified if the object is deleted by cascading?

If you use the hibernate event api you don't have a problem with that. So I guess you will do use a pre/postEvent.... so don't worry about that. I wrote that just in case you wanna implement your own notifier....


Quote:
Do you happen to know how?

Code:
ALTER TABLE `tablename` ADD (FOREIGN KEY (`imageId`) REFERENCES `imagetable` (`idcolumn`))



Quote:
This is commonly discouraged. Also I don't want to change all my code now.


I wouldn't do that either!! :)


simon


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 9:13 am 
Beginner
Beginner

Joined: Thu Dec 01, 2005 1:09 pm
Posts: 33
simonwillnauer wrote:
Quote:
Why? Listeners aren't notified if the object is deleted by cascading?

If you use the hibernate event api you don't have a problem with that.


Great!

simonwillnauer wrote:
Code:
ALTER TABLE `tablename` ADD (FOREIGN KEY (`imageId`) REFERENCES `imagetable` (`idcolumn`))


I already have that, but doesn't this allow more than one object to reference one and the same image? I could put a UNIQUE contraint on the foreign key table, but this would still allow the image to be referenced from other tables.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 9:23 am 
Regular
Regular

Joined: Tue Mar 07, 2006 11:18 am
Posts: 54
Location: Berlin
Quote:
I already have that, but doesn't this allow more than one object to reference one and the same image? I could put a UNIQUE contraint on the foreign key table, but this would still allow the image to be referenced from other tables.


I guess not :), AFAIK there is no such mechanism build in dbms...

I hope I could help you a bit further.... at least with the event system!

simon :)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Mar 11, 2006 10:04 am 
Beginner
Beginner

Joined: Thu Dec 01, 2005 1:09 pm
Posts: 33
Yes, thank you very much :).


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