-->
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.  [ 4 posts ] 
Author Message
 Post subject: Composite primary key with @Embeddable throws exception
PostPosted: Mon Apr 07, 2008 5:57 am 
Newbie

Joined: Tue Oct 02, 2007 10:42 am
Posts: 10
All,
I am using Hibernate 3 with Oracle 9 database and am using Hibernate annotations. My problem is that I have to query a legacy database table that does not have a primary key. So I am trying to create a composite primary key out of several columns.
On reading the hibernate documentation at :
http://www.hibernate.org/hib_docs/annot ... e/#d0e1662
I found that this can be done using the @Embeddable annotation.
I tried that and it works only if I do not use any columns from the primary key class in the HQL. If I do use a column from the Primary key class a QuerySyntaxException exception is thrown.

My Mapping class is as follows:
Code:
@Entity
public class Address {
   
    @EmbeddedId
    private addressPK AddressKey;

    @Column(name="DEPARTMENT")
   private String department;

    // more columns and their getters and setters

}

My Primary key class is as follows:
Code:
@Embeddable
public class AddressPK implements Serializable
{

    private String building;
    // more columns and their getters and setters
}

If the following HQL is used, it works:
Code:
from Address address where address.department='aaa'


If the following HQL is used, an Exception is thrown.
Code:
from Address address, AddressKey key where key.building='bb'

The exception thrown is as follows:
Code:
org.hibernate.hql.ast.QuerySyntaxException: AddressPK is not mapped [from test.Address address, AddressPK key where key.building='bb']


Another thing to mention is i have also created an enty for the primary key class in the hibernate.cfg.xml. So the mapping section now looks as follows:
Code:
   <mapping class="test.PostalAddress"/>
   <mapping class="test.PostalAddressPK"/>


Can anyone help? Is there something I am doing wrong?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 9:16 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
Yes you are. The HQL snippet
Code:
from Address address, AddressKey key

joins the entities of type Address with the entitities of type AddressKey, but AddressKey is not an entity.

Instead, you want to take an address ("from Address address")
and refer to its AddressKey property ("address.AddressKey"). Your corrected query therefore is:

Code:
from Address address where address.AddressKey.building='bb'


Read up on the from-clause in the hibernate reference manual for more information.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 9:21 am 
Newbie

Joined: Tue Oct 02, 2007 10:42 am
Posts: 10
That worked! I cant believe i had been missing something so obvious :(

Thanks very much anyways!!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 07, 2008 9:24 am 
Beginner
Beginner

Joined: Wed Nov 21, 2007 8:04 am
Posts: 27
You're welcome :-)


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