11package io .quarkus .hibernate .panache .deployment ;
22
3- import java .util .Collections ;
43import java .util .HashMap ;
4+ import java .util .List ;
55import java .util .Map ;
66import java .util .Optional ;
77import java .util .Set ;
88import java .util .TreeMap ;
99
1010import io .quarkus .hibernate .orm .deployment .JpaModelPersistenceUnitMappingBuildItem ;
11+ import io .quarkus .hibernate .orm .deployment .PersistenceUnitDescriptorBuildItem ;
1112
1213//FIXME: duplicate with ORM and probably HR
1314public final class EntityToPersistenceUnitUtil {
1415
16+ @ FunctionalInterface
17+ public interface TriConsumer {
18+ void consume (String entity , String persistenceUnit , String reactivePersistenceUnit );
19+ }
20+
1521 private EntityToPersistenceUnitUtil () {
1622 }
1723
1824 /**
1925 * Given the candidate entities, return a map with the persistence unit that single contains them
2026 * or throw an exception if any of the candidates is part of more than one persistence unit
2127 */
22- public static Map < String , String > determineEntityPersistenceUnits (
28+ public static void determineEntityPersistenceUnits (
2329 Optional <JpaModelPersistenceUnitMappingBuildItem > jpaModelPersistenceUnitMapping ,
24- Set <String > candidates , String source ) {
30+ List <PersistenceUnitDescriptorBuildItem > descriptors ,
31+ Set <String > candidates , String source ,
32+ TriConsumer consumer ) {
2533 if (jpaModelPersistenceUnitMapping .isEmpty ()) {
26- return Collections . emptyMap () ;
34+ return ;
2735 }
2836 Map <String , String > result = new HashMap <>();
2937 Map <String , Set <String >> collectedEntityToPersistenceUnits = jpaModelPersistenceUnitMapping .get ()
3038 .getEntityToPersistenceUnits ();
3139
40+ Map <String , PersistenceUnitDescriptorBuildItem > descriptorsMap = new HashMap <>();
41+ for (PersistenceUnitDescriptorBuildItem descriptor : descriptors ) {
42+ descriptorsMap .put (descriptor .getPersistenceUnitName (), descriptor );
43+ }
44+
3245 Map <String , Set <String >> violatingEntities = new TreeMap <>();
3346
3447 for (Map .Entry <String , Set <String >> entry : collectedEntityToPersistenceUnits .entrySet ()) {
@@ -40,10 +53,39 @@ public static Map<String, String> determineEntityPersistenceUnits(
4053 continue ;
4154 }
4255
43- if (selectedPersistenceUnits .size () == 1 ) {
44- result .put (entityName , selectedPersistenceUnits .iterator ().next ());
45- } else {
46- violatingEntities .put (entityName , selectedPersistenceUnits );
56+ String persistenceUnit = null ;
57+ String reactivePersistenceUnit = null ;
58+ if (selectedPersistenceUnits .size () > 0 ) {
59+ boolean error = false ;
60+ // collect at most one of blocking/reactive PU
61+ for (String selectedPersistenceUnit : selectedPersistenceUnits ) {
62+ PersistenceUnitDescriptorBuildItem descriptor = descriptorsMap .get (selectedPersistenceUnit );
63+ if (descriptor == null ) {
64+ throw new IllegalStateException (String .format ("Cannot find descriptor unit named %s for entity %s" ,
65+ selectedPersistenceUnit , entityName ));
66+ }
67+ if (descriptor .isReactive ()) {
68+ if (reactivePersistenceUnit != null ) {
69+ error = true ;
70+ break ;
71+ } else {
72+ reactivePersistenceUnit = selectedPersistenceUnit ;
73+ }
74+ } else {
75+ if (persistenceUnit != null ) {
76+ error = true ;
77+ break ;
78+ } else {
79+ persistenceUnit = selectedPersistenceUnit ;
80+ }
81+ }
82+ }
83+ if (error ) {
84+ violatingEntities .put (entityName , selectedPersistenceUnits );
85+ } else if (persistenceUnit != null || reactivePersistenceUnit != null ) {
86+ // ignore the entity if it belongs to no PU, though I doubt that happens
87+ consumer .consume (entityName , persistenceUnit , reactivePersistenceUnit );
88+ }
4789 }
4890 }
4991
@@ -57,7 +99,5 @@ public static Map<String, String> determineEntityPersistenceUnits(
5799 throw new IllegalStateException (message .toString ());
58100 }
59101 }
60-
61- return result ;
62102 }
63103}
0 commit comments