-->
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.  [ 7 posts ] 
Author Message
 Post subject: how to hql query a composite primary key
PostPosted: Tue Sep 18, 2007 11:37 pm 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Hey,

My question is how to query in HQL a composite primary key using
@EmbeddedId.

I have the embeddable class ClubPk:
___________________________________________________________
@Embeddable
public class NamePk implements Serializable{

String FirstName;
String LastName;.............
___________________________________________________________

And the class where it is embedded as the composite primary key:
__________________________________________________________

public class Group implements Serializable {

NamePk CompKey;
..........

public Group(){}

@EmbeddedId
public NamePk getCompKey()
{
return this.CompKey;
} ...........
_____________________________________________________________
When I try HQL query such as :

String querystring ="from Group group where group.firstName='"+var1+"' and group.lastName='"+var2+"'";
The columns firstname is not found.

How to query fields of a composite key?


Top
 Profile  
 
 Post subject: Re: how to hql query a composite primary key
PostPosted: Wed Sep 19, 2007 2:47 am 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
illusion wrote:
Hey,

My question is how to query in HQL a composite primary key using
@EmbeddedId.

I have the embeddable class ClubPk:
___________________________________________________________
@Embeddable
public class NamePk implements Serializable{

String FirstName;
String LastName;.............
___________________________________________________________

And the class where it is embedded as the composite primary key:
__________________________________________________________

public class Group implements Serializable {

NamePk CompKey;
..........

public Group(){}

@EmbeddedId
public NamePk getCompKey()
{
return this.CompKey;
} ...........
_____________________________________________________________
When I try HQL query such as :

String querystring ="from Group group where group.firstName='"+var1+"' and group.lastName='"+var2+"'";
The columns firstname is not found.

How to query fields of a composite key?


This way worked for us:
Code:
String querystring ="from Group group where group.NamePK = ('"+var1+"','"+var2+"')";


Top
 Profile  
 
 Post subject: thanks
PostPosted: Wed Sep 19, 2007 10:36 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Hey,

Thanks, works great.


Top
 Profile  
 
 Post subject: individual field of composite key
PostPosted: Wed Sep 19, 2007 10:59 am 
Newbie

Joined: Tue Apr 11, 2006 4:57 pm
Posts: 13
Hey,

Is there a way to query individual fields of a composite key. I find with the way stated every field of the key must be specified. If I wanted to query just one field of say a 3 field key how to get around it.

thanks in advance for your assistance


Top
 Profile  
 
 Post subject: Re: individual field of composite key
PostPosted: Wed Sep 19, 2007 2:24 pm 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
illusion wrote:
Hey,

Is there a way to query individual fields of a composite key. I find with the way stated every field of the key must be specified. If I wanted to query just one field of say a 3 field key how to get around it.

thanks in advance for your assistance


Try this. Folks saying it should work.

Code:
String querystring ="from Group group where group.NamePK.FirstName = 'myname'";


Top
 Profile  
 
 Post subject: Re: individual field of composite key
PostPosted: Mon Oct 29, 2007 12:04 pm 
Newbie

Joined: Mon Oct 29, 2007 11:50 am
Posts: 5
hutorny wrote:
illusion wrote:
Hey,

Is there a way to query individual fields of a composite key. I find with the way stated every field of the key must be specified. If I wanted to query just one field of say a 3 field key how to get around it.

thanks in advance for your assistance


Try this. Folks saying it should work.

Code:
String querystring ="from Group group where group.NamePK.FirstName = 'myname'";


Hi, i'm trying to do this, but i still cant.
Look at my classes

Code:
@Embeddable
public class CustomerPK implements Serializable {

   private static final long serialVersionUID = 314150377756575581L;
   private long id;
   private long dId;
   private int wId;


   @Column(name = "C_ID", nullable = false)
   public long getId() {
      return id;
   }

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

   @Column(name = "C_D_ID", nullable = false)
   public long getDId() {
      return dId;
   }

   public void setDId(long id) {
      dId = id;
   }

   @Column(name = "C_W_ID", nullable = false)
   public int getWId() {
      return wId;
   }
                ...


Code:
@Entity
@Table(name = "CUSTOMER")
@NamedQueries({
   @NamedQuery(name = "selectAllCustomers", query = "select c from Customer c"),
   @NamedQuery(name = "countAllCustomers", query = "select count(*) from Customer c"),
   @NamedQuery(name = "deleteAllCustomers", query = "delete from Customer c"),
   @NamedQuery(name = "getCustomerByLastNameAndHomeWarehouse", query = "from " +
         "Customer c where c.cPk.dId=:dId and c.cPk.wId=:wId and c.last=:last")
})
public class Customer implements Serializable {

   
   private static final long serialVersionUID = -6640493644418267201L;
   private CustomerPK cPk;
                ...
   private String last;
                ...
   @EmbeddedId
   @AttributeOverrides({
      @AttributeOverride(name="id", column=@Column(name="C_ID", nullable = false)),
      @AttributeOverride(name="dId", column=@Column(name="C_D_ID", nullable = false)),
      @AttributeOverride(name="wId", column=@Column(name="C_W_ID", nullable = false))
   })
   public CustomerPK getCPk() {
      return cPk;
   }
                ....


When i try to obtain an EntityManagerFactory, un exception raises:
ERROR [main] (Log4JLogger.java:119) - Error in named query: getCustomerByLastNameAndHomeWarehouse
org.hibernate.QueryException: could not resolve property: cPK of: br.ufpr.***.Customer [from br.ufpr.***.Customer c where c.cPK.dId=:dId and c.cPk.wId=:wId and c.last=:last]

Does anyone knows why is this happening?

Thks.

Murilo


Top
 Profile  
 
 Post subject: Re: individual field of composite key
PostPosted: Wed Oct 31, 2007 7:57 am 
Beginner
Beginner

Joined: Mon Aug 27, 2007 8:10 am
Posts: 37
murilorl wrote:

Hi, i'm trying to do this, but i still cant.
Look at my classes
Murilo


Try to remove alias 'c'

Code:
   @NamedQuery(name = "getCustomerByLastNameAndHomeWarehouse", query = "from " +
         "Customer where cPk.dId=:dId and cPk.wId=:wId and last=:last")


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