Skip to content

Configuration for Duality Views #210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jun 19, 2025
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.spring.json.duality.builder;

import com.oracle.spring.json.duality.annotation.JsonRelationalDualityView;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.core.io.ResourceLoader;
import org.springframework.data.util.AnnotatedTypeScanner;
import org.springframework.util.StringUtils;

@AutoConfiguration
@EnableConfigurationProperties(DualityViewConfigurationProperties.class)
public class DualityViewAutoConfiguration {
@Bean
@Qualifier("jsonRelationalDualityViewScanner")
public AnnotatedTypeScanner scanner(ResourceLoader resourceLoader, Environment environment) {
AnnotatedTypeScanner dvScanner = new AnnotatedTypeScanner(JsonRelationalDualityView.class);

dvScanner.setResourceLoader(resourceLoader);
dvScanner.setEnvironment(environment);
return dvScanner;
}

@Bean
DualityViewBuilder dualityViewBuilder(DataSource dataSource,
JpaProperties jpaProperties,
HibernateProperties hibernateProperties,
DualityViewConfigurationProperties dvProperties) {
boolean isShowSQL = dvProperties != null && dvProperties.isShowSql() != null ?
dvProperties.isShowSql() :
jpaProperties.isShowSql();
String ddlAuto = dvProperties != null && StringUtils.hasText(dvProperties.getDdlAuto()) ?
dvProperties.getDdlAuto() :
hibernateProperties.getDdlAuto();

return new DualityViewBuilder(
dataSource,
isShowSQL,
ddlAuto
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static com.oracle.spring.json.duality.builder.Annotations.getAccessModeStr;
import static com.oracle.spring.json.duality.builder.Annotations.getViewName;

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

public DualityViewBuilder(DataSource dataSource,
JpaProperties jpaProperties,
HibernateProperties hibernateProperties) {
boolean isShowSql,
String ddlAuto) {
this.dataSource = dataSource;
this.isShowSql = jpaProperties.isShowSql();
this.rootSnippet = RootSnippet.fromDdlAuto(
hibernateProperties.getDdlAuto()
);
this.isShowSql = isShowSql;
this.rootSnippet = RootSnippet.fromDdlAuto(ddlAuto);
}

void apply() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright (c) 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.

package com.oracle.spring.json.duality.builder;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties("spring.jpa.dv")
public class DualityViewConfigurationProperties {
private Boolean showSql;
private String ddlAuto;

public Boolean isShowSql() {
return showSql;
}

public void setShowSql(boolean showSql) {
this.showSql = showSql;
}

public String getDdlAuto() {
return ddlAuto;
}

public void setDdlAuto(String ddlAuto) {
this.ddlAuto = ddlAuto;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ public static RootSnippet fromDdlAuto(String ddlAuto) {
return switch (ddlAuto) {
case "none" -> NONE;
case "validate" -> VALIDATE;
case "create" -> CREATE;
case "create-only" -> CREATE;
case "create-drop" -> CREATE_DROP;
case "update" -> UPDATE;
case "create", "update" -> UPDATE;
default -> throw new IllegalStateException("Unexpected value: " + ddlAuto);
};
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"groups": [],
"properties": [],
"hints": [
{
"name": "spring.jpa.dv.ddl-auto",
"values": [
{
"value": "create",
"description": "Create the duality view, overwriting the existing view if necessary."
},
{
"value": "create-drop",
"description": "Create and then destroy the duality view at the end of the session."
},
{
"value": "create-only",
"description": "Create the duality view."
},
{
"value": "drop",
"description": "Drop the duality view."
},
{
"value": "none",
"description": "Disable DDL handling."
},
{
"value": "update",
"description": "Update the duality view if necessary."
},
{
"value": "validate",
"description": "Validate the duality view, make no changes to the database."
}
]
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.oracle.spring.json.duality.builder.DualityViewAutoConfiguration
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateProperties;
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties;

import static com.oracle.spring.json.duality.SpringBootDualityTest.readViewFile;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -24,11 +22,11 @@ public class DualityViewBuilderTest {
public static @NotNull Stream<Arguments> entityClasses() {
return Stream.of(
Arguments.of(Student.class, "student-update.sql", "update"),
Arguments.of(Student.class, "student-create.sql", "create"),
Arguments.of(Actor.class, "actor-create.sql", "create"),
Arguments.of(Order.class, "order-create.sql", "create"),
Arguments.of(Student.class, "student-create.sql", "create-only"),
Arguments.of(Actor.class, "actor-create.sql", "create-only"),
Arguments.of(Order.class, "order-create.sql", "create-only"),
Arguments.of(Member.class, "member-create-drop.sql", "create-drop"),
Arguments.of(Employee.class, "employee-create.sql", "create")
Arguments.of(Employee.class, "employee-create.sql", "create-only")
);
}

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

private DualityViewBuilder getDualityViewBuilder(String ddlAuto) {
HibernateProperties hibernateProperties = new HibernateProperties();
hibernateProperties.setDdlAuto(ddlAuto);
return new DualityViewBuilder(
null,
new JpaProperties(),
hibernateProperties
false,
ddlAuto
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,7 @@ spring:
jpa:
hibernate:
ddl-auto: create-drop
show-sql: true
show-sql: false
dv:
ddl-auto: create-drop
show-sql: false
Loading