Skip to content

Commit 2f80ff3

Browse files
committed
Calculate Entity Persistent Property
1 parent bb1d7bb commit 2f80ff3

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Book implements Serializable {
2424

2525
@Generated(value = GenerationTime.ALWAYS)
2626
@Column(insertable = false, updatable = false /*, columnDefinition = "double AS (price - price * 0.25)"*/)
27-
private double discount;
27+
private double discounted;
2828

2929
public Long getId() {
3030
return id;
@@ -58,13 +58,13 @@ public void setPrice(double price) {
5858
this.price = price;
5959
}
6060

61-
public double getDiscount() {
62-
return discount;
61+
public double getDiscounted() {
62+
return discounted;
6363
}
6464

6565
@Override
6666
public String toString() {
6767
return "Book{" + "id=" + id + ", title=" + title + ", isbn="
68-
+ isbn + ", price=" + price + ", discount=" + discount + '}';
68+
+ isbn + ", price=" + price + ", discounted=" + discounted + '}';
6969
}
7070
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public void insertBook() {
2727

2828
bookRepository.save(book);
2929

30-
System.out.println("Discount after insert: " + book.getDiscount());
30+
System.out.println("Discounted price after insert: " + book.getDiscounted());
3131
}
3232

3333
@Transactional
@@ -37,6 +37,6 @@ public void updateBook() {
3737

3838
bookRepository.flush();
3939

40-
System.out.println("Discount after update: " + book.getDiscount());
40+
System.out.println("Discounted price after update: " + book.getDiscounted());
4141
}
4242
}

HibernateSpringBootCalculatePropertyGenerated/src/main/resources/application.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ spring.datasource.password=root
77

88
spring.jpa.show-sql=true
99

10-
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
10+
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
1111

1212
spring.jpa.open-in-view=false
1313

HibernateSpringBootCalculatePropertyGenerated/src/main/resources/schema-mysql.sql

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ DROP TABLE IF EXISTS bookstoredb.book;
44
-- Create
55
CREATE TABLE book (
66
id BIGINT NOT NULL AUTO_INCREMENT,
7-
discount DOUBLE GENERATED ALWAYS AS ((`price` - (`price` * 0.25))) STORED,
7+
discounted DOUBLE GENERATED ALWAYS AS ((`price` - `price` * 0.25)) STORED,
88
isbn VARCHAR(255),
99
price DOUBLE PRECISION NOT NULL,
1010
title VARCHAR(255),

0 commit comments

Comments
 (0)