0

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.

Yotus
  • 264
  • 1
  • 4
  • 17
  • Is it not possible to use **@Column(name = "category")** – sgpalit Aug 27 '15 at 06:54
  • On `private Code code` in CodeParam ? Because it is not possible to mix @ManyToOne with @Column – Yotus Aug 27 '15 at 06:59
  • 1
    looked at @JoinColumns? http://stackoverflow.com/questions/25264994/manytoone-and-onetoone-relations-with-embeddedid – sgpalit Aug 27 '15 at 07:05
  • Yeah, I tried everything I could think of yesterday.. or so I thought. Tried again and it does indeed work with the correct annotation : `@JoinColumns({ @JoinColumn(referencedColumnName = "category", name = "category"), @JoinColumn(referencedColumnName = "code", name = "code") })` – Yotus Aug 27 '15 at 07:22
  • I am happy for you that you have solved your problem :) – sgpalit Aug 27 '15 at 07:24
  • Thank you for making me try that again ! You could write the answer and I will gladly accept it :) – Yotus Aug 27 '15 at 07:29

1 Answers1

1

Add this to private Code code in CodeParam

@JoinColumns({ 
    @JoinColumn(referencedColumnName = "category", name = "category"),   
    @JoinColumn(referencedColumnName = "code", name = "code") })
sgpalit
  • 2,676
  • 2
  • 16
  • 22
  • 1
    Just to add on this answer: the reason it didn't work for me at first was because I thought I add to put "code.category" in the referencedColumnName instead of just "category" – Yotus Aug 27 '15 at 07:45