mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
readonly-transaction-demo example updated
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package ru.otus.demo.model;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Entity
|
||||
public class Email {
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||
private long id;
|
||||
|
||||
private String address;
|
||||
}
|
||||
@@ -4,10 +4,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@@ -20,4 +17,7 @@ public class Person {
|
||||
|
||||
private String name;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Email email;
|
||||
|
||||
}
|
||||
|
||||
+1
-1
@@ -19,7 +19,7 @@ public class PersonRepositoryJpa implements PersonRepository {
|
||||
@Override
|
||||
public Person save(Person person) {
|
||||
if (person.getId() == 0) {
|
||||
Person savedPerson = new Person(person.getId(), person.getName());
|
||||
Person savedPerson = new Person(person.getId(), person.getName(), person.getEmail());
|
||||
em.persist(savedPerson);
|
||||
return savedPerson;
|
||||
|
||||
|
||||
+5
@@ -22,6 +22,11 @@ public class PersonService {
|
||||
personRepository.save(person);
|
||||
}
|
||||
|
||||
public void updateWithoutTran(long id, String name) {
|
||||
Person person = personRepository.findById(id);
|
||||
person.setName(name);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void updateWithNormalTran(long id, String name) {
|
||||
Person person = personRepository.findById(id);
|
||||
|
||||
@@ -1,7 +1,17 @@
|
||||
drop table if exists person;
|
||||
drop table if exists email;
|
||||
|
||||
|
||||
create table email (
|
||||
id bigserial,
|
||||
address varchar(200),
|
||||
primary key(id)
|
||||
);
|
||||
|
||||
create table person (
|
||||
id bigserial,
|
||||
email_id bigint references email(id),
|
||||
name varchar(200),
|
||||
primary key(id)
|
||||
);
|
||||
|
||||
|
||||
+9
-1
@@ -8,6 +8,7 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.otus.demo.model.Email;
|
||||
import ru.otus.demo.model.Person;
|
||||
|
||||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||
@@ -23,7 +24,7 @@ class PersonServiceTest {
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
personService.save(new Person(0, "Igor"));
|
||||
personService.save(new Person(0, "Igor", new Email(0, "noname@nomail.ru")));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -39,4 +40,11 @@ class PersonServiceTest {
|
||||
Person actualPerson = personService.findById(1);
|
||||
assertThat(actualPerson).extracting(Person::getName).isEqualTo("Igor");
|
||||
}
|
||||
|
||||
@Test
|
||||
void updateWithoutTran() {
|
||||
personService.updateWithoutTran(1, "Vasya");
|
||||
Person actualPerson = personService.findById(1);
|
||||
assertThat(actualPerson).extracting(Person::getName).isEqualTo("Igor");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user