22
22
import java .util .stream .Stream ;
23
23
import java .util .stream .StreamSupport ;
24
24
25
+ import jakarta .persistence .EntityManager ;
25
26
import jakarta .persistence .EntityManagerFactory ;
26
27
import jakarta .persistence .metamodel .EntityType ;
27
28
@@ -43,17 +44,24 @@ public EclipseStoreDataImporter(final EclipseStoreStorage eclipseStoreStorage)
43
44
this .eclipseStoreStorage = eclipseStoreStorage ;
44
45
}
45
46
47
+ @ SuppressWarnings ("java:S1452" )
46
48
public List <SimpleEclipseStoreRepository <?, ?>> importData (final EntityManagerFactory ... entityManagerFactories )
47
49
{
48
50
return this .importData (Arrays .stream (entityManagerFactories ));
49
51
}
50
52
51
- public List <SimpleEclipseStoreRepository <?, ?>> importData (final Iterable <EntityManagerFactory > entityManagerFactories )
53
+ @ SuppressWarnings ("java:S1452" )
54
+ public List <SimpleEclipseStoreRepository <?, ?>> importData (
55
+ final Iterable <EntityManagerFactory > entityManagerFactories
56
+ )
52
57
{
53
58
return this .importData (StreamSupport .stream (entityManagerFactories .spliterator (), false ));
54
59
}
55
60
56
- public List <SimpleEclipseStoreRepository <?, ?>> importData (final Stream <EntityManagerFactory > entityManagerFactories )
61
+ @ SuppressWarnings ({"java:S1452" , "java:S6204" })
62
+ public List <SimpleEclipseStoreRepository <?, ?>> importData (
63
+ final Stream <EntityManagerFactory > entityManagerFactories
64
+ )
57
65
{
58
66
LOG .info ("Start importing data from JPA Repositories to EclipseStore..." );
59
67
@@ -62,7 +70,7 @@ public EclipseStoreDataImporter(final EclipseStoreStorage eclipseStoreStorage)
62
70
entityManagerFactories
63
71
.map (this ::createEclipseStoreRepositoriesFromEntityManager )
64
72
.toList ();
65
- LOG .info (String . format ( "Found %d repositories to export data from." , allRepositories .size () ));
73
+ LOG .info ("Found {} repositories to export data from." , allRepositories .size ());
66
74
67
75
// Now copy the data
68
76
allRepositories .forEach (
@@ -89,28 +97,33 @@ private static <T> void copyData(
89
97
{
90
98
final String className = classRepositoryPair .domainClass .getName ();
91
99
92
- LOG .info (String .format ("Loading entities of %s..." , className ));
93
- final List <T > existingEntitiesToExport =
94
- entityManagerFactoryRepositoryListPair .entityManagerFactory .createEntityManager ()
95
- .createQuery ("SELECT c FROM " + className + " c" )
96
- .getResultList ();
97
- LOG .info (String .format (
98
- "Loaded %d entities of type %s to export." ,
99
- existingEntitiesToExport .size (),
100
- className
101
- ));
102
-
103
- LOG .info (String .format (
104
- "Saving %d entities of type %s to the EclipseStore Repository..." ,
105
- existingEntitiesToExport .size (),
106
- className
107
- ));
108
- classRepositoryPair .repository .saveAll (existingEntitiesToExport );
109
- LOG .info (String .format (
110
- "Done saving entities of type %s. The EclipseStore now holds %d entities of that type." ,
111
- className ,
112
- classRepositoryPair .repository .count ()
113
- ));
100
+ LOG .info ("Loading entities of {}..." , className );
101
+ try (final EntityManager entityManager =
102
+ entityManagerFactoryRepositoryListPair .entityManagerFactory .createEntityManager ())
103
+ {
104
+ final List <T > existingEntitiesToExport =
105
+ entityManager
106
+ .createQuery ("SELECT c FROM " + className + " c" , classRepositoryPair .domainClass )
107
+ .getResultList ();
108
+
109
+ LOG .info (
110
+ "Loaded {} entities of type {} to export." ,
111
+ existingEntitiesToExport .size (),
112
+ className
113
+ );
114
+
115
+ LOG .info (
116
+ "Saving {} entities of type {} to the EclipseStore Repository..." ,
117
+ existingEntitiesToExport .size (),
118
+ className
119
+ );
120
+ classRepositoryPair .repository .saveAll (existingEntitiesToExport );
121
+ LOG .info (
122
+ "Done saving entities of type {}. The EclipseStore now holds {} entities of that type." ,
123
+ className ,
124
+ classRepositoryPair .repository .count ()
125
+ );
126
+ }
114
127
}
115
128
116
129
private EntityManagerFactoryRepositoryListPair createEclipseStoreRepositoriesFromEntityManager (
0 commit comments