-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : Consolidated JPA properties under a common namespace for easie…
…r configuration and maintenance. (#1307) * feat : use common properties for both db * refactor to reduce duplicate
- Loading branch information
1 parent
8ecf7f3
commit 7fdb737
Showing
7 changed files
with
84 additions
and
114 deletions.
There are no files selected for viewing
47 changes: 47 additions & 0 deletions
47
.../main/java/com/example/multipledatasources/configuration/BaseDataSourceConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.example.multipledatasources.configuration; | ||
|
||
import jakarta.persistence.EntityManagerFactory; | ||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.autoconfigure.orm.jpa.JpaProperties; | ||
import org.springframework.boot.orm.jpa.EntityManagerFactoryBuilder; | ||
import org.springframework.orm.jpa.JpaTransactionManager; | ||
import org.springframework.orm.jpa.JpaVendorAdapter; | ||
import org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager; | ||
import org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter; | ||
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter; | ||
import org.springframework.transaction.PlatformTransactionManager; | ||
|
||
public abstract class BaseDataSourceConfiguration { | ||
|
||
protected final PersistenceUnitManager persistenceUnitManager; | ||
protected final JpaProperties jpaProperties; | ||
|
||
protected BaseDataSourceConfiguration( | ||
ObjectProvider<PersistenceUnitManager> persistenceUnitManager, JpaProperties jpaProperties) { | ||
this.persistenceUnitManager = persistenceUnitManager.getIfAvailable(); | ||
this.jpaProperties = jpaProperties; | ||
} | ||
|
||
protected EntityManagerFactoryBuilder createEntityManagerFactoryBuilder() { | ||
JpaVendorAdapter jpaVendorAdapter = createJpaVendorAdapter(); | ||
return new EntityManagerFactoryBuilder( | ||
jpaVendorAdapter, jpaProperties.getProperties(), this.persistenceUnitManager); | ||
} | ||
|
||
protected JpaVendorAdapter createJpaVendorAdapter() { | ||
AbstractJpaVendorAdapter adapter = new HibernateJpaVendorAdapter(); | ||
adapter.setShowSql(jpaProperties.isShowSql()); | ||
if (jpaProperties.getDatabase() != null) { | ||
adapter.setDatabase(jpaProperties.getDatabase()); | ||
} | ||
if (jpaProperties.getDatabasePlatform() != null) { | ||
adapter.setDatabasePlatform(jpaProperties.getDatabasePlatform()); | ||
} | ||
adapter.setGenerateDdl(jpaProperties.isGenerateDdl()); | ||
return adapter; | ||
} | ||
|
||
protected PlatformTransactionManager createTransactionManager(EntityManagerFactory entityManagerFactory) { | ||
return new JpaTransactionManager(entityManagerFactory); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-sql-db.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
--liquibase formatted sql | ||
|
||
--changeset raja:create-sequence | ||
CREATE SEQUENCE member_seq START WITH 100 INCREMENT BY 50; | ||
CREATE SEQUENCE member_seq START WITH 101 INCREMENT BY 50; |
9 changes: 6 additions & 3 deletions
9
jpa/boot-data-multipledatasources/src/main/resources/db/changelog/db.changelog-xml-db.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
<?xml version="1.1" encoding="UTF-8" standalone="no"?> | ||
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd"> | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.20.xsd"> | ||
|
||
<property name="string.type" value="varchar(255)" dbms="!postgresql"/> | ||
<property name="string.type" value="text" dbms="postgresql"/> | ||
|
||
<changeSet author="raja" id="create-table"> | ||
|
||
<createTable tableName="member"> | ||
<column name="id" type="BIGINT"> | ||
<constraints nullable="false" primaryKey="true" primaryKeyName="pk_member"/> | ||
</column> | ||
<column name="name" type="VARCHAR(255)"/> | ||
<column name="member_id" type="VARCHAR(255)"/> | ||
<column name="name" type="${string.type}"/> | ||
<column name="member_id" type="${string.type}"/> | ||
</createTable> | ||
</changeSet> | ||
</databaseChangeLog> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters