Automagiczne usuwanie obiektu

0

Hej
Mam taką klasę

@Transactional
public abstract class BusinessObject<T extends AbstractEntity> {
    private T entity;
    private Class<T> clazz;
    protected EntityManager entityManager;
    public BusinessObject(@NotNull T entity, Class<T> clazz,EntityManager entityManager) {
        this.entity = entity;
        this.clazz = clazz;
        this.entityManager = entityManager;
    }

    public T attached() {
        return entityManager.find(clazz,entity.getId());
    }

    public void delete(){
        attached().deactivate();
}
}

Chodzi o to czy w momencie wywołania delete nie powinien iść jakiś update do bazy? Klasa Encji


```@Data
@MappedSuperclass
public abstract class AbstractEntity implements Identifiable<Long> {
   //id itp
    @Column
    private boolean active = true;

    public void deactivate(){
        active = false;
    }
}

Wywołanie usunięcia wygląda tak
```java
    public void removeById(long id){
        leagueFinder.find(id).orElseThrow(()->new ResourceNotFoundException("No League with id: "+id)).delete();
    }

a finder tak

@Service
public class LeagueFinder implements Finder<League,Long>{

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public Optional<League> find(Long id) {
        LeagueEntity leagueEntity = entityManager.find(LeagueEntity.class,id);
        if(leagueEntity !=null && leagueEntity.isActive()){
            return Optional.of(new League(leagueEntity,entityManager));
        }
        return Optional.empty();
    }
}

League to obiekt który dziedziczy po BusinessObject ale w tym kontekście nic to nie zmienia.
Co chcę osiągnąć? Obiekty BusinessObject jako "nakładki" na encje, aby same encje były anemiczne a wszystkie metody biznesowe przenieść poziom abstrakcji wyżej do Obiektów biznesowych i teraz testuję jak to mogę łatwo osiągnąć.

0

Hej,

Po wywołaniu tego:

    public T attached() {
        return entityManager.find(clazz,entity.getId());
    }

T nie jest już attached (raczej ;)

0

Problem rozwiązany po przed dodanie @Transactional w service. Obiekty biznesowe nie są beanami dlatego ta adnotacja nie działała

1 użytkowników online, w tym zalogowanych: 0, gości: 1