This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to name ManyToMany table and ManyToOne column?  RSS feed

 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,


When @ManyToMany annotation is used between 2 tables, table1 and table2, a new table is created with default name as table1_table2.

Is there a possibility of changing this default name to a custom one?


The same question comes with the @ManyToOne attribute inside the class. Is it possible to provide the custom name of such column?




Thanks in advance
 
Ismael Upright
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Found solution...


Renaming the column with @ManyToOne annotation:

Use @JoinColumn(name="My_Custom_Column_Name") as a second annotation before attribute.



Renaming the column with @ManyToMany annotation:

Use @JoinTable(name="My_Custom_Table_Name") as a second annotation before attribute.
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed, the @JoinColumn attribute allows you to specify a name. Of course, the annotation has to match your database, so be careful!

-Cameron McKenzie
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!