Home > AI > Backend > SpringBoot > mysql-connector-java >

@Rollback

Default is @Rollback(true), which means the transaction will not be saved into database.

@Rollback(false) means the transaction will not rolled back, and will be persisted into the database.

Customer.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Customer")
public class Customer {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String name;

    @OneToMany(mappedBy = "customer", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Set<Number> numbers;


    public void addNumber(Number number) {
        if (number == null){
            return;
        }

        if (this.numbers == null) {
            this.numbers = new HashSet<>();
        }

        this.numbers.add(number);
        number.setCustomer(this);
    }
}

Number.java

@Getter
@Setter
@NoArgsConstructor
@AllArgsConstructor
@Entity
@Table(name = "Number")
public class Number {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;
    private String number;
    private String type;

    @ManyToOne
    @JoinColumn(name = "customer_id")
    private Customer customer;
}

CutomerRepository.java

@Repository
public interface CustomerRepository extends JpaRepository<Customer, Long> {
}

NumberRepository.java

@Repository
public interface NumberRepository extends JpaRepository<Number, Long> {
}

TestRepository.java

@Slf4j
@SpringBootTest
@Transactional
public class TestRepository {


    @Autowired
    CustomerRepository customerRepository;
    @Autowired
    NumberRepository numberRepository;



    @Test
//    @Rollback(false)
    public void testRead() {
        // 1 - save
        Customer customer = new Customer();
        customer.setName("Mary");

        Number p1 = new Number();
        p1.setNumber("111");
        p1.setType("cell");

        Number p2 = new Number();
        p2.setNumber("222");
        p2.setType("home");


        customer.addNumber(p1);
        customer.addNumber(p2);

        // both entities are saved, but
        customerRepository.save(customer);




        // 2 - read
        Customer c = customerRepository.findById(customer.getId()).get();
        assertEquals("Mary", c.getName());
        assertEquals(2, c.getNumbers().size());
    }



    @Test
//    @Rollback(false)
    public void testUpdate() {
        // 1 - save
        Customer customer = new Customer();
        customer.setName("Mary");

        Number p1 = new Number();
        p1.setNumber("111");
        p1.setType("cell");

        Number p2 = new Number();
        p2.setNumber("222");
        p2.setType("home");

        customer.addNumber(p1);
        customer.addNumber(p2);

        // both entities are saved, but
        customerRepository.save(customer);


        // 2 - update
        Customer c = customerRepository.findById(customer.getId()).orElse(null);
        c.setName("Aiden John");
        Set<Number> numbers = c.getNumbers();
        numbers.forEach(number -> number.setType("work"));
        customerRepository.save(customer);


        // 3 - validate
        Customer c2 = customerRepository.findById(c.getId()).orElse(null);
        assertEquals("Aiden John", c2.getName());
        assertEquals(2, c2.getNumbers().size());
        c2.getNumbers().forEach(n -> assertEquals("work", n.getType()));

    }


}

resources/application.properties

server.port = 8888


##################
# Mysql
##################
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver
spring.jpa.show-sql= false

Leave a Reply