Skip to content

Commit

Permalink
Update HibernateEntityModule to allow for binding the same set
Browse files Browse the repository at this point in the history
of entities to multiple datasources.

GitOrigin-RevId: 391e2194a72b5c6b7c79982550a9163853c529da
  • Loading branch information
rgfindl authored and svc-squareup-copybara committed Jan 14, 2025
1 parent aa11bb4 commit 300c730
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
2 changes: 2 additions & 0 deletions misk-hibernate/api/misk-hibernate.api
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ public abstract class misk/hibernate/HibernateEntityModule : misk/inject/KAbstra
protected final fun addEntities ([Lkotlin/reflect/KClass;)V
protected final fun addEntity (Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;)V
public static synthetic fun addEntity$default (Lmisk/hibernate/HibernateEntityModule;Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;Lkotlin/reflect/KClass;ILjava/lang/Object;)V
protected final fun addHibernateEntities ([Lkotlin/reflect/KClass;)V
protected final fun addHibernateEntity (Lkotlin/reflect/KClass;)V
protected final fun bindListener (Lorg/hibernate/event/spi/EventType;Lmisk/hibernate/BindPolicy;)Lcom/google/inject/binder/LinkedBindingBuilder;
public static synthetic fun bindListener$default (Lmisk/hibernate/HibernateEntityModule;Lorg/hibernate/event/spi/EventType;Lmisk/hibernate/BindPolicy;ILjava/lang/Object;)Lcom/google/inject/binder/LinkedBindingBuilder;
protected fun configure ()V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,26 @@ abstract class HibernateEntityModule(
addEntity(T::class)
}

/** Bind just the HibernateEntities using the provided qualifier */
protected fun addHibernateEntities(vararg entities: KClass<out DbEntity<*>>) {
entities.forEach { entity ->
addHibernateEntity(dbEntityClass = entity)
}
}

/** Bind just the HibernateEntity using the provided qualifier */
protected fun <T : DbEntity<T>> addHibernateEntity(
dbEntityClass: KClass<T>
) {
multibind<HibernateEntity>(qualifier)
.toInstance(HibernateEntity(dbEntityClass))
}

/** Bind just the HibernateEntity using the provided qualifier */
protected inline fun <reified T : DbEntity<T>> addHibernateEntity() {
addHibernateEntity(T::class)
}

/** Adds a DbEntity to Database-Query Admin Dashboard Tab with a dynamic query
* (not a static Misk.Query)
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,26 @@ internal class HibernateEntityModuleTest {
.containsExactly(Square::class, Circle::class)
}

@Test fun multipleDataSourcesSameEntities() {
val injector = Guice.createInjector(
object : HibernateEntityModule(Dinosaurs::class) {
override fun configureHibernate() {
addHibernateEntities(Triceratops::class, Stegosaurus::class)
}
},
object : HibernateEntityModule(Shapes::class) {
override fun configureHibernate() {
addHibernateEntities(Triceratops::class, Stegosaurus::class)
}
}
)

assertThat(injector.getSetOf(HibernateEntity::class, Dinosaurs::class).unwrap())
.containsExactly(Triceratops::class, Stegosaurus::class)
assertThat(injector.getSetOf(HibernateEntity::class, Shapes::class).unwrap())
.containsExactly(Triceratops::class, Stegosaurus::class)
}

@Test fun multipleModulesSameDataSource() {
val injector = Guice.createInjector(
object : HibernateEntityModule(Dinosaurs::class) {
Expand Down

0 comments on commit 300c730

Please sign in to comment.