Error "IllegalStateException: Cannot get unfetched attribute [tokens] from detached object" but not always

I have something strange happening on my project. I have a Bean with 2 methods. Both, do a query, on the same entity, use the same view, and only change the condition of the search, and then iterate over a collection to call another service. This 2 methods are called from 2 different scheduled tasks.

First method, works perfectly without problems, returns the record of the table, and iterate over tokens, but second, once get the record from table, get the error on the title when trying to getTokens() to iterate over it. I know this error comes from a property not added to the view, but can’t understand where is the problem here, because as I said, not only is the same entity and same view, it’s the same code BTW.

Some idea?

First method:

@Override
public void procesarNotificacionesMensajesNuevos() {
    NotificacionesService servicio = AppBeans.get(NotificacionesService.class);

    LoadContext<NotificacionesPool> loadContext = LoadContext.create(NotificacionesPool.class)
            .setQuery(LoadContext.createQuery("Select e from project_NotificacionesPool e where e.tipoNotificacion = :tipo and e.procesado is null")
                    .setParameter("tipo", TipoNotificacionPool.CHAT_NUEVOS))
            .setView("notificacionesPool-view");

    List<NotificacionesPool> lista = dataManager.loadList(loadContext);
    for(NotificacionesPool noti: lista){
        for(FirebaseToken token: noti.getDestino().getTokens()) {
            servicio.enviar(token.getToken(),"CHAT", "Tienes mensajes sin leer.");
        }
        noti.setProcesado(new Date());
        dataManager.commit(noti);
    }
}

Second method

@Override
public void procesarNotificacionesDeCompra() {
    NotificacionesService servicio = AppBeans.get(NotificacionesService.class);

    LoadContext<NotificacionesPool> loadContext = LoadContext.create(NotificacionesPool.class)
            .setQuery(LoadContext.createQuery("Select e from project_NotificacionesPool e where e.tipoNotificacion = :tipo and e.procesado is null and e.anuncio is not null")
                    .setParameter("tipo", TipoNotificacionPool.ARTICULO_COMPRADO))
            .setView("notificacionesPool-view");

    List<NotificacionesPool> lista = dataManager.loadList(loadContext);
    for(NotificacionesPool noti: lista){
        for(FirebaseToken token: noti.getDestino().getTokens()) {
            String mensaje = "Su artículo ".concat(noti.getAnuncio().getDescripcion()).concat(" ha sido comprado.");
            servicio.enviar(token.getToken(),"Anuncio", mensaje );
        }
        noti.setProcesado(new Date());
        dataManager.commit(noti);
    }
}

Hi,
So what is the definition of the “notificacionesPool-view” view?
Does it contain necessary attributes?

My blind guess is that destino object is repeating between some noti objects in the result list, and when you commit dataManager.commit(noti);, it makes destino object loose its tokens. So if you move commit out of the cycle, it may help. But it’s just a guess.
It’s possible to say anything determinely only if you will be able to prepare a demo project reproducing the problem.

Thanks for your answer. Not sure if I totally understand. If I move the commit outside the for, can’t commit the instances. BTW, it’s the same code I use in other process (only where clause changes) and it works as expected)