-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL Problem
PostPosted: Wed Jun 18, 2008 3:32 am 
Newbie

Joined: Wed Jun 18, 2008 2:48 am
Posts: 3
Hi guys,

I am trying to generate the HQL for the following SQL:

Code:
select distinct groupmembers.mapkey
from Group_members groupmembers, Group_members b
where groupmembers.group_id = b.group_id
and b.mapkey = 'some value'


Now the problem I am having is that groupmembers is a Map object in the group bean containing a string and a boolean and the above SQL does comparisons on just the String (mapkey).

We have used the members map object in other HQL statements by using
Code:
indices(group.members)
.

So far I have tried using functions such as 'some elements', 'all elements' and 'indices' to name a few but cant get anything to work, each throwing an exception as useless as the next.

Can anyone suggest how to create the HQL???

Thanks in advance, Scott

P.S. Am using hibernate 3.1.3.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 7:58 am 
Newbie

Joined: Wed Jun 11, 2008 6:36 am
Posts: 10
i would suggest to add in your mapping two columns rerpresting you mapkey elements

<property name="readOnlyMapKey1" column="mapkey1" insert=false update=false/>
<property name="readOnlyMapKey2" column="mapkey2" insert=false update=false/>

as you can notice this is only a read only value. and then write your HQL query romaly.

from group g
where g.groupmembers.readOnlyMapKey1 = :mapkey1 and g.groupmembers.readOnlyMapKey2 = :mapkey2


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 4:39 pm 
Beginner
Beginner

Joined: Tue Dec 12, 2006 6:43 am
Posts: 32
Location: London
Hi,
Can you send the mapping files.


Regards,

_________________
Alan Mehio
London
UK


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 4:41 pm 
Newbie

Joined: Wed Jun 11, 2008 6:36 am
Posts: 10
yes i can but first send me your DB schema !!..


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 18, 2008 9:00 pm 
Newbie

Joined: Wed Jun 18, 2008 2:48 am
Posts: 3
Hi guys,

I dont actually have any mapping files, we use annotations to automatically create the tables. Here is the group bean if it helps.

Code:
import java.io.Serializable;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.Map;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

import org.hibernate.annotations.CollectionOfElements;

@Entity
public class Group implements Serializable
{
   /**
    * Serializable ID
    */
   private static final long serialVersionUID = 1L;

   /**
    * Hibernate primary key
    */
   private long id;
   
   /**
    * Name of the Group
    */
   private String name;
   
   /**
    * Description of the Group
    */
   private String description;
   
   /**
    * Owner of the Group
    */
   private String owner;
   
   /**
    * Time that group was created
    */
   private Timestamp creationTime = new Timestamp(System.currentTimeMillis());

   /**
    * The type of group
    */
   private GroupType groupType;

   /**
    * Group
    * This key value pair, is the member and true of false to indicate if the User is visible.
    * If the Boolean part of the HashMap (the value) is TRUE then the User (the Key) is visible to the group,
    * otherwise the User is NOT visible.
    */
   public Map<String, Boolean> members = new HashMap<String, Boolean>();
   
   /**
    * Default Constructor.
    *
    */
   public Group()
   {
      super();
      members = new HashMap<String, Boolean>();
   }
   
   
   /**
    * Constructor for Creating groups
    * PRECONDITIONS: None of the parameters can be NULL.
    * @param owner
    * @param name
    * @param description
    * @param groupType
    */
   public Group(String owner, final String name, String description, final GroupType groupType)
   {
      super();
      if(StringUtils.isBlank(owner))
      {
         throw new IllegalArgumentException("Group owner must not be NULL");
      }
      if(StringUtils.isBlank(name))
      {
         throw new IllegalArgumentException("Group name must not be NULL");
      }
      if(groupType == null)
      {
         throw new IllegalArgumentException("Group TYPE must not be NULL");   
      }
      if(description == null)
      {
         description = "";
      }
      this.owner = owner;
      this.name = name;
      this.description = description;
      this.groupType = groupType;
      this.members.put(owner, Boolean.TRUE); // add the owner to the members list
   }
   

   // Getter and Setter Methods ///////////////////////////////////////////////////////
   
   public Timestamp getCreationTime()
   {
      return creationTime;
   }

   public void setCreationTime(final Timestamp creationTime)
   {
      if(creationTime == null)
      {
         throw new IllegalArgumentException("Group creationTime must not be NULL");   
      }
      this.creationTime = creationTime;
   }

   public String getOwner()
   {
      return owner;
   }

   public void setOwner(final String owner)
   {
      if(StringUtils.isBlank(owner))
      {
         throw new IllegalArgumentException("Group owner must not be NULL");
      }
      this.owner = owner;
   }

   public String getDescription()
   {
      return description;
   }

   public void setDescription(String description)
   {
      if(description == null)
      {
         description = "";
      }
      this.description = description;
   }

   public GroupType getGroupType()
   {
      return groupType;
   }

   public void setGroupType(final GroupType groupType)
   {
      if(groupType == null)
      {
         throw new IllegalArgumentException("Group TYPE must not be NULL");   
      }
      this.groupType = groupType;
   }

   @Id
   @GeneratedValue
   public long getId()
   {
      return id;
   }

   public void setId(final long id)
   {
      this.id = id;
   }

   /**
    * Get list of members who are in this group.
    * @return
    */
   @CollectionOfElements(fetch = FetchType.EAGER)
   public Map<String, Boolean> getMembers()
   {
      return members;
   }

   public void setMembers(final Map<String, Boolean> members)
   {
      if(members == null)
      {
         throw new IllegalArgumentException("Group Members must not be NULL");   
      }
      this.members = members;
   }

   /**
    * Name of the group
    * @return
    */
   @Column(nullable = false, unique = true)
   public String getName()
   {
      return name;
   }

   public void setName(final String name)
   {
      if(StringUtils.isBlank(name))
      {
         throw new IllegalArgumentException("Group name must not be NULL");
      }
      this.name = name;
   }
   
   /**
    * String Method for Group
    */
   public String toString()
   {
      String retString = name + " : " + description;
      return retString;      
   }   
   
}


So this code creates a group table and a group_members table which links to the group table via the group_id.

Hope that helps.

Thanks, Scott.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jun 20, 2008 1:44 am 
Newbie

Joined: Wed Jun 18, 2008 2:48 am
Posts: 3
Any suggestions???


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