Java.Hibernate.Middle.What will happen if i will not write @JoinColumn on the owning side ?

Short Answer

If you don’t specify @JoinColumn on the owning side (usually the @ManyToOne side), JPA will still create a foreign key column, but it will use a default naming convention → typically <entity_field>_id.
This can lead to unexpected or unclear column names in your database schema.


🔎 Detailed Explanation

🔹 What JPA does by default

  • When you write:
@ManyToOne
private ParentEntity parent;

and don’t add @JoinColumn, JPA will auto-generate the foreign key column name.

🔹 Default column name

  • By default, it’s:
<field_name>_id

So if your field is named parent, Hibernate will create a column named parent_id.

🔹 Potential issues without @JoinColumn
❗ If you want a specific name (e.g., company standards or integration with existing DB schema), you must explicitly set @JoinColumn(name=”…”).

❗ Without it, you risk:

  • Inconsistent or unexpected column names across entities.
  • Database schema not matching naming conventions → surprises in SQL scripts or during schema migration.
  • Harder-to-read or maintain database tables.

🔹 Example without @JoinColumn

@Entity
public class ChildEntity {
    @ManyToOne
    private ParentEntity parent; // creates parent_id column by default
}

Results in:

alter table child_entity add column parent_id bigint;

🔹 Example with @JoinColumn

@Entity
public class ChildEntity {
    @ManyToOne
    @JoinColumn(name = "my_custom_parent_fk")
    private ParentEntity parent;
}

Results in:

alter table child_entity add column my_custom_parent_fk bigint;

✅ Clear, consistent column naming.

📌 Key Takeaways

✅ Without @JoinColumn, JPA creates the foreign key column but names it automatically → usually <field>_id.
✅ Always use @JoinColumn(name=...) if you want control over column names, or to match existing schemas.
✅ Explicitly defining @JoinColumn improves readability, maintainability, and portability.

This entry was posted in Без рубрики. Bookmark the permalink.

Leave a Reply

Your email address will not be published.