Designing a blog starts with its database schema. In a Spring Boot project, Hibernate can generate the tables from the entity classes, which keeps the model and schema aligned while the design is still evolving. The example model has five entities: Blog, Type, Tag, Comment, and User.








Project structure:

Add the JPA starter to pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
Then set the Hibernate options in application.properties:
jpa:
hibernate:
ddl-auto: update
show-sql: true

ddl-auto has several useful values:
create-drop: creates tables when the JPA application starts and drops them when it shuts down. Useful in tests, not typical in development.create: recreates the tables on every startup and removes existing data.update: updates table information on each run.
The entity mappings are where the relationships take shape. Blog has many-to-one links to Type and User, a many-to-many link to Tag, and one-to-many comments. Comment points back to its Blog and can have reply comments through a parent/child self-reference. Tag and Type both map back to Blog, and User owns a list of blogs.
Blog
@Entity
@Table(name = "t_blog")
public class Blog {
@Id
@GeneratedValue
private Long id;
private String title;
private String content;
private String firstPicture;
private String flag;
private Integer views;
private boolean appreciation;
private boolean shareStatement;
private boolean commentabled;
private boolean published;
private boolean recommend;
private Date createTime;
private Date updateTime;
@ManyToOne
private Type type;
@ManyToMany(cascade = {CascadeType.PERSIST})
private List<Tag> tags = new ArrayList<>();
@ManyToOne
private User user;
@OneToMany(mappedBy = "blog")
private List<Comment> comments = new ArrayList<>();
}
Comment
@Entity
@Table(name = "t_comment")
public class Comment {
@Id
@GeneratedValue
private Long id;
private String nickname;
private String email;
private String content;
private String avatar;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@ManyToOne
private Blog blog;
@OneToMany(mappedBy = "parentComment")
private List<Comment> replyComments = new ArrayList<>();
@ManyToOne
private Comment parentComment;
}
Tag
@Entity
@Table(name = "t_tag")
public class Tag {
@Id
@GeneratedValue
private Long id;
private String name;
@ManyToMany(mappedBy = "tags")
private List<Blog> blogs = new ArrayList<>();
}
Type
@Entity
@Table(name = "t_type")
public class Type {
@Id
@GeneratedValue
private long id;
private String name;
@OneToMany(mappedBy = "type")
private List<Blog> blogs = new ArrayList<>();
}
User
@Entity
@Table(name = "t_user")
public class User {
@Id
@GeneratedValue
private Long id;
private String nickname;
private String username;
private String password;
private String email;
private String avatar;
private Integer type;
@Temporal(TemporalType.TIMESTAMP)
private Date createTime;
@Temporal(TemporalType.TIMESTAMP)
private Date updateTime;
@OneToMany(mappedBy = "user")
private List<Blog> blogs = new ArrayList<>();
}
With the entities in place, configure the datasource before running the project:
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf-8
username: root
password: root

Once the application starts, Hibernate creates the tables from these mappings. The test run shows the resulting schema:

