-->
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.  [ 9 posts ] 
Author Message
 Post subject: Annotations and use of PK problems
PostPosted: Sun Sep 07, 2008 10:51 am 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Hi,
I am attempting to use annotations in my spring-hibernate application. I am having some difficulties with the primary key.

The problem is with the MyKey which represent the id of the object. The database field which is bankaccountID is of type int but i cant get it to retrieve the data, it fails with the following error.

Quote:
[JDBCExceptionReporter] Unknown column 'this_.primaryKey' in 'field list'


I have found that the problem is with

Code:
@Column(name = "BankAccountID")
   public MyKey getPrimaryKey() {
      return primaryKey;
   }


Changing it so it uses int works.
Code:
@Column(name = "BankAccountID")
   public Integer getPrimaryKey() {
      return primaryKey;
   }


I have enclosed the full code listing below.

The following is the Entity class

Code:
@SuppressWarnings("serial")
@Entity
@Table(name = "bankaccount")
public class BankTOAnonImpl implements Serializable {
   MyKey primaryKey;
   String description;
   Date transactionDate;

   public BankTOAnonImpl() {
   }

   @Column(name = "description")
   public String getDescription() {
      return description;
   }

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

   @Column(name = "transactionDate")
   public Date getTransactionDate() {
      return transactionDate;
   }

   public void setTransactionDate(Date transactionDate) {
      this.transactionDate = transactionDate;
   }

   [b]
        @Id
   @Column(name = "BankAccountID")
   public MyKey getPrimaryKey() {
      return primaryKey;
   }
        [/b]

   /**
    * @param primaryKey the primaryKey to set
    */
   public void setPrimaryKey(MyKey primaryKey) {
      this.primaryKey = primaryKey;
   }
}

The primary key object is defined as

Code:

@Embeddable
public class MyKey implements Serializable {
   private int primaryKey;

   /**
    * @return the primaryKey
    */
   public int getPrimaryKey() {
      return primaryKey;
   }

   /**
    * @param primaryKey the primaryKey to set
    */
   public void setPrimaryKey(int primaryKey) {
      this.primaryKey = primaryKey;
   }
}


The hibernate config file mapping is specified as

<mapping class="MyKey"/>
<mapping class="BankTOAnonImpl"/>

Thanks in advance


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 11:34 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
hi,
to use an embeddable object as PK you should use

Code:
@EmbeddedId


btw if you really only need an "int" in the embedded class, why not to use an Integer directly? Just wondering.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 11:51 am 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Hi,
I have tried using EmbeddedId instead of Id, but got a different error

[JDBCExceptionReporter] Unknown column 'this_.primaryKey' in 'field list'

The actual field on the DB is BankAccountID but its called primaryKey on the java object.

The reason why we use an object to represent the key is because we may need to mask the key in future, it gives us more options to represent the key.

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 11:53 am 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
I opened http://opensource.atlassian.com/projects/hibernate/browse/ANN-772
but I should have asked you which versions you are using.
Could you please tell me?

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 11:58 am 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
I am using hibernate-annotations-3.4.0.GA and hibernate-3.2.6.ga

Thanks


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 07, 2008 1:34 pm 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Good news, I have managed to figure it out.

Code:
@EmbeddedId
   @Column(name = "BankAccountID")
   @AttributeOverrides( {
      @AttributeOverride(name = "primaryKey", column = @Column(name = "BankAccountID"))
   })


The retreive at least is now working fine.

Thanks for your help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 2:36 pm 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
A follow up on this, does hibernate annotations support interfaces. For example if I changed my Return type so it returns an interface for example

Code:

public IMyKey getPrimaryKey() {
      return primaryKey;
   }

public interface IMyKey{
int getID();
void setID(int id);

}



I got an error

"Cannot instantiate abstract class or interface IMyKey "


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 09, 2008 7:51 pm 
Hibernate Team
Hibernate Team

Joined: Fri Oct 05, 2007 4:47 pm
Posts: 2536
Location: Third rock from the Sun
Hi,
yes of course, but you need to tell hibernate what to actually instantiate to set the value:

see
Code:
@Target


or
Code:
targetEntity

on the reference docs.

_________________
Sanne
http://in.relation.to/


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 10, 2008 6:44 am 
Newbie

Joined: Tue Jan 24, 2006 7:50 pm
Posts: 11
Great, thats worked. I tried to use @Target before but it wouldn't work, turns out even though i downloaded the latest annotations my ide wasn't picking the jar up. I corrected this and now it works fine now

Thanks again


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