-->
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: How to remove items from a collection by using HQL?
PostPosted: Sun May 31, 2009 10:16 pm 
Newbie

Joined: Mon Mar 16, 2009 10:57 am
Posts: 2
Hi all,

I'm new to hibernate, trying to grasp the hibernate style of programming. I have a many to many relationship between the following entities DMap and MapablePoint:
Code:
@Entity(name = "Map")
public class DMap
{
   @ManyToMany(cascade = {CascadeType.PERSIST, CascadeType.MERGE}, fetch = FetchType.LAZY)
   //@MapKey(targetElement = String.class)
   @JoinTable(name="map_mapable",
      joinColumns=@JoinColumn(name="map_ID", referencedColumnName="ID"),
      inverseJoinColumns=@JoinColumn(name="mapable_ID", referencedColumnName="ID")
   )
   @javax.persistence.MapKey
   public Map<String, MapablePoint> getPointsByKey() { return pointsByKey; }

}


I need to delete some MapablePoints given their IDs and the map's ID. I'm able to do that by using this:

Code:
   @Transactional(propagation=Propagation.REQUIRED)
   public void disassociateMapable(Long mapId, Set<Long> mapableIds)
   {
      String s = "from Map map " +
         " left join fetch map.pointsByKey As mapPointsByKey " +
         " where map.id=:mapId ";
      Query query = _entityManager.createQuery(s);
      query.setParameter("mapId", mapId);
      
      DMap map = (DMap)query.getSingleResult();
      
      for(Long id : mapableIds)
      {
         MapablePoint p = map.getPointsByKey().remove(id);
      }
   }

In this code I load the map and all its MapablePoints in order to delete the few ones I need to delete.
I'm looking for a more elegant way to delete the specified points by using HQL. But I can not find the proper example to remove elements from a collection by using HQL.

The problem is the syntax for the HQL Delete statements requires an entity which will be deleted. But in the given case the pointsByKey collection is NOT an entity. I do not want to delete Map entity nor MapablePoint entity but the relation between them. The query should look like:
Code:
"Delete From Map.pointsByKey As mapPointsByKey " +
         " where Map.id=:mapId " +
         " And mapPointsByKey.id In (:pointIds)";

But this causes:
Code:
IllegalArgumentException: org.hibernate.hql.ast.QuerySyntaxException: Map.pointsByKey is not mapped [Delete From Map.pointsByKey As mapPointsByKey  where Map.id=:mapId  And mapPointsByKey.id In (:pointIds)]


Which is the proper syntax?

Thanks in advance
Sammy


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.