= JdoEntityDiscoveryListener

:Notice: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at. http://www.apache.org/licenses/LICENSE-2.0 . Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR  CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.


The `JdoEntityDiscoveryListener` interface is called during bootstrapping after the `PersistenceManagerFactory` has been created.
It can be used to perform additional bootstrapping tasks.

For example, to eagerly register all named queries, you could use:

[source,java]
.NamedQueryEagerRegistrar.java
----
@Component
public class NamedQueryEagerRegistrar implements JdoEntityDiscoveryListener {

    @Override
    public void onEntitiesDiscovered(
            final @NonNull PersistenceManagerFactory persistenceManagerFactory,
            final @NonNull Set<Class<?>> entityTypes,
            final @NonNull Map<String, Object> properties) {

        val pmf = (JDOPersistenceManagerFactory) persistenceManagerFactory;
        val nucleusContext = pmf.getNucleusContext();

        val clr = nucleusContext.getClassLoaderResolver(_Context.getDefaultClassLoader());
        val metaDataManager = nucleusContext.getMetaDataManager();

        metaDataManager
            .getClassesWithMetaData()
            .forEach(className->{
                val meta = metaDataManager.getMetaDataForClass(className, clr);
                _NullSafe.stream(meta.getQueries())
                .forEach(metaDataManager::registerNamedQuery);
            });
    }
}
----

You could also use an implementation to eagerly create the database tables in the database:

[source,java]
.DatabaseCreator.java
----
@Component
public class DatabaseCreator implements JdoEntityDiscoveryListener {

    @Override
    public void onEntitiesDiscovered(
            final @NonNull PersistenceManagerFactory persistenceManagerFactory,
            final @NonNull Set<Class<?>> entityTypes,
            final @NonNull Map<String, Object> properties) {

        val pmf = (JDOPersistenceManagerFactory) persistenceManagerFactory;
        val nucleusContext = pmf.getNucleusContext();
        val schemaAwareStoreManager = (SchemaAwareStoreManager)nucleusContext.getStoreManager();

        val classNames = entityTypes
                .stream()
                .map(Class::getName)
                .collect(Collectors.toSet());

        schemaAwareStoreManager.createSchemaForClasses(classNames, properties);
    }
}
----

However, for this second use case it's easier to simply set the `datanucleus.schema.generate-database.mode` configuration property.

