-->
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.  [ 1 post ] 
Author Message
 Post subject: HQL question: deleting orphaned collection elements
PostPosted: Tue Nov 22, 2005 12:34 pm 
Newbie

Joined: Tue Nov 22, 2005 11:57 am
Posts: 5
Hello!

Suppose you have the following two legacy tables:

Code:
CREATE TABLE dvds (
  dvdid    INTEGER NOT NULL PRIMARY KEY,
  name     VARCHAR(100),
  language VARCHAR(10),
  price    DECIMAL(9,2),
  movieId  INTEGER NOT NULL   # <--- not unique!
);

CREATE TABLE moviepics (
  movieId INTEGER NOT NULL,
  idx     SMALLINT UNSIGNED NOT NULL,
  url     VARCHAR(255) NOT NULL
);


and this Hibernate mapping:

Code:
<hibernate-mapping>
  <class name="DVD" table="dvds">
    <id name="dvdId"/>
    <property name="name"/>
    <property name="language" />
    <property name="price" />
    <property name="movieId" />
    <list name="moviepics" table="moviepics" >
      <key foreign-key="movieId" column="movieId" />
      <list-index column="idx" />
      <element column="url" type="java.lang.String"/>
    </list>
  </class>
</hibernate-mapping>


So far so good.

Now, ( let's say after some legacy update operation that deletes Dvds but does not touch the moviepics table)
you want to delete all orphaned moviepics that are no longer referenced from the dvds table.

In SQL you could do something like

Code:
DELETE moviepics FROM moviepics LEFT JOIN dvds USING (movieId) WHERE dvds.movieId IS NULL;


What would be the corresponding HQL?

Your help is greatly appreciated.

Cheers!
-- spunky


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

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.