membersound
membersound

Reputation: 86747

How to set foreign key contraint name generated by hibernate on a linking table?

I want to let hibernate create sql base on the following mapping:

@Entity
public class MyBaseEntity {
    @EmbeddedId
    private MyEmbeddable id;

    @ManyToMany
    @JoinTable(name = "mybaseentity_mymode", //sets the linking table name
    joinColumns = {
        @JoinColumn(name = "fk_param1", referencedColumnName = "param2"), //@Id param
        @JoinColumn(name = "fk_pram2", referencedColumnName = "param2")}, //@Id param
            foreignKey = @ForeignKey(name = "fk_myembeddable_id")) //the contraint name
    private Set<MyModes> modes;
}

@Embeddable
public class MyEmbeddable {
    private String param1;
    private String param2;
}

@Entity
public class MyMode {
    @Id
    private long id;

    private String code;
}

Each MyBaseEntity can have multiple modes. And the same MyMode can be linked to multiple base entities.

Question: how can I define the generated name of CONSTRAINT and UNIQUE inside the autogenerated linking table?

So far I used @JoinTable and two @JoinColumn definitions to set the constraint of the @EmbeddedId reference. But the foreign key constraint name reference to MyMode entity is still autogenerated.

Resulting sql so far with auto generated names:

CREATE TABLE public.mybaseentity_mymode
(
  mybaseentity_param1 character varying(255) NOT NULL,
  mybaseentity_param2 character varying(255) NOT NULL,
  modes_id bigint NOT NULL,
  CONSTRAINT mybaseentity_mymode_pkey PRIMARY KEY (mybaseentity_param1, mybaseentity_param2, modes_id),
  CONSTRAINT fk9tr9k9gtbqcpvoxfv2hqop6ta FOREIGN KEY (modes_id)
      REFERENCES public.mymode (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION,
  CONSTRAINT fk_myembeddable_id FOREIGN KEY (fk_param1, fk_param2)
      REFERENCES public.mybaseentity (param1, param2) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION
)

Upvotes: 2

Views: 1849

Answers (2)

membersound
membersound

Reputation: 86747

Solution as follows: the 2nd constraint is set by using the inverse* parameters.

@ManyToMany
@JoinTable(name = "mybaseentity_mymode", //sets the linking table name
    joinColumns = {
        @JoinColumn(name = "fk_param1", referencedColumnName = "param2"), //@Id param   
        @JoinColumn(name = "fk_pram2", referencedColumnName = "param2")}, //@Id param
        foreignKey = @ForeignKey(name = "fk_myembeddable_id"), //the contraint name
    inverseJoinColumns = @JoinColumn(name = "fk_mode", referencedColumnName = "id"),
    inverseForeignKey = @ForeignKey(name = "fk_mode_id"))
private Set<MyModes> modes;

Upvotes: 1

Naresh Joshi
Naresh Joshi

Reputation: 4547

We define custom ForeignKey constraints by following way You can use @ForeignKey(name="something_something") for this purpose.

And you are defining @OneToOne, @ManyToOne mappings you can also use @ForeignKey embedded in @JoinColumn like this:

@JoinColumn(name = "your_id", foreignKey = @ForeignKey(name = "something_something"))

And for @ManyToMany relations you can use uniqueConstraints, foreignKey and inverseForeignKey embedded in @JoinTable

However, I am not sure it will work the way you want to define your constraints.

Upvotes: 4

Related Questions