In Hibernate, to specify a column for joining association, @JoinColumn annotation in used, for example like this:
@ManyToOne
@JoinColumn(name="address_id")
public Address getAddress() {
return address;
}
In most cases, name of the column is snaked-cased class name plus _id
. So it is reasonable to expect from Hibernate to derive it automatically (as it is done, for example, in Django's ORM). But is such behavior implemented somehow?
It is not necessary, JPA follows convention over configuration principle which means there are allways some default values that you can override with annotations.
In case of @JoinColumn
, the default column name is generated like this: <field_name>_<id_column_name>
field_name
is address
in your case, and id_column_name
is referring to the related entity's id, which is id
. Thus, you get address_id
by default.
It is not necessary to have @JoinColumn
annotation. You can always override it. If you won't provide it in your code then Hibernate will automatically generate one for you i.e. default name for your column.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With