Skip to content

Commit d9b1d59

Browse files
Sanneedeandrea
andauthored
Some minor tuning of the Hibernate model, enable 2LC (#67)
* Custom hashcode and equals aren't necessary with Hibernate - actually make things a bit trickier * Enable 2LC on the StoreFruitPrice -> Store relation and request eager initialization * Enabling Hibernate 2LC in Spring is a bit more work --------- Co-authored-by: Eric Deandrea <eric.deandrea@ibm.com>
1 parent f993a23 commit d9b1d59

11 files changed

Lines changed: 46 additions & 133 deletions

File tree

quarkus3-spring-compatibility/src/main/java/org/acme/domain/Fruit.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.util.List;
4-
import java.util.Objects;
54
import java.util.StringJoiner;
65

76
import jakarta.persistence.Column;
@@ -84,22 +83,4 @@ public String toString() {
8483
.toString();
8584
}
8685

87-
@Override
88-
public boolean equals(Object o) {
89-
if (this == o) {
90-
return true;
91-
}
92-
93-
if ((o == null) || (getClass() != o.getClass())) {
94-
return false;
95-
}
96-
97-
Fruit fruit = (Fruit) o;
98-
return this.id.equals(fruit.id);
99-
}
100-
101-
@Override
102-
public int hashCode() {
103-
return Objects.hash(this.id);
104-
}
10586
}

quarkus3-spring-compatibility/src/main/java/org/acme/domain/Store.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.acme.domain;
22

3-
import java.util.Objects;
43
import java.util.StringJoiner;
54

5+
import jakarta.persistence.Cacheable;
66
import jakarta.persistence.Column;
77
import jakarta.persistence.Embedded;
88
import jakarta.persistence.Entity;
@@ -17,6 +17,7 @@
1717

1818
@Entity
1919
@Table(name = "stores")
20+
@Cacheable
2021
public class Store {
2122
@Id
2223
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "stores_seq")
@@ -71,16 +72,4 @@ public String toString() {
7172
.toString();
7273
}
7374

74-
@Override
75-
public boolean equals(Object o) {
76-
if (this == o) return true;
77-
if (o == null || getClass() != o.getClass()) return false;
78-
Store store = (Store) o;
79-
return Objects.equals(id, store.id);
80-
}
81-
82-
@Override
83-
public int hashCode() {
84-
return Objects.hash(id);
85-
}
8675
}

quarkus3-spring-compatibility/src/main/java/org/acme/domain/StoreFruitPrice.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.math.BigDecimal;
4-
import java.util.Objects;
54

65
import jakarta.persistence.Column;
76
import jakarta.persistence.EmbeddedId;
@@ -16,6 +15,10 @@
1615
import jakarta.validation.constraints.NotNull;
1716

1817
import com.fasterxml.jackson.annotation.JsonIgnore;
18+
import org.hibernate.annotations.Cache;
19+
import org.hibernate.annotations.CacheConcurrencyStrategy;
20+
import org.hibernate.annotations.Fetch;
21+
import org.hibernate.annotations.FetchMode;
1922

2023
@Entity
2124
@Table(name = "store_fruit_prices")
@@ -25,8 +28,10 @@ public class StoreFruitPrice {
2528
private StoreFruitPriceId id;
2629

2730
@MapsId("storeId")
28-
@ManyToOne(fetch = FetchType.LAZY, optional = false)
31+
@ManyToOne(fetch = FetchType.EAGER, optional = false)
2932
@JoinColumn(name = "store_id", nullable = false)
33+
@Fetch(FetchMode.SELECT)
34+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
3035
private Store store;
3136

3237
@MapsId("fruitId")
@@ -70,14 +75,4 @@ public void setFruit(Fruit fruit) {
7075
public BigDecimal getPrice() { return price; }
7176
public void setPrice(BigDecimal price) { this.price = price; }
7277

73-
@Override
74-
public boolean equals(Object o) {
75-
if (!(o instanceof StoreFruitPrice that)) return false;
76-
return Objects.equals(id, that.id);
77-
}
78-
79-
@Override
80-
public int hashCode() {
81-
return Objects.hashCode(id);
82-
}
8378
}

quarkus3/src/main/java/org/acme/domain/Fruit.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.util.List;
4-
import java.util.Objects;
54
import java.util.StringJoiner;
65

76
import jakarta.persistence.Column;
@@ -84,22 +83,4 @@ public String toString() {
8483
.toString();
8584
}
8685

87-
@Override
88-
public boolean equals(Object o) {
89-
if (this == o) {
90-
return true;
91-
}
92-
93-
if ((o == null) || (getClass() != o.getClass())) {
94-
return false;
95-
}
96-
97-
Fruit fruit = (Fruit) o;
98-
return this.id.equals(fruit.id);
99-
}
100-
101-
@Override
102-
public int hashCode() {
103-
return Objects.hash(this.id);
104-
}
10586
}

quarkus3/src/main/java/org/acme/domain/Store.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.acme.domain;
22

3-
import java.util.Objects;
43
import java.util.StringJoiner;
54

5+
import jakarta.persistence.Cacheable;
66
import jakarta.persistence.Column;
77
import jakarta.persistence.Embedded;
88
import jakarta.persistence.Entity;
@@ -17,6 +17,7 @@
1717

1818
@Entity
1919
@Table(name = "stores")
20+
@Cacheable
2021
public class Store {
2122
@Id
2223
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "stores_seq")
@@ -71,16 +72,4 @@ public String toString() {
7172
.toString();
7273
}
7374

74-
@Override
75-
public boolean equals(Object o) {
76-
if (this == o) return true;
77-
if (o == null || getClass() != o.getClass()) return false;
78-
Store store = (Store) o;
79-
return Objects.equals(id, store.id);
80-
}
81-
82-
@Override
83-
public int hashCode() {
84-
return Objects.hash(id);
85-
}
8675
}

quarkus3/src/main/java/org/acme/domain/StoreFruitPrice.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.math.BigDecimal;
4-
import java.util.Objects;
54

65
import jakarta.persistence.Column;
76
import jakarta.persistence.EmbeddedId;
@@ -16,6 +15,10 @@
1615
import jakarta.validation.constraints.NotNull;
1716

1817
import com.fasterxml.jackson.annotation.JsonIgnore;
18+
import org.hibernate.annotations.Cache;
19+
import org.hibernate.annotations.CacheConcurrencyStrategy;
20+
import org.hibernate.annotations.Fetch;
21+
import org.hibernate.annotations.FetchMode;
1922

2023
@Entity
2124
@Table(name = "store_fruit_prices")
@@ -25,8 +28,10 @@ public class StoreFruitPrice {
2528
private StoreFruitPriceId id;
2629

2730
@MapsId("storeId")
28-
@ManyToOne(fetch = FetchType.LAZY, optional = false)
31+
@ManyToOne(fetch = FetchType.EAGER, optional = false)
2932
@JoinColumn(name = "store_id", nullable = false)
33+
@Fetch(FetchMode.SELECT)
34+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
3035
private Store store;
3136

3237
@MapsId("fruitId")
@@ -70,14 +75,4 @@ public void setFruit(Fruit fruit) {
7075
public BigDecimal getPrice() { return price; }
7176
public void setPrice(BigDecimal price) { this.price = price; }
7277

73-
@Override
74-
public boolean equals(Object o) {
75-
if (!(o instanceof StoreFruitPrice that)) return false;
76-
return Objects.equals(id, that.id);
77-
}
78-
79-
@Override
80-
public int hashCode() {
81-
return Objects.hashCode(id);
82-
}
8378
}

springboot3/pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@
3838
<artifactId>postgresql</artifactId>
3939
<scope>runtime</scope>
4040
</dependency>
41+
<dependency>
42+
<groupId>org.hibernate.orm</groupId>
43+
<artifactId>hibernate-jcache</artifactId>
44+
</dependency>
45+
<dependency>
46+
<groupId>org.ehcache</groupId>
47+
<artifactId>ehcache</artifactId>
48+
<classifier>jakarta</classifier>
49+
</dependency>
4150
<dependency>
4251
<groupId>org.springframework.boot</groupId>
4352
<artifactId>spring-boot-starter-test</artifactId>

springboot3/src/main/java/org/acme/domain/Fruit.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.util.List;
4-
import java.util.Objects;
54
import java.util.StringJoiner;
65

76
import jakarta.persistence.Column;
@@ -84,22 +83,4 @@ public String toString() {
8483
.toString();
8584
}
8685

87-
@Override
88-
public boolean equals(Object o) {
89-
if (this == o) {
90-
return true;
91-
}
92-
93-
if ((o == null) || (getClass() != o.getClass())) {
94-
return false;
95-
}
96-
97-
Fruit fruit = (Fruit) o;
98-
return this.id.equals(fruit.id);
99-
}
100-
101-
@Override
102-
public int hashCode() {
103-
return Objects.hash(this.id);
104-
}
10586
}

springboot3/src/main/java/org/acme/domain/Store.java

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package org.acme.domain;
22

3-
import java.util.Objects;
43
import java.util.StringJoiner;
54

5+
import jakarta.persistence.Cacheable;
66
import jakarta.persistence.Column;
77
import jakarta.persistence.Embedded;
88
import jakarta.persistence.Entity;
@@ -17,6 +17,7 @@
1717

1818
@Entity
1919
@Table(name = "stores")
20+
@Cacheable
2021
public class Store {
2122
@Id
2223
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "stores_seq")
@@ -71,16 +72,4 @@ public String toString() {
7172
.toString();
7273
}
7374

74-
@Override
75-
public boolean equals(Object o) {
76-
if (this == o) return true;
77-
if (o == null || getClass() != o.getClass()) return false;
78-
Store store = (Store) o;
79-
return Objects.equals(id, store.id);
80-
}
81-
82-
@Override
83-
public int hashCode() {
84-
return Objects.hash(id);
85-
}
8675
}

springboot3/src/main/java/org/acme/domain/StoreFruitPrice.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.acme.domain;
22

33
import java.math.BigDecimal;
4-
import java.util.Objects;
54

65
import jakarta.persistence.Column;
76
import jakarta.persistence.EmbeddedId;
@@ -16,6 +15,10 @@
1615
import jakarta.validation.constraints.NotNull;
1716

1817
import com.fasterxml.jackson.annotation.JsonIgnore;
18+
import org.hibernate.annotations.Cache;
19+
import org.hibernate.annotations.CacheConcurrencyStrategy;
20+
import org.hibernate.annotations.Fetch;
21+
import org.hibernate.annotations.FetchMode;
1922

2023
@Entity
2124
@Table(name = "store_fruit_prices")
@@ -25,8 +28,10 @@ public class StoreFruitPrice {
2528
private StoreFruitPriceId id;
2629

2730
@MapsId("storeId")
28-
@ManyToOne(fetch = FetchType.LAZY, optional = false)
31+
@ManyToOne(fetch = FetchType.EAGER, optional = false)
2932
@JoinColumn(name = "store_id", nullable = false)
33+
@Fetch(FetchMode.SELECT)
34+
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
3035
private Store store;
3136

3237
@MapsId("fruitId")
@@ -70,14 +75,4 @@ public void setFruit(Fruit fruit) {
7075
public BigDecimal getPrice() { return price; }
7176
public void setPrice(BigDecimal price) { this.price = price; }
7277

73-
@Override
74-
public boolean equals(Object o) {
75-
if (!(o instanceof StoreFruitPrice that)) return false;
76-
return Objects.equals(id, that.id);
77-
}
78-
79-
@Override
80-
public int hashCode() {
81-
return Objects.hashCode(id);
82-
}
8378
}

0 commit comments

Comments
 (0)