Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
S
spring-boot
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
DEMO
spring-boot
Commits
c2f452a8
Commit
c2f452a8
authored
Mar 22, 2021
by
Stephane Nicoll
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Polish "Take JPA database action into account when setting ddlAuto"
See gh-25129
parent
674b01cb
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
51 additions
and
30 deletions
+51
-30
HibernateProperties.java
...ework/boot/autoconfigure/orm/jpa/HibernateProperties.java
+7
-4
HibernateJpaAutoConfigurationTests.java
...configure/orm/jpa/HibernateJpaAutoConfigurationTests.java
+44
-26
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java
View file @
c2f452a8
...
...
@@ -130,14 +130,17 @@ public class HibernateProperties {
}
private
String
determineDdlAuto
(
Map
<
String
,
String
>
existing
,
Supplier
<
String
>
defaultDdlAuto
)
{
if
(
existing
.
get
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
)
!=
null
)
{
return
null
;
}
String
ddlAuto
=
existing
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
);
if
(
ddlAuto
!=
null
)
{
return
ddlAuto
;
}
return
(
this
.
ddlAuto
!=
null
)
?
this
.
ddlAuto
:
defaultDdlAuto
.
get
();
if
(
this
.
ddlAuto
!=
null
)
{
return
this
.
ddlAuto
;
}
if
(
existing
.
get
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
)
!=
null
)
{
return
null
;
}
return
defaultDdlAuto
.
get
();
}
public
static
class
Naming
{
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java
View file @
c2f452a8
...
...
@@ -373,47 +373,65 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenDdlAutoPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.hibernate.ddl-auto:create-drop"
,
true
);
void
vendorPropertiesWithEmbeddedDatabaseAndNoDdlProperty
()
{
contextRunner
().
run
(
vendorProperties
((
vendorProperties
)
->
{
assertThat
(
vendorProperties
).
doesNotContainKeys
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
);
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
)).
isEqualTo
(
"create-drop"
);
}));
}
@Test
void
dataSourceSchemaCreatedEventNotFiredWhenDdlAutoPropertyIsSetToNone
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.hibernate.ddl-auto:none"
,
false
);
void
vendorPropertiesWithDdlAutoPropertyIsSet
()
{
contextRunner
().
withPropertyValues
(
"spring.jpa.hibernate.ddl-auto=update"
)
.
run
(
vendorProperties
((
vendorProperties
)
->
{
assertThat
(
vendorProperties
).
doesNotContainKeys
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
);
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
)).
isEqualTo
(
"update"
);
}));
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenHibernateSpecificDdlAutoPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.hibernate.hbm2ddl.auto=create"
,
true
);
void
vendorPropertiesWithDdlAutoPropertyAndHibernatePropertiesAreSet
()
{
contextRunner
()
.
withPropertyValues
(
"spring.jpa.hibernate.ddl-auto=update"
,
"spring.jpa.properties.hibernate.hbm2ddl.auto=create-drop"
)
.
run
(
vendorProperties
((
vendorProperties
)
->
{
assertThat
(
vendorProperties
).
doesNotContainKeys
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
);
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
)).
isEqualTo
(
"create-drop"
);
}));
}
@Test
void
dataSourceSchemaCreatedEventNotFiredWhenHibernateSpecificDdlAutoPropertyIsSetToNone
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.hibernate.hbm2ddl.auto=none"
,
false
);
void
vendorPropertiesWithDdlAutoPropertyIsSetToNone
()
{
contextRunner
().
withPropertyValues
(
"spring.jpa.hibernate.ddl-auto=none"
)
.
run
(
vendorProperties
((
vendorProperties
)
->
assertThat
(
vendorProperties
).
doesNotContainKeys
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
,
AvailableSettings
.
HBM2DDL_AUTO
)));
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenJpaDbActionPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create"
,
true
);
void
vendorPropertiesWhenJpaDdlActionIsSet
()
{
contextRunner
()
.
withPropertyValues
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=create"
)
.
run
(
vendorProperties
((
vendorProperties
)
->
{
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
)).
isEqualTo
(
"create"
);
assertThat
(
vendorProperties
).
doesNotContainKeys
(
AvailableSettings
.
HBM2DDL_AUTO
);
}));
}
@Test
void
dataSourceSchemaCreatedEventNotFiredWhenJpaDbActionPropertyIsSetToNone
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=none"
,
false
);
}
private
void
dataSourceSchemaCreatedEventFired
(
String
schemaGenerationPropertyWithValue
,
boolean
expectEventToBeFired
)
{
contextRunner
().
withUserConfiguration
(
JpaUsingApplicationListenerConfiguration
.
class
)
.
withPropertyValues
(
"spring.datasource.initialization-mode=never"
,
schemaGenerationPropertyWithValue
)
.
run
((
context
)
->
{
assertThat
(
context
).
hasNotFailed
();
assertThat
(
context
.
getBean
(
EventCapturingApplicationListener
.
class
).
events
.
stream
()
.
filter
(
DataSourceSchemaCreatedEvent
.
class
::
isInstance
))
.
hasSize
(
expectEventToBeFired
?
1
:
0
);
});
void
vendorPropertiesWhenBothDdlAutoPropertiesAreSet
()
{
contextRunner
()
.
withPropertyValues
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=create"
,
"spring.jpa.hibernate.ddl-auto=create-only"
)
.
run
(
vendorProperties
((
vendorProperties
)
->
{
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
)).
isEqualTo
(
"create"
);
assertThat
(
vendorProperties
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
)).
isEqualTo
(
"create-only"
);
}));
}
private
ContextConsumer
<
AssertableApplicationContext
>
vendorProperties
(
Consumer
<
Map
<
String
,
Object
>>
vendorProperties
)
{
return
(
context
)
->
vendorProperties
.
accept
(
context
.
getBean
(
HibernateJpaConfiguration
.
class
).
getVendorProperties
());
}
@Test
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment