这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion persistence-modules/hibernate-jpa-3/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@
<spring-boot.version>3.0.4</spring-boot.version>
<h2.version>2.1.214</h2.version>
<p6spy.version>3.9.1</p6spy.version>
<jakarta-persistence.version>3.2.0</jakarta-persistence.version>
<!--
Spring Boot v3.0.4 is designed and tested with Jakarta Persistence API v3.1.0
Version upgrade should be done with cautious, and in correlation with Spring Boot version upgrade
-->
<jakarta-persistence.version>3.1.0</jakarta-persistence.version>
</properties>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.hibernate.dirtychecking;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class DirtyCheckingApplication {

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.baeldung.hibernate.dirtychecking.entity;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;

@Entity
public class Product {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String code;
private double price;

public Product() {
}

public Product(String code, double price) {
this.code = code;
this.price = price;
}

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public String getCode() {
return code;
}

public void setCode(String code) {
this.code = code;
}

public double getPrice() {
return price;
}

public void setPrice(double price) {
this.price = price;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.baeldung.hibernate.dirtychecking.repository;

import java.util.Optional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import com.baeldung.hibernate.dirtychecking.entity.Product;

@Repository
public interface ProductRepository extends JpaRepository<Product, Long> {
Optional<Product> findByCode(String code);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
jpa:
show-sql: true
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.baeldung.hibernate.dirtychecking;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.transaction.annotation.Transactional;

import com.baeldung.hibernate.dirtychecking.entity.Product;
import com.baeldung.hibernate.dirtychecking.repository.ProductRepository;

import jakarta.persistence.EntityManager;

@SpringBootTest
@ActiveProfiles("dirtychecking")
public class ProductRepositoryTest {

@Autowired
private ProductRepository productRepository;

@Autowired
private EntityManager entityManager;

@Test
@Transactional
void givenProduct_whenModifiedWithoutSave_thenAssertChangesPersisted() {
Product product = new Product("LOREM", 100.00);
productRepository.save(product);

product.setPrice(80.00);
entityManager.flush();
entityManager.clear();

Product updatedProduct = productRepository.findByCode("LOREM").orElseThrow(RuntimeException::new);
assertEquals(80.00, updatedProduct.getPrice());
}

@Test
@Transactional
void givenDetachedProduct_whenModifiedWithoutSave_thenAssertChangesNotPersisted() {
Product product = new Product("LOREM", 100.00);
productRepository.save(product);

entityManager.detach(product);
product.setPrice(80.00);
entityManager.flush();
entityManager.clear();

Product updatedProduct = productRepository.findByCode("LOREM").orElseThrow(RuntimeException::new);
assertEquals(100.00, updatedProduct.getPrice());
}

}