All type of Mapping in Spring boot

Welcome to Mapping in Spring boot

Here in this post, you will learn how to make the complex mapping design of models like the below types

  1. 1-1
  2. 1-M
  3. M-1
  4. M-N 

 The most interesting thing in spring boot is that you dont need to make one more model in Many to Many mapping in plane SQL, it will create for you as by default

To learn how to make these models like the below picture click on below and learn, how to make these models. I request you to just Subscribe to my youtube channel and like. 


1:1 mapping: means there should be only one student allowed to have one address

1:M mapping: means one student can have many courses to be enroled

M:1 mapping: many courses can be enrolled by one student

M:M mapping: Many posts will have many Tags

My Youtube Channel Link:

View Youtube Channel

Design Scheema

All type of mapping
Mapping in Spring boot

Pom.xml file

See Dependencies

application.properties file

# Spring DATASOURCE (DataSourceAutoConfiguration & DataSourceProperties)
spring.datasource.url=jdbc:mysql://localhost:3306/relations?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username=root
spring.datasource.password=Fahad@123
spring.jpa.show-sql=true
# Hibernate Properties
#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=create-drop
logging.level.org.hibernate.sql=DEBUG
logging.level.org.hibernate.type=TRACE
server.port=9090

Models:

Student

package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.Set;

@Entity
@Table(name = "student")
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Student {
@Id
@Column(name = "student_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String name;
private String email;

@OneToOne(mappedBy = "address_id", cascade = CascadeType.ALL)
private Address address;

@OneToMany(mappedBy = "course")
private Set<Course> student;
}

Course

package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;

@Entity
@Table(name = "course")
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Course {
@Id
@Column(name = "course_id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String courseName;

@ManyToOne
@JoinColumn(name="student", nullable=false)
private Student course;
}


Address
package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;

@Entity
@Table(name = "address")
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Address {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;

private String address;

@OneToOne
@MapsId
private Student address_id;
}

Posts:

package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "posts")
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Posts {
@Id
@Column(name = "post_id")
private Integer id;

private String description;

@ManyToMany(mappedBy = "posts")
private List<Tags> tags;
}


Tags:

package com.example.demo.model;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

import javax.persistence.*;
import java.util.List;

@Entity
@Table(name = "tags")
@AllArgsConstructor
@NoArgsConstructor
@Data
@ToString
public class Tags {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer tag_id;

private String tag_name;

@ManyToMany
private List<Posts> posts;
}




Repositories


package com.example.demo.repositories;

import com.example.demo.model.Address;
import org.springframework.data.jpa.repository.JpaRepository;

public interface AddressRepo extends JpaRepository<Address, Integer> {
}


package com.example.demo.repositories;

import com.example.demo.model.Course;
import org.springframework.data.jpa.repository.JpaRepository;

public interface CourseRepo extends JpaRepository<Course, Integer> {
}


package com.example.demo.repositories;

import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;

public interface StudentRepo extends JpaRepository<Student, Integer> {
}



package com.example.demo.repositories;

import com.example.demo.model.Posts;
import org.springframework.data.jpa.repository.JpaRepository;

public interface PostsRepo extends JpaRepository<Posts, Integer> {
}


package com.example.demo.repositories;

import com.example.demo.model.Tags;
import org.springframework.data.jpa.repository.JpaRepository;

public interface TagsRepo extends JpaRepository<Tags, Integer> {
}


Comments

Popular Posts