Skip to content

Commit 7267a4d

Browse files
committed
Calculate entity property at INSERT or UPDATE time
1 parent 911bcd1 commit 7267a4d

File tree

6 files changed

+33
-13
lines changed

6 files changed

+33
-13
lines changed

HibernateSpringBootCalculatePropertyGenerated/src/main/java/com/bookstore/MainApplication.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.bookstore;
22

3+
import com.bookstore.entity.Book;
34
import com.bookstore.service.BookstoreService;
45
import org.springframework.boot.ApplicationRunner;
56
import org.springframework.boot.SpringApplication;
@@ -23,6 +24,9 @@ public static void main(String[] args) {
2324
public ApplicationRunner init() {
2425
return args -> {
2526
bookstoreService.insertBook();
27+
28+
Book book = bookstoreService.fetchBook();
29+
System.out.println(book);
2630
};
2731
}
2832
}

HibernateSpringBootCalculatePropertyGenerated/src/main/java/com/bookstore/entity/Book.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ public class Book implements Serializable {
2626
// @Column(name="...", insertable=false)
2727

2828
@Generated(value = GenerationTime.ALWAYS)
29-
@Column(insertable = false, updatable = false,
30-
columnDefinition = "double AS (price - price * 0.25)")
29+
@Column(insertable = false, updatable = false /*, columnDefinition = "double AS (price - price * 0.25)") */)
3130
private double discount;
3231

3332
public Long getId() {

HibernateSpringBootCalculatePropertyGenerated/src/main/java/com/bookstore/repository/BookRepository.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,7 @@
55
import org.springframework.stereotype.Repository;
66

77
@Repository
8-
public interface BookRepository extends JpaRepository<Book, Long> {
8+
public interface BookRepository extends JpaRepository<Book, Long> {
9+
10+
public Book findByTitle(String title);
911
}

HibernateSpringBootCalculatePropertyGenerated/src/main/java/com/bookstore/service/BookstoreService.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,27 @@
88

99
@Service
1010
public class BookstoreService {
11-
11+
1212
private static final Logger logger
1313
= Logger.getLogger(BookstoreService.class.getName());
14-
14+
1515
private final BookRepository bookRepository;
16-
17-
public BookstoreService(BookRepository bookRepository) {
16+
17+
public BookstoreService(BookRepository bookRepository) {
1818
this.bookRepository = bookRepository;
1919
}
20-
20+
2121
public void insertBook() {
2222
Book book = new Book();
23-
24-
book.setTitle("Title_1");
25-
book.setIsbn("Isbn_1");
23+
24+
book.setTitle("Ancient History");
25+
book.setIsbn("001-AH");
2626
book.setPrice(13.99);
27-
27+
2828
bookRepository.save(book);
2929
}
30+
31+
public Book fetchBook() {
32+
return bookRepository.findByTitle("Ancient History");
33+
}
3034
}

HibernateSpringBootCalculatePropertyGenerated/src/main/resources/application.properties

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ spring.datasource.url=jdbc:mysql://localhost:3306/bookstoredb?createDatabaseIfNo
22
spring.datasource.username=root
33
spring.datasource.password=root
44

5-
spring.jpa.hibernate.ddl-auto=create
5+
spring.jpa.hibernate.ddl-auto=none
66
spring.jpa.show-sql=true
77

88
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
99

1010
spring.jpa.open-in-view=false
1111

12+
spring.datasource.initialization-mode=always
13+
spring.datasource.platform=mysql
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE `book` (
2+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
3+
`discount` double GENERATED ALWAYS AS ((`price` - (`price` * 0.25))) VIRTUAL,
4+
`isbn` varchar(255) DEFAULT NULL,
5+
`price` double NOT NULL,
6+
`title` varchar(255) DEFAULT NULL,
7+
PRIMARY KEY (`id`)
8+
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
9+
SELECT * FROM bookstoredb.book;

0 commit comments

Comments
 (0)