Skip to content

Commit 18f04a3

Browse files
Configuration for Duality Views (#210)
Configuration for Duality Views Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent dcd3818 commit 18f04a3

File tree

9 files changed

+134
-45
lines changed

9 files changed

+134
-45
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.spring.json.duality.builder;
5+
6+
import com.oracle.spring.json.duality.annotation.JsonRelationalDualityView;
7+
import javax.sql.DataSource;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.boot.autoconfigure.AutoConfiguration;
10+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
11+
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
12+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
13+
import org.springframework.context.annotation.Bean;
14+
import org.springframework.core.env.Environment;
15+
import org.springframework.core.io.ResourceLoader;
16+
import org.springframework.data.util.AnnotatedTypeScanner;
17+
import org.springframework.util.StringUtils;
18+
19+
@AutoConfiguration
20+
@EnableConfigurationProperties(DualityViewConfigurationProperties.class)
21+
public class DualityViewAutoConfiguration {
22+
@Bean
23+
@Qualifier("jsonRelationalDualityViewScanner")
24+
public AnnotatedTypeScanner scanner(ResourceLoader resourceLoader, Environment environment) {
25+
AnnotatedTypeScanner dvScanner = new AnnotatedTypeScanner(JsonRelationalDualityView.class);
26+
27+
dvScanner.setResourceLoader(resourceLoader);
28+
dvScanner.setEnvironment(environment);
29+
return dvScanner;
30+
}
31+
32+
@Bean
33+
DualityViewBuilder dualityViewBuilder(DataSource dataSource,
34+
JpaProperties jpaProperties,
35+
HibernateProperties hibernateProperties,
36+
DualityViewConfigurationProperties dvProperties) {
37+
boolean isShowSQL = dvProperties != null && dvProperties.isShowSql() != null ?
38+
dvProperties.isShowSql() :
39+
jpaProperties.isShowSql();
40+
String ddlAuto = dvProperties != null && StringUtils.hasText(dvProperties.getDdlAuto()) ?
41+
dvProperties.getDdlAuto() :
42+
hibernateProperties.getDdlAuto();
43+
44+
return new DualityViewBuilder(
45+
dataSource,
46+
isShowSQL,
47+
ddlAuto
48+
);
49+
}
50+
}

database/starters/oracle-spring-boot-json-relational-duality-views/src/main/java/com/oracle/spring/json/duality/builder/DualityViewBuilder.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import static com.oracle.spring.json.duality.builder.Annotations.getAccessModeStr;
2121
import static com.oracle.spring.json.duality.builder.Annotations.getViewName;
2222

23-
@Component
2423
public final class DualityViewBuilder implements DisposableBean {
2524
private static final String PREFIX = "JSON Relational Duality Views: ";
2625
private static final int TABLE_OR_VIEW_DOES_NOT_EXIST = 942;
@@ -31,13 +30,11 @@ public final class DualityViewBuilder implements DisposableBean {
3130
private final Map<String, String> dualityViews = new HashMap<>();
3231

3332
public DualityViewBuilder(DataSource dataSource,
34-
JpaProperties jpaProperties,
35-
HibernateProperties hibernateProperties) {
33+
boolean isShowSql,
34+
String ddlAuto) {
3635
this.dataSource = dataSource;
37-
this.isShowSql = jpaProperties.isShowSql();
38-
this.rootSnippet = RootSnippet.fromDdlAuto(
39-
hibernateProperties.getDdlAuto()
40-
);
36+
this.isShowSql = isShowSql;
37+
this.rootSnippet = RootSnippet.fromDdlAuto(ddlAuto);
4138
}
4239

4340
void apply() {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package com.oracle.spring.json.duality.builder;
5+
6+
import org.springframework.boot.context.properties.ConfigurationProperties;
7+
8+
@ConfigurationProperties("spring.jpa.dv")
9+
public class DualityViewConfigurationProperties {
10+
private Boolean showSql;
11+
private String ddlAuto;
12+
13+
public Boolean isShowSql() {
14+
return showSql;
15+
}
16+
17+
public void setShowSql(boolean showSql) {
18+
this.showSql = showSql;
19+
}
20+
21+
public String getDdlAuto() {
22+
return ddlAuto;
23+
}
24+
25+
public void setDdlAuto(String ddlAuto) {
26+
this.ddlAuto = ddlAuto;
27+
}
28+
}

database/starters/oracle-spring-boot-json-relational-duality-views/src/main/java/com/oracle/spring/json/duality/builder/RootSnippet.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public static RootSnippet fromDdlAuto(String ddlAuto) {
2727
return switch (ddlAuto) {
2828
case "none" -> NONE;
2929
case "validate" -> VALIDATE;
30-
case "create" -> CREATE;
30+
case "create-only" -> CREATE;
3131
case "create-drop" -> CREATE_DROP;
32-
case "update" -> UPDATE;
32+
case "create", "update" -> UPDATE;
3333
default -> throw new IllegalStateException("Unexpected value: " + ddlAuto);
3434
};
3535
}

database/starters/oracle-spring-boot-json-relational-duality-views/src/main/java/com/oracle/spring/json/duality/builder/ScannerConfiguration.java

Lines changed: 0 additions & 25 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"groups": [],
3+
"properties": [],
4+
"hints": [
5+
{
6+
"name": "spring.jpa.dv.ddl-auto",
7+
"values": [
8+
{
9+
"value": "create",
10+
"description": "Create the duality view, overwriting the existing view if necessary."
11+
},
12+
{
13+
"value": "create-drop",
14+
"description": "Create and then destroy the duality view at the end of the session."
15+
},
16+
{
17+
"value": "create-only",
18+
"description": "Create the duality view."
19+
},
20+
{
21+
"value": "drop",
22+
"description": "Drop the duality view."
23+
},
24+
{
25+
"value": "none",
26+
"description": "Disable DDL handling."
27+
},
28+
{
29+
"value": "update",
30+
"description": "Update the duality view if necessary."
31+
},
32+
{
33+
"value": "validate",
34+
"description": "Validate the duality view, make no changes to the database."
35+
}
36+
]
37+
}
38+
]
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
com.oracle.spring.json.duality.builder.DualityViewAutoConfiguration

database/starters/oracle-spring-boot-json-relational-duality-views/src/test/java/com/oracle/spring/json/duality/builder/DualityViewBuilderTest.java

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@
1414
import org.junit.jupiter.params.ParameterizedTest;
1515
import org.junit.jupiter.params.provider.Arguments;
1616
import org.junit.jupiter.params.provider.MethodSource;
17-
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
18-
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
1917

2018
import static com.oracle.spring.json.duality.SpringBootDualityTest.readViewFile;
2119
import static org.assertj.core.api.Assertions.assertThat;
@@ -24,11 +22,11 @@ public class DualityViewBuilderTest {
2422
public static @NotNull Stream<Arguments> entityClasses() {
2523
return Stream.of(
2624
Arguments.of(Student.class, "student-update.sql", "update"),
27-
Arguments.of(Student.class, "student-create.sql", "create"),
28-
Arguments.of(Actor.class, "actor-create.sql", "create"),
29-
Arguments.of(Order.class, "order-create.sql", "create"),
25+
Arguments.of(Student.class, "student-create.sql", "create-only"),
26+
Arguments.of(Actor.class, "actor-create.sql", "create-only"),
27+
Arguments.of(Order.class, "order-create.sql", "create-only"),
3028
Arguments.of(Member.class, "member-create-drop.sql", "create-drop"),
31-
Arguments.of(Employee.class, "employee-create.sql", "create")
29+
Arguments.of(Employee.class, "employee-create.sql", "create-only")
3230
);
3331
}
3432

@@ -43,12 +41,10 @@ public void buildViews(Class<?> entity, String viewFile, String ddlAuto) {
4341
}
4442

4543
private DualityViewBuilder getDualityViewBuilder(String ddlAuto) {
46-
HibernateProperties hibernateProperties = new HibernateProperties();
47-
hibernateProperties.setDdlAuto(ddlAuto);
4844
return new DualityViewBuilder(
4945
null,
50-
new JpaProperties(),
51-
hibernateProperties
46+
false,
47+
ddlAuto
5248
);
5349
}
5450
}

database/starters/oracle-spring-boot-json-relational-duality-views/src/test/resources/application.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ spring:
1414
jpa:
1515
hibernate:
1616
ddl-auto: create-drop
17-
show-sql: true
17+
show-sql: false
18+
dv:
19+
ddl-auto: create-drop
20+
show-sql: false

0 commit comments

Comments
 (0)