-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c4486fe
commit 191cacd
Showing
14 changed files
with
339 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?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.3.3.RELEASE</version> | ||
<relativePath/> | ||
</parent> | ||
<groupId>ru.sysout</groupId> | ||
<artifactId>one-to-many</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
<name>one-to-many</name> | ||
<description>Demo project for Spring Boot</description> | ||
|
||
<properties> | ||
<java.version>11</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-data-jpa</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.h2database</groupId> | ||
<artifactId>h2</artifactId> | ||
<scope>runtime</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
<optional>true</optional> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</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> |
32 changes: 32 additions & 0 deletions
32
hibernate-one-to-many/src/main/java/ru/sysout/SpringDataJpaApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package ru.sysout; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.CommandLineRunner; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
import org.springframework.transaction.annotation.Transactional; | ||
import ru.sysout.bad.dao.TopicRepository; | ||
import ru.sysout.bad.model.Topic; | ||
|
||
import java.util.Optional; | ||
|
||
@SpringBootApplication | ||
public class SpringDataJpaApplication implements CommandLineRunner { | ||
@Autowired | ||
private TopicRepository topicRepository; | ||
public static void main(String[] args) { | ||
SpringApplication.run(SpringDataJpaApplication.class, args); | ||
} | ||
|
||
@Override | ||
public void run(String... args) throws Exception { | ||
|
||
// delComment(); | ||
} | ||
@Transactional | ||
public void delComment(){ | ||
Optional<Topic> optionalTopic = topicRepository.findById(-1l); | ||
optionalTopic.get().getComments().remove(0); | ||
topicRepository.save(optionalTopic.get()); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
hibernate-one-to-many/src/main/java/ru/sysout/bad/dao/CommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.sysout.bad.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.bad.model.Comment; | ||
|
||
public interface CommentRepository extends JpaRepository<Comment, Long> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
hibernate-one-to-many/src/main/java/ru/sysout/bad/dao/TopicRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.sysout.bad.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.bad.model.Topic; | ||
|
||
public interface TopicRepository extends JpaRepository<Topic, Long> { | ||
} |
24 changes: 24 additions & 0 deletions
24
hibernate-one-to-many/src/main/java/ru/sysout/bad/model/Comment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ru.sysout.bad.model; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.Entity; | ||
import javax.persistence.GeneratedValue; | ||
import javax.persistence.GenerationType; | ||
import javax.persistence.Id; | ||
|
||
@NoArgsConstructor | ||
@Data | ||
@Entity | ||
public class Comment { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private long id; | ||
private String text; | ||
|
||
public Comment(String text) { | ||
this.text = text; | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
hibernate-one-to-many/src/main/java/ru/sysout/bad/model/Topic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ru.sysout.bad.model; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Entity | ||
public class Topic { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private long id; | ||
private String title; | ||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true) | ||
@JoinColumn(name = "topic_id") | ||
private List<Comment> comments=new ArrayList<>(); | ||
public Topic(String title){ | ||
this.title=title; | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
hibernate-one-to-many/src/main/java/ru/sysout/good/dao/GoodCommentRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package ru.sysout.good.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.good.model.GoodComment; | ||
|
||
public interface GoodCommentRepository extends JpaRepository<GoodComment, Long> { | ||
|
||
} |
7 changes: 7 additions & 0 deletions
7
hibernate-one-to-many/src/main/java/ru/sysout/good/dao/GoodTopicRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ru.sysout.good.dao; | ||
|
||
import org.springframework.data.jpa.repository.JpaRepository; | ||
import ru.sysout.good.model.GoodTopic; | ||
|
||
public interface GoodTopicRepository extends JpaRepository<GoodTopic, Long> { | ||
} |
24 changes: 24 additions & 0 deletions
24
hibernate-one-to-many/src/main/java/ru/sysout/good/model/GoodComment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package ru.sysout.good.model; | ||
|
||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
|
||
@NoArgsConstructor | ||
@Data | ||
@Entity | ||
public class GoodComment { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private long id; | ||
private String text; | ||
|
||
@ManyToOne(fetch = FetchType.LAZY) | ||
private GoodTopic topic; | ||
|
||
public GoodComment(String text) { | ||
this.text = text; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
hibernate-one-to-many/src/main/java/ru/sysout/good/model/GoodTopic.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package ru.sysout.good.model; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import javax.persistence.*; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@Data | ||
@NoArgsConstructor | ||
@Entity | ||
public class GoodTopic { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.SEQUENCE) | ||
private long id; | ||
private String title; | ||
@OneToMany(mappedBy = "topic", cascade = CascadeType.ALL, orphanRemoval = true) | ||
private List<GoodComment> comments=new ArrayList<>(); | ||
public GoodTopic(String title){ | ||
this.title=title; | ||
} | ||
public void addComment(GoodComment comment) { | ||
comments.add(comment); | ||
comment.setTopic(this); | ||
} | ||
|
||
public void removeComment(GoodComment comment) { | ||
comments.remove(comment); | ||
comment.setTopic(null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
spring: | ||
h2: | ||
console: | ||
enabled: true | ||
path: /h2-console | ||
datasource: | ||
url: jdbc:h2:mem:testdb | ||
username: sa | ||
password: | ||
initialization-mode: always | ||
jpa: | ||
show-sql: true | ||
hibernate: | ||
ddl-auto: create |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
insert into topic (id, title) values (-1,'title1'); | ||
insert into topic (id, title) values (-2, 'title2'); | ||
insert into topic (id, title) values (-3, 'title3'); | ||
|
||
insert into comment (id, text, topic_id) values (-4, 'text1', -1); | ||
insert into comment (id, text, topic_id) values (-5, 'text2', -1); | ||
insert into comment (id, text, topic_id) values (-6, 'text3', -1); | ||
|
||
|
||
insert into good_topic (id, title) values (-11,'title1'); | ||
insert into good_topic (id, title) values (-12, 'title2'); | ||
insert into good_topic (id, title) values (-13, 'title3'); | ||
|
||
insert into good_comment (id, text, topic_id) values (-14, 'text1', -11); | ||
insert into good_comment (id, text, topic_id) values (-15, 'text2', -11); | ||
insert into good_comment (id, text, topic_id) values (-16, 'text3', -11); |
41 changes: 41 additions & 0 deletions
41
hibernate-one-to-many/src/test/java/ru/sysout/BadStructureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru.sysout; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import ru.sysout.bad.dao.CommentRepository; | ||
import ru.sysout.bad.dao.TopicRepository; | ||
import ru.sysout.bad.model.Comment; | ||
import ru.sysout.bad.model.Topic; | ||
|
||
@DataJpaTest | ||
public class BadStructureTest { | ||
@Autowired | ||
private TopicRepository topicRepository; | ||
@Autowired | ||
private CommentRepository commentRepository; | ||
|
||
@Test | ||
@DisplayName("при добавлении топика для каждого комментария выполняется insert и update") | ||
public void whenAddTopicWithComments_thenInsertsWithUpdates() { | ||
Topic topic = new Topic("title"); | ||
topic.getComments().add(new Comment("c1")); | ||
topic.getComments().add(new Comment("c2")); | ||
topic.getComments().add(new Comment("c3")); | ||
topic = topicRepository.save(topic); | ||
|
||
Assertions.assertEquals(4, topicRepository.count()); | ||
} | ||
|
||
@Test | ||
@DisplayName("при удалении комментария для него выполняется update и delete") | ||
|
||
public void whenDeleteComment_thenDeleteWithUpdate() { | ||
Topic topic = topicRepository.getOne(-1l); | ||
topic.getComments().remove(0); | ||
|
||
Assertions.assertEquals(2, commentRepository.count()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
hibernate-one-to-many/src/test/java/ru/sysout/GoodStructureTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package ru.sysout; | ||
|
||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; | ||
import ru.sysout.good.dao.GoodCommentRepository; | ||
import ru.sysout.good.dao.GoodTopicRepository; | ||
import ru.sysout.good.model.GoodComment; | ||
import ru.sysout.good.model.GoodTopic; | ||
|
||
@DataJpaTest | ||
public class GoodStructureTest { | ||
@Autowired | ||
private GoodTopicRepository topicRepository; | ||
@Autowired | ||
private GoodCommentRepository commentRepository; | ||
|
||
@Test | ||
@DisplayName("при добавлении топика для каждого комментария выполняется один insert") | ||
public void whenAddTopicWithComments_thenInsertsWithUpdates() { | ||
GoodTopic topic = new GoodTopic("title"); | ||
topic.addComment(new GoodComment("c1")); | ||
topic.addComment(new GoodComment("c2")); | ||
topic.addComment(new GoodComment("c3")); | ||
topic = topicRepository.save(topic); | ||
|
||
Assertions.assertEquals(4, topicRepository.count()); | ||
} | ||
|
||
@Test | ||
@DisplayName("при удалении комментария для него выполняется один delete") | ||
|
||
public void whenDeleteComment_thenDeleteWithUpdate() { | ||
GoodTopic topic = topicRepository.getOne(-11l); | ||
topic.getComments().remove(0); | ||
|
||
Assertions.assertEquals(2, commentRepository.count()); | ||
} | ||
} |