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