Specifying table and field names for join tables in Spring Boot/Hibernate/JPA -
i using spring boot/jpa backed mysql. have db provisioned , plan on using hibernate.hbm2ddl.auto = validate (so hibernate doesn't try create db me, instead uses 1 supply , "validates" against entities define in code).
i have several many-to-many relationships entities, such book , author:
// groovy pseudo code! 1 book can have multiple authors, , // single author can have written many books. @entity class book { @id @generatedvalue(strategy=generationtype.identity) long id @column(name="title") string title @manytomany list<author> authors } @entity class author { @id @generatedvalue(strategy=generationtype.identity) long id @column(name="name") string name @manytomany list<book> books } here, classes represented following tables:
[books] id bigint unsigned not null auto_increment # primary key title varchar(50) not null [authors] id bigint unsigned not null auto_increment # primary key name varchar(100) not null [books_x_authors] books_x_authors_id bigint unsigned not null auto_increment # primary key book_id # fk on books author_id # fk on authors where books_x_authors many-to-many or "crosswalk" table. thats db naming convention , want stick if @ possible. how configure spring boot/hibernate use books_x_authors join/crosswalk table instead of expecting perhaps table adhering own conventions?
under manytomany specify join table need
@manytomany @jointable(name="books_x_authors", joincolumns={@joincolumn(name="author_id"}, inversejoincolumns={@joincolumn(name="book_id")} ) list<book> books
Comments
Post a Comment