Relationships and Cascade types. @OneToOne, @OneToMany, @ManyToMany relationships

@OneToOne 

Method1.

package com.example.lesson_8_parvin.entity;

import lombok.Data;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();

@OneToOne(mappedBy = "post", cascade = CascadeType.ALL,
fetch = FetchType.LAZY, optional = false)
private Detail detail;

}
package com.example.lesson_8_parvin.entity;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Data
@Entity
@Table(name = "detail")
public class Detail {

@Id
@GeneratedValue
private Long id;
private String detail;

@OneToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
}
package com.example.lesson_8_parvin;

import com.example.lesson_8_parvin.entity.Detail;
import com.example.lesson_8_parvin.entity.Post;
import com.example.lesson_8_parvin.repository.CommentRepository;
import com.example.lesson_8_parvin.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
@RequiredArgsConstructor
public class Lesson8ParvinApplication implements CommandLineRunner {

private final PostRepository postRepository;
private final CommentRepository commentRepository;

public static void main(String[] args) {
SpringApplication.run(Lesson8ParvinApplication.class, args);
}

@Override
// @Transactional
public void run(String... args) throws Exception {
Detail detail = new Detail();
detail.setDetail("detail");
Post post = new Post();
post.setTitle("title");
post.setDetail(detail);
detail.setPost(post);
postRepository.save(post);
}
}



@OneToMany

Method1.

Post(parent) - Comment(child)

package com.example.lesson_8_parvin.entity;

import lombok.Data;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import java.util.ArrayList;
import java.util.List;

@Data
@Entity
@Table(name = "post")
public class Post {
@Id
@GeneratedValue
private Long id;
private String title;

@OneToMany(mappedBy = "post", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Comment> comments = new ArrayList<>();
}
package com.example.lesson_8_parvin.entity;

import lombok.Data;
import lombok.ToString;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Data
@Entity
@Table(name = "comment")
public class Comment {

@Id
@GeneratedValue
private Long id;
private String review;

@ToString.Exclude
@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "post_id")
private Post post;
}
package com.example.lesson_8_parvin;

import com.example.lesson_8_parvin.entity.Comment;
import com.example.lesson_8_parvin.entity.Post;
import com.example.lesson_8_parvin.repository.CommentRepository;
import com.example.lesson_8_parvin.repository.PostRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.Arrays;
import java.util.List;

@SpringBootApplication
@RequiredArgsConstructor
public class Lesson8ParvinApplication implements CommandLineRunner {

private final PostRepository postRepository;
private final CommentRepository commentRepository;

public static void main(String[] args) {
SpringApplication.run(Lesson8ParvinApplication.class, args);
}

@Override
// @Transactional
public void run(String... args) throws Exception {
Comment one = new Comment();
one.setReview("one");
Comment two = new Comment();
two.setReview("two");
Comment three = new Comment();
three.setReview("three");
List<Comment> list = Arrays.asList(one, two, three);

Post post = new Post();
post.setTitle("title");
post.setComments(list);
one.setPost(post);
two.setPost(post);
three.setPost(post);
postRepository.save(post);
}
}



@ManyToMany



Cascade types












Комментарии

Популярные сообщения из этого блога

Lesson1: JDK, JVM, JRE

SE_21_Lesson_11: Inheritance, Polymorphism

SE_21_Lesson_9: Initialization Blocks, Wrapper types, String class