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