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
674b01cb
Commit
674b01cb
authored
Feb 07, 2021
by
bono007
Committed by
Stephane Nicoll
Mar 22, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Take JPA database action into account when setting ddlAuto
See gh-25129
parent
e4fa39df
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
1 deletion
+65
-1
DataSourceInitializedPublisher.java
...autoconfigure/orm/jpa/DataSourceInitializedPublisher.java
+2
-1
HibernateProperties.java
...ework/boot/autoconfigure/orm/jpa/HibernateProperties.java
+4
-0
HibernateJpaAutoConfigurationTests.java
...configure/orm/jpa/HibernateJpaAutoConfigurationTests.java
+45
-0
HibernatePropertiesTests.java
.../boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java
+14
-0
No files found.
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/DataSourceInitializedPublisher.java
View file @
674b01cb
...
@@ -129,7 +129,8 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
...
@@ -129,7 +129,8 @@ class DataSourceInitializedPublisher implements BeanPostProcessor {
:
"none"
);
:
"none"
);
Map
<
String
,
Object
>
hibernate
=
this
.
hibernateProperties
.
determineHibernateProperties
(
Map
<
String
,
Object
>
hibernate
=
this
.
hibernateProperties
.
determineHibernateProperties
(
this
.
jpaProperties
.
getProperties
(),
new
HibernateSettings
().
ddlAuto
(
defaultDdlAuto
));
this
.
jpaProperties
.
getProperties
(),
new
HibernateSettings
().
ddlAuto
(
defaultDdlAuto
));
return
hibernate
.
containsKey
(
"hibernate.hbm2ddl.auto"
);
return
hibernate
.
containsKey
(
"hibernate.hbm2ddl.auto"
)
||
!
hibernate
.
getOrDefault
(
"javax.persistence.schema-generation.database.action"
,
"none"
).
equals
(
"none"
);
}
}
/**
/**
...
...
spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateProperties.java
View file @
674b01cb
...
@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
...
@@ -35,6 +35,7 @@ import org.springframework.util.StringUtils;
* Configuration properties for Hibernate.
* Configuration properties for Hibernate.
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Chris Bono
* @since 2.1.0
* @since 2.1.0
* @see JpaProperties
* @see JpaProperties
*/
*/
...
@@ -129,6 +130,9 @@ public class HibernateProperties {
...
@@ -129,6 +130,9 @@ public class HibernateProperties {
}
}
private
String
determineDdlAuto
(
Map
<
String
,
String
>
existing
,
Supplier
<
String
>
defaultDdlAuto
)
{
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
);
String
ddlAuto
=
existing
.
get
(
AvailableSettings
.
HBM2DDL_AUTO
);
if
(
ddlAuto
!=
null
)
{
if
(
ddlAuto
!=
null
)
{
return
ddlAuto
;
return
ddlAuto
;
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaAutoConfigurationTests.java
View file @
674b01cb
...
@@ -91,6 +91,7 @@ import static org.mockito.Mockito.mock;
...
@@ -91,6 +91,7 @@ import static org.mockito.Mockito.mock;
* @author Andy Wilkinson
* @author Andy Wilkinson
* @author Kazuki Shimizu
* @author Kazuki Shimizu
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Chris Bono
*/
*/
class
HibernateJpaAutoConfigurationTests
extends
AbstractJpaAutoConfigurationTests
{
class
HibernateJpaAutoConfigurationTests
extends
AbstractJpaAutoConfigurationTests
{
...
@@ -371,6 +372,50 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
...
@@ -371,6 +372,50 @@ class HibernateJpaAutoConfigurationTests extends AbstractJpaAutoConfigurationTes
.
run
((
context
)
->
assertThat
(
context
).
doesNotHaveBean
(
City
.
class
));
.
run
((
context
)
->
assertThat
(
context
).
doesNotHaveBean
(
City
.
class
));
}
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenDdlAutoPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.hibernate.ddl-auto:create-drop"
,
true
);
}
@Test
void
dataSourceSchemaCreatedEventNotFiredWhenDdlAutoPropertyIsSetToNone
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.hibernate.ddl-auto:none"
,
false
);
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenHibernateSpecificDdlAutoPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.hibernate.hbm2ddl.auto=create"
,
true
);
}
@Test
void
dataSourceSchemaCreatedEventNotFiredWhenHibernateSpecificDdlAutoPropertyIsSetToNone
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.hibernate.hbm2ddl.auto=none"
,
false
);
}
@Test
void
dataSourceSchemaCreatedEventFiredWhenJpaDbActionPropertyIsSet
()
{
dataSourceSchemaCreatedEventFired
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create"
,
true
);
}
@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
);
});
}
@Test
@Test
void
withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException
()
{
void
withSyncBootstrappingAnApplicationListenerThatUsesJpaDoesNotTriggerABeanCurrentlyInCreationException
()
{
contextRunner
().
withUserConfiguration
(
JpaUsingApplicationListenerConfiguration
.
class
)
contextRunner
().
withUserConfiguration
(
JpaUsingApplicationListenerConfiguration
.
class
)
...
...
spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/orm/jpa/HibernatePropertiesTests.java
View file @
674b01cb
...
@@ -44,6 +44,7 @@ import static org.mockito.Mockito.verify;
...
@@ -44,6 +44,7 @@ import static org.mockito.Mockito.verify;
*
*
* @author Stephane Nicoll
* @author Stephane Nicoll
* @author Artsiom Yudovin
* @author Artsiom Yudovin
* @author Chris Bono
*/
*/
class
HibernatePropertiesTests
{
class
HibernatePropertiesTests
{
...
@@ -135,6 +136,19 @@ class HibernatePropertiesTests {
...
@@ -135,6 +136,19 @@ class HibernatePropertiesTests {
.
run
(
assertDefaultDdlAutoNotInvoked
(
"create"
));
.
run
(
assertDefaultDdlAutoNotInvoked
(
"create"
));
}
}
@Test
void
defaultDdlAutoIsNotInvokedAndDdlAutoIsNotSetIfJpaDbActionPropertyIsSet
()
{
this
.
contextRunner
.
withPropertyValues
(
"spring.jpa.properties.javax.persistence.schema-generation.database.action=drop-and-create"
)
.
run
(
assertHibernateProperties
((
hibernateProperties
)
->
{
assertThat
(
hibernateProperties
).
doesNotContainKey
(
AvailableSettings
.
HBM2DDL_AUTO
);
assertThat
(
hibernateProperties
).
containsEntry
(
AvailableSettings
.
HBM2DDL_DATABASE_ACTION
,
"drop-and-create"
);
verify
(
this
.
ddlAutoSupplier
,
never
()).
get
();
}));
}
private
ContextConsumer
<
AssertableApplicationContext
>
assertDefaultDdlAutoNotInvoked
(
String
expectedDdlAuto
)
{
private
ContextConsumer
<
AssertableApplicationContext
>
assertDefaultDdlAutoNotInvoked
(
String
expectedDdlAuto
)
{
return
assertHibernateProperties
((
hibernateProperties
)
->
{
return
assertHibernateProperties
((
hibernateProperties
)
->
{
assertThat
(
hibernateProperties
).
containsEntry
(
AvailableSettings
.
HBM2DDL_AUTO
,
expectedDdlAuto
);
assertThat
(
hibernateProperties
).
containsEntry
(
AvailableSettings
.
HBM2DDL_AUTO
,
expectedDdlAuto
);
...
...
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