Issue with persistence of programmatically created IDocument

Hello,
I’m trying to fill the database with a few documents that I create in the backend.

To do so I listen to an event that fires once the persistence is ready, all models loaded, etc:

@CommonDataServicesEventListener
    public void onApplicationEventDataServicesInitializationEvent(ContentStoreInitializationFinishedEvent event) {
        createA12Doc(); 
    }

The document is created, filled as a V2 document, converted back to a V1 document and then saved:

DataServicesDocument result = documentService.create(doc, new Locale("DE"));

To see whether this worked I query the database using the DocumentQueryService…

ResultSet<DocumentReference> queryDocRefs = documentQueryService.query("MinSt_input", filterSpec2, null, null, null);
queryDocRefs.getEntries().forEach(e -> System.out.println(e.toString()));

I get the expected result! The document was created successfully and I am able to find it in the SOLR repo:

MinSt_input/ace67305-c2bd-4a0f-834b-07e95f11d9ab

However, now I fill a document manually through the frontend, using the form. Once I save the document it seems that the originally created document disappears. The original document also never appeared in the table.

I have a listener for the document creation:

@CommonDataServicesEventListener
    DataServicesDocument listenToCreation(DocumentAfterCreateEvent event) throws Exception {

Its purpose is to trigger another query to see how many documents are now managed by Solr.
Again I print out the docRefs to the console:

queryDocRefs.getEntries().forEach(e -> System.out.println(e.toString()));

Only one document shows up, although together with the initial document created on ContentStoreInitializationFinishedEvent event, I’d now expect 2 entries.

I then tested the following: what if I implement a method that creates a document with .create() in the backend, triggered by the creation of a document in the frontend via the form?
It works! Now when I create a document with the form, 2 documents fill the table of the Overview.

The question:
Why does it seem that the persistence “forgets” previously created documents after the ContentStoreInitializationFinishedEvent , but remembers them at or after the stage where I can create documents via the form view?

My suspicion is that there must be an event even after ContentStoreInitializationFinishedEvent that I could use to properly persist the database with initial data.
For this task i also do not wish to use rpc calls via .json files in the import module.

Thanks for the help.

We found the solution.
While the data was visible in the h2 database, the frontend didn’t show it. This was due to authorization issues. Once we created a document in the frontend we use a different SecurityContext with another Authentication than the one we used before logging into the application. Triggering the document generation in the backend again would use the new SecurityContext, which is why it worked. This was visible when checking the creator field of the meta data.

 "__meta": {

    "creator": "admin",

Using:

@Transactional
@Authenticated(username = "admin")

the method works as expected.