Skip to content

lewis2951/hibernate-table-relationship

Repository files navigation

多表关系映射

目录说明

  • simple: 单表操作,通过 Spring Data JPA 进行 CRUD 操作。
  • one-to-one: 一对一
  • one-to-one-shared-pk: 一对一,子表主外键共享
  • one-to-many: 一对多
  • many-to-many: 多对多
  • many-to-many-extra-columns: 多对多,关联表含扩展字段

以上目录,各自独立。

环境约定

  • JDK 1.8
  • Intellij IDEA (Community Edition)
  • Maven 3.6.0
  • Spring Boot 2.1.4.RELEASE
  • Spring Data JPA, H2, Lombok

数据库

当前使用 H2 内存数据库,配置方式如下:

## jpa settings
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.show-sql=true

如需使用实体数据库,可参考如下:

## datasource settings
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.password=demo
spring.datasource.url=jdbc:h2:tcp://127.0.0.1/~/jpa-h2
spring.datasource.username=demo
## jpa settings
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.show-sql=true

如需使用 H2 Web Console 访问数据库,配置如下:

## datasource settings
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.password=demo
spring.datasource.url=jdbc:h2:mem:jpa-h2
spring.datasource.username=demo
## h2 web console settings
spring.h2.console.enabled=true
## jpa settings
spring.jpa.generate-ddl=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=false
spring.jpa.show-sql=true

About

在 Hibernate 中如何配置多表关系映射,包括:一对一、一对多和多对多。

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages