0

The following relationship creates a foreign key mapping

@Entity
public class Department {
   @Id
   private String name;

   //some more fields
}

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

   private String name;
   private String designation;

   @ManyToOne
   @JoinColumn(name = "fk_department_id", foreignKey = @ForeignKey(name="fk_department"))
   private Department department;
}

generates:

...CONSTRAINT fk_department FOREIGN KEY (fk_department_id) REFERENCES department (name)

Question: how can I trigger this constraint creation in hibernate without having to create the Department entity?

Eg just adding the foreign key @Id field without an explicit entity reference. But still trigger the fk on initial creation. The following is of course invalid:

   @ManyToOne
   @JoinColumn(name = "fk_department_id", foreignKey = @ForeignKey(name="fk_department"))
   private String department;

You get the intention. Is that possible?

(sidenote: I'm not interested in creating that foreign key link by startup ddl/sql statements).

2
  • 2
    So you want to create the schema from your entities, but you don't want to write your entities to match the schema? Commented Jul 19, 2018 at 11:12
  • Exactly, in this special case for that foreign table entity. Commented Jul 19, 2018 at 11:15

1 Answer 1

3

You'll have to drop @ManyToOne at least, since that's for entities.

The following should work by overriding the column definition to include the foreign key while creating it

@Column(name = "department_id", columnDefinition = "VARCHAR(255), foreign key (department_id) REFERENCES department(name)")
private String department;

Now there's only a column and a constraint defined, but no relation (as far as Hibernate knows) defined between entities.

Essentially copied from Hibernate and JPA: how to make a foreign key constraint on a String but that was darn hard to find, so I'm not just going to close this as a duplicate! ;)

Sign up to request clarification or add additional context in comments.

5 Comments

Unfortunately as a result of this, no foreign key relation is created by hibernate!
@membersound how about this?
Unfortunately I'm using mysql, and that does not support inline foreign keys directly. To quote the docs: MySQL does not recognize or support “inline REFERENCES specifications” (as defined in the SQL standard) where the references are defined as part of the column specification. Therefore the column definition would have to be as follows: @Column(name = "department_id", columnDefinition = "varchar(255), foreign key (department_id) REFERENCES department (name)"). I tried this, it worked. BUT it feels somehow very very hacky...
Well your requirements are hacky. You've already said in your question that you don't want to use either of the 2 clean ways.
Ok you're probably right. So if there is no "cleaner" way this is probably the answer. Glad it works at all!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.