📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Learn Spring boot at https://www.javaguides.net/p/spring-boot-tutorial.html.
Learn Hibernate at https://www.javaguides.net/p/hibernate-tutorial.html
Video Tutorial
This tutorial explained in below youtube video:Overview
Tools and Technologies used
- Spring boot 2+
- Hibernate 5+
- JDK 1.8+
- Maven 3+
- IDE - STS or Eclipse
- Spring Data JPA
- MySQL 5+
Development Steps
- Create a Spring Boot Application
- Maven pom Dependencies
- Project Structure
- Configuring the Database and Hibernate Log levels
- Defining the Domain Models
- Defining the Repositories
- Run and Test the Application
1. Create a Spring Boot Application
>> Create Spring Boot Project in Spring Tool Suite [STS]
2. Maven pom Dependencies
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
<!-- lookup parent from repository -->
</parent>
<groupId>net.javaguides</groupId>
<artifactId>springboot-hibernate-many-to-many-mapping</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot-hibernate-many-to-many-mapping</name>
<description>Demo project for Spring Boot Hibernate many to many mapping</description>
<properties>
<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>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
3. Project Structure
4. Configuring the Database and Hibernate Log levels
spring.datasource.url = jdbc:mysql://localhost:3306/demo?useSSL=false
spring.datasource.username = root
spring.datasource.password = root
## Hibernate Properties
We’ll need to configure MySQL database URL, username, and password so that Spring can establish a connection with the database on startup.
Open src/main/resources/application.properties and add the following properties to it -
# The SQL dialect makes Hibernate generate better SQL for the chosen database
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialect
# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto = update
logging.level.org.hibernate.SQL=DEBUG
5. Defining the Domain Models
Post.java
package net.javaguides.springboot.entity;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
/**
* Post domain model
* @author javaguides.net
*
*/
@Entity
@Table(name = "posts")
public class Post {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "title")
private String title;
@Column(name = "description")
private String description;
@Column(name = "content")
private String content;
@Column(name = "posted_at")
private Date postedAt = new Date();
@Column(name = "last_updated_at")
private Date lastUpdatedAt = new Date();
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "post_tags",
joinColumns = {
@JoinColumn(name = "post_id")
},
inverseJoinColumns = {
@JoinColumn(name = "tag_id")
})
private Set < Tag > tags = new HashSet < > ();
public Post() {
}
public Post(String title, String description, String content) {
super();
this.title = title;
this.description = description;
this.content = content;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getPostedAt() {
return postedAt;
}
public void setPostedAt(Date postedAt) {
this.postedAt = postedAt;
}
public Date getLastUpdatedAt() {
return lastUpdatedAt;
}
public void setLastUpdatedAt(Date lastUpdatedAt) {
this.lastUpdatedAt = lastUpdatedAt;
}
public Set < Tag > getTags() {
return tags;
}
public void setTags(Set < Tag > tags) {
this.tags = tags;
}
}
Tag.java
package net.javaguides.springboot.entity;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
/**
* Tag domain model
* @author javaguides.net
*
*/
@Entity
@Table(name = "tags")
public class Tag {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "tags")
private Set < Post > posts = new HashSet < > ();
public Tag() {
}
public Tag(String name) {
super();
this.name = name;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set < Post > getPosts() {
return posts;
}
public void setPosts(Set < Post > posts) {
this.posts = posts;
}
}
6. Defining the Repositories
PostRepository.java
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springboot.entity.Post;
@Repository
public interface PostRepository extends JpaRepository<Post, Long>{
}
TagRepository.java
package net.javaguides.springboot.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import net.javaguides.springboot.entity.Tag;
@Repository
public interface TagRepository extends JpaRepository<Tag, Long>{
}
7. Run and Test the Application
package net.javaguides.springboot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import net.javaguides.springboot.entity.Post;
import net.javaguides.springboot.entity.Tag;
import net.javaguides.springboot.repository.PostRepository;
@SpringBootApplication
public class SpringbootHibernateManyToManyMappingApplication implements CommandLineRunner {
@Autowired
private PostRepository postRepository;
public static void main(String[] args) {
SpringApplication.run(SpringbootHibernateManyToManyMappingApplication.class, args);
}
@Override
public void run(String...args) throws Exception {
Post post = new Post("Hibernate Many to Many Mapping Example with Spring Boot",
"Hibernate Many to Many Mapping Example with Spring Boot",
"Hibernate Many to Many Mapping Example with Spring Boot");
Post post1 = new Post("Hibernate One to Many Mapping Example with Spring Boot",
"Hibernate One to Many Mapping Example with Spring Boot",
"Hibernate One to Many Mapping Example with Spring Boot");
Tag springBoot = new Tag("Spring Boot");
Tag hibernate = new Tag("Hibernate");
// add tag references post
post.getTags().add(springBoot);
post.getTags().add(hibernate);
// add post references tag
springBoot.getPosts().add(post);
hibernate.getPosts().add(post);
springBoot.getPosts().add(post1);
post1.getTags().add(springBoot);
this.postRepository.save(post);
this.postRepository.save(post1);
}
}
Hi, Ramesh, May I know how to print those values in post_tags table on thymeleaf?
ReplyDeleteI tried a lot of method available but unable to get the result