Injecting service, application not working anymore

Hello,

I have tried to implement a service. This service should return a new article number for the init() function of the “Artikel” entity. However if I insert the @Inject for the service the application doesn’t work anymore (console errors in attached image). If I comment the @Inject line the application works.

My service interface:

package com.company.testprojekt.service;

import com.company.testprojekt.entity.Artikel;

public interface ArtikelService {
    String NAME = "testprojekt_ArtikelService";

    void neueBumNummerErzeugen(Artikel artikel);
}

My service implementation:

package com.company.testprojekt.service;

import org.springframework.stereotype.Service;
import com.company.testprojekt.entity.Artikel;
import com.company.testprojekt.entity.Nummer;
import com.haulmont.cuba.core.Persistence;
import com.haulmont.cuba.core.global.Metadata;
import com.haulmont.cuba.core.EntityManager;
import com.haulmont.cuba.core.Transaction;
import javax.inject.Inject;

@Service(ArtikelService.NAME)
public class ArtikelServiceBean implements ArtikelService {

    @Inject
    Persistence persistence;

    @Inject
    Metadata metadata;

    public void neueBumNummerErzeugen(Artikel artikel) {
        try( Transaction tx = persistence.createTransaction()) {
            EntityManager em = persistence.getEntityManager();
            Nummer neueNummer = metadata.create(Nummer.class);
            neueNummer.setBezeichnung("B+M Nummer");
            neueNummer.setNummer("7XXXXXX");
            neueNummer.setArtikelID(artikel);
            em.persist(neueNummer);
            tx.commit();
        }
    }
}

My entity:

package com.company.testprojekt.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import com.haulmont.cuba.core.entity.StandardEntity;
import javax.persistence.Column;
import javax.persistence.Lob;
import com.haulmont.chile.core.annotations.NamePattern;
import com.haulmont.chile.core.annotations.Composition;
import com.haulmont.cuba.core.entity.annotation.OnDelete;
import com.haulmont.cuba.core.entity.annotation.OnDeleteInverse;
import com.haulmont.cuba.core.global.DeletePolicy;
import java.util.List;
import java.util.ArrayList;
import javax.persistence.OneToMany;
import com.haulmont.chile.core.annotations.MetaProperty;
import com.haulmont.cuba.core.global.PersistenceHelper;

import javax.persistence.Transient;
import javax.inject.Inject;
import com.haulmont.cuba.core.entity.annotation.Lookup;
import com.haulmont.cuba.core.entity.annotation.LookupType;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.annotation.PostConstruct;
import com.company.testprojekt.service.ArtikelService;

@NamePattern("%s|bezeichnung")
@Table(name = "TESTPROJEKT_ARTIKEL")
@Entity(name = "testprojekt$Artikel")
public class Artikel extends StandardEntity {
    @Inject
    private ArtikelService artikelService;

    @PostConstruct
    protected void init() {
    //    if(getBumNummer() == null)
    //    {
    //        // create B+M Nummer, normal at creation of new entity, abnormal but self-correcting with existent entity
    //        artikelService.neueBumNummerErzeugen(this);
    //    }
    }
}

Unbenannt

1 Like

Hi,

Dependency injection is not supported in entity classes, you cannot inject services there. If needed, you can use AppBeans static class:

public BigDecimal calculateTotals() {
    return AppBeans.get(OrderService.class).calculateTotals(order);
}
1 Like