前期准备
准备搭建一个Springboot+JPA(Hibernate)作为例子
pom配置
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
application.properties配置
spring.datasource.url=jdbc:mysql://localhost:3306/demo
spring.datasource.username=root
spring.datasource.password=yourpwd
spring.jpa.properties.hibernate.hbm2ddl.auto=update
spring.jpa.properties.hibernate.show_sql=true
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect
一对一
用户User
@Entity
public class User implements Serializable {
@Id
@GeneratedValue
private Long userId;
private String userName;
//省略Get/Set方法
}
操作日志OperationLog
@Entity
public class OperationLog implements Serializable {
@Id
@GeneratedValue
private Long id;
private String content;
@OneToOne
@JoinColumn(name = "op_id",referencedColumnName = "userId",foreignKey = @ForeignKey(value = ConstraintMode.NO_CONSTRAINT))
@NotFound(action = NotFoundAction.IGNORE)
private User user;
//省略Get/Set方法
}
Hibernate的建表语句
CREATE TABLE `operation_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`content` varchar(255) DEFAULT NULL,
`op_id` bigint(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `user` (
`user_id` bigint(20) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
一对多
书籍Book
@Entity
public class Book implements Serializable {
/**
* 逻辑主键,自增长
*/
@Id
@GeneratedValue
private Long bookId;
/**
* 书籍名称
*/
private String name;
//省略Get/Set方法
}
作者Author
@Entity
public class Author implements Serializable {
/**
* 逻辑主键,自增长
*/
@Id
@GeneratedValue
private Long id;
/**
* 作者名称
*/
private String name;
/**
* 作者名下的书籍
*/
@OneToMany(fetch=FetchType.EAGER)
@JoinTable(name = "author_book",joinColumns = @JoinColumn(name="author_id"),inverseJoinColumns = @JoinColumn(name = "book_id"),foreignKey = @ForeignKey(ConstraintMode.NO_CONSTRAINT))
@NotFound(action=NotFoundAction.IGNORE)
private List<Book> books;
//省略Get/Set方法
}
Hibernate的建表语句
CREATE TABLE `author` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `author_book` (
`author_id` bigint(20) NOT NULL,
`book_id` bigint(20) NOT NULL,
UNIQUE KEY `UK_bfff0qy5yokmeel0q0e10msv7` (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `book` (
`book_id` bigint(20) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`book_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
总结
1、无外键关联的关键是@ForeignKey(value = ConstraintMode.NO_CONSTRAINT)
2、@NotFound(action=NotFoundAction.IGNORE)
是用来防止无关联的数据的时候抛出异常