views:

38

answers:

0

I need some help in defining the Hibernate annotation for a foreign key referencing a composite PK table.

My composite PK table is FqTankType and I have defined it as

private FqTankTypeId      id;

@EmbeddedId
@AttributeOverrides( {
        @AttributeOverride(name = "tankTypeCd", column = @Column(name = "TANK_TYPE_CD", nullable = false, length = 10)),
        @AttributeOverride(name = "localeId", column = @Column(name = "LOCALE_ID", nullable = false)) })
public FqTankTypeId getId() {
    return this.id;
}

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

and FqTankTypeId is defined as

private String tankTypeCd;
private int localeId;
@Column(name = "TANK_TYPE_CD", nullable = false, length = 10)
public String getTankTypeCd() {
    return this.tankTypeCd;
}

public void setTankTypeCd(String tankTypeCd) {
    this.tankTypeCd = tankTypeCd;
}

@Column(name = "LOCALE_ID", nullable = false)
public int getLocaleId() {
    return this.localeId;
}

public void setLocaleId(int localeId) {
    this.localeId = localeId;
}

I want to refer to this table from another table, FqTank..I tried like this

private FqTankType fqTankType;

private FqTankType fqTankType;

@ManyToOne(fetch = FetchType.LAZY, targetEntity=FqTankType.class)
@JoinColumns ( {
    @JoinColumn(nullable=false, name="tankTypeCd", insertable=false, updatable=false, referencedColumnName="TANK_TYPE_CD"),
    @JoinColumn(nullable=false, name="localeId", insertable=false, updatable=false, referencedColumnName="LOCALE_ID")})
public String getTankTypeCd() {
    return tankTypeCd;
}

public void setTankTypeCd(String tankTypeCd) {
    this.tankTypeCd = tankTypeCd;
}

If I use like above, my hibernate DAo find query gives me error like.

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Unknown column 'fqtank0_.localeId' in 'field list'
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)

I even tried replacing with

private String tankTypeCd;

@Column(name = "TANK_TYPE_CD")
public String getTankTypeCd() {
    return tankTypeCd;
}

public void setTankTypeCd(String tankTypeCd) {
    this.tankTypeCd = tankTypeCd;
}

But, I get the following error during server load

org.hibernate.MappingException: Foreign key (FKDA0027DE595FB799:fq_tank [TANK_TYPE_CD])) must have same number of columns as the referenced primary key (fq_tank_type [LOCALE_ID,TANK_TYPE_CD])
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275) 

Any help will be highly appreciated..

Thanks..

The table definitions are

CREATE TABLE fq_tank (
TANK_CD INT(10) NOT NULL AUTO_INCREMENT,
SITE_KEY INT(38) NOT NULL,
TANK_NAME VARCHAR(100) NOT NULL,
TANK_TYPE_CD VARCHAR(10) NOT NULL,
PRIMARY KEY (TANK_CD),
KEY FQ_TANK_FK1 (SITE_KEY),
KEY FQ_TANK_FK2 (TANK_TYPE_CD),
CONSTRAINT FQ_TANK_FK1 FOREIGN KEY (SITE_KEY) REFERENCES fq_sites (SITE_KEY),
CONSTRAINT FQ_TANK_FK2 FOREIGN KEY (TANK_TYPE_CD) REFERENCES fq_tank_type (TANK_TYPE_CD),
) ENGINE=INNODB DEFAULT CHARSET=utf8

CREATE TABLE fq_tank_type (
TANK_TYPE_CD VARCHAR(10) NOT NULL,
LOCALE_ID INT(38) NOT NULL,
TANK_TYPE_NAME VARCHAR(100) NOT NULL,
PRIMARY KEY (TANK_TYPE_CD, LOCALE_ID),
KEY FQ_TANK_TYPE_FK1 (LOCALE_ID),
CONSTRAINT FQ_TANK_TYPE_FK1 FOREIGN KEY (LOCALE_ID) REFERENCES fq_locale (LOCALE_ID)
) ENGINE=INNODB DEFAULT CHARSET=utf8