Search code examples
javahibernatejpa-2.0hbm2ddl

How can I specify the generated column name in the DDL for my derived identifier with JPA2?


We have codes in a database that have a compound identifier (here category+code), and code parameters in another table that have the same two identifiers + a parameter name (so category+code+name)

We use @IdClass for the identifiers. Here is the code:

Code.java

@Entity
@IdClass(CodeId.class)
public class Code implements Serializable {

    @Id
    private String category;

    @Id
    private String code;

    private Date validFrom;
    private Date validUntil;

    public String getCategory() { return category; }
    public String getCode() { return code; }
    public Date getValidFrom() { return validFrom; }
    public Date getValidUntil() { return validUntil; }
}

CodeId.java

public class CodeId implements Serializable {

    private String category;
    private String code;

    public CodeId() {
    }

    public CodeId(String category, String code) {
        this.category = category;
        this.code = code;
    }

    public String getCategory() { return category; }
    public String getCode() { return code; }
}

CodeParam.java

@Entity
@IdClass(CodeParamId.class)
public class CodeParam implements Serializable {

    @Id
    @ManyToOne
    private Code code;

    @Id
    private String name;

    private String value;

    public Code getCode() { return code; }
    public String getName() { return name; }
    public String getValue() { return value; }
}

CodeParamId.java

public class CodeParamId implements Serializable {

    private CodeId code;
    private String name;

    public CodeParamId() {
    }

    public CodeParamId(CodeId code, String name) {
        this.code = code;
        this.name = name;
    }

    public CodeId getCode() { return code; }
    public String getName() { return name; }
}

So, this works quite good. The problem we are having is for our unit tests, when we generate a HSQLDB in-memory DB and create the schema using the entities, using hbm2ddl. Here is the generated DDL:

create table Code (
    category varchar(255) not null,
    code varchar(255) not null,
    validFrom timestamp,
    validUntil timestamp,
    primary key (category, code)
)

create table CodeParam (
    name varchar(255) not null,
    code_category varchar(255),
    code_code varchar(255) not null,
    primary key (code_category, code_code, name)
)

alter table CodeParam 
    add constraint FK_mvkuf50rko4mipw1sb7r9yidk 
    foreign key (code_category, code_code) 
    references Code

How can I change the names generated for the derived identifiers in the CodeParam table (here code_category and code_code) ?

I need to have "category" instead of "code_category" and "code" instead of "code_code".

I tried overriding the @ManyToOne association using @AssociationOverride but couldn't find anything that did that.

I'm also assuming this would be easier using @EmbeddedId and the likes, but I need to know if it is possible using @IdClass this way.


Solution

  • Add this to private Code code in CodeParam

    @JoinColumns({ 
        @JoinColumn(referencedColumnName = "category", name = "category"),   
        @JoinColumn(referencedColumnName = "code", name = "code") })