-->
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: Annotation problem with attributeOverride and muticolumn PK
PostPosted: Fri Sep 12, 2008 1:58 pm 
Newbie

Joined: Tue Apr 08, 2008 6:37 am
Posts: 3
Hi everybody,


I have numerous tables in the format

Code:
pk1 | pk2 | pk3 | yyy


all of the fields are numeric, the primary key is pk1, pk2 and pk3 .. the only difference between the tables is the column name of pk3

This table layout is fixed and can not be changed

I want to map these tables with JPA (or Hibernate Annotations if it is not possible with JPA alone). For this I created the following structure:


Code:
@MappedSuperclass
public class Base {
   
   private Key key;
   
   private int yyy;

   @EmbeddedId
   public Key getKey() {
       return key;
    }

   public void setKey(Key key) {
       this.key = key;
    }

   public int getYyy() {
       return yyy;
    }

   public void setYyy(int yyy) {
       this.yyy = yyy;
    }
   
   public void setPk1(int pk1) {
      key.setPk1(pk1);
   }
   
   @Transient
   public int getPk1() {
      return key.getPk1();
   }

   public void setPk2(int pk2) {
      key.setPk2(pk2);
   }
   
   @Transient
   public int getPk2() {
      return key.getPk2();
   }   

}


Code:
@Embeddable
@MappedSuperclass
public class Key {
   
   
   private int pk1;
   
   private int pk2;
   
   private int generic_pk3;

   
   @Column(name = "pk1", nullable = false, insertable = false, updatable = false)
   public int getPk1() {
       return pk1;
    }

   public void setPk1(int pk1) {
       this.pk1 = pk1;
    }

   @Column(name = "pk2", nullable = false, insertable = false, updatable = false)
   public int getPk2() {
       return pk2;
    }

   public void setPk2(int pk2) {
       this.pk2 = pk2;
    }

   @Column(nullable = false, insertable = false, updatable = false)
   public int getGeneric_pk3() {
       return generic_pk3;
    }

   public void setGeneric_pk3(int generic_pk3) {
       this.generic_pk3 = generic_pk3;
    }
   
   @Override
   public int hashCode() {
      return pk1 * pk2 * generic_pk3;
   }
   
   @Override
   public boolean equals(Object obj) {
      if (obj == this) return true;
      if (obj == null || obj.getClass() != this.getClass()) return false;
      Key key = (Key) obj;
      if (key.getPk1() == getPk1() && key.getPk2() == getPk2() && key.getGeneric_pk3() == getGeneric_pk3())
         return true;
      else
       return false;      
   }

}


Code:
@Entity(name = "some_table_name")
public class FirstBaseImpl extends Base {
   
   
   public FirstBaseImpl() {
      setKey(new Key());
   }

   @Override
   @AttributeOverride(name = "generic_pk3", column = @Column(name = "pk3"))
   public Key getKey() {
      return super.getKey();
   }
   
   public void setPk3(int pk3) {
      getKey().setGeneric_pk3(pk3);
   }
   
   @Transient
   public int getPk3() {
      return getKey().getGeneric_pk3();
   }

}


To execute this code I use Hibernate EntityManager and just say persist() on one example object.

The error I get says it could not find a column called "generic_pk3", which is obvious as the correct column is called "pk3". I tried to archive this by using the AttributeOverride annotation which seems to be ignored.

Can sombody help me here?

thanks,

Christoph


Top
 Profile  
 
 Post subject:
PostPosted: Wed Sep 17, 2008 7:37 am 
Newbie

Joined: Tue Apr 08, 2008 6:37 am
Posts: 3
Hi,

I am still struggling with this one.

Maybe I am also doing something conceptionally wrong? Can you point me to my mistake? Is this easier solved with some other OR structure?

many thanks to any responses,

Christoph


Top
 Profile  
 
 Post subject: Ditto
PostPosted: Mon Oct 13, 2008 5:48 pm 
Newbie

Joined: Tue Apr 25, 2006 4:04 pm
Posts: 4
We're having the same problem.

AttributeOverride isn't exactly being ignored. It appears that if the AttributeOverride is for an identifier column, the Hibernate generated query still tries to fetch the superclass defined column for the identifier in addition to the overridden column name.

I'm hoping to find an answer on this soon.

_________________
--
jj


Top
 Profile  
 
 Post subject:
PostPosted: Wed Dec 03, 2008 9:00 pm 
Newbie

Joined: Wed Dec 03, 2008 8:55 pm
Posts: 2
Try pushing the @EmbeddedId down to the sub classes and then annotating the superclass key getter as @Transient.

@MappedSuperclass
public class Base {
...

@Transient
public Key getKey() {
return key;
}

...
}

public class FirstBaseImpl extends Base {
....


@Override
@AttributeOverride(name = "generic_pk3", column = @Column(name = "pk3"))
@EmbeddedId
public Key getKey() {
return super.getKey();
}
....
}


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.