Modi
Modi

Reputation: 2355

QueryDSL delete method

I'm using spring-data-mongodb 1.2.0 with QueryDSL 2.9.0.

Why doesn't the QueryDslPredicateExecutor have a delete(Predicate predicate) method?

Is there a workaround?

Upvotes: 9

Views: 8621

Answers (3)

Tomás Nunes
Tomás Nunes

Reputation: 21

I was able to perform this by:

    @Autowired
    LocalContainerEntityManagerFactoryBean emFactory;
    
    EntityManager em;
    
    @PostConstruct
    private void setup(){
    em = emFactory.getObject().createEntityManager();
    }
    
        @Transactional private voide deleteByPredicate(Predicate predicate) { 
JPADeleteClause deleteClause = new JPADeleteClause(em, QPersonEntity.personEntity);
    em.joinTransaction();
    deleteClause.where(predicate).execute(); 
    }

Upvotes: 1

BehroOoz
BehroOoz

Reputation: 1

using JPADeleteClause, pass two following parameters to it: entityManager and EntityPath QClass

QPersonEntity path = QPersonEntity.personEntity;
JPADeleteClause deleteClause = new JPADeleteClause(getEntityManager(), path);
deleteClause.where(path.name.eq("behrooz")).execute();

Upvotes: -1

Dhanush Gopinath
Dhanush Gopinath

Reputation: 5739

What you can probably do is this. With the predicate for "where" condition, query for the objects and then pass that to the delete method

QMyObj obj= new QMyObj("myObj");
Iterable<MyObj> myObjs = myObjRepository.findAll(obj.property.eq("property"));
myObjRepository.delete(myObjs);

Here I am first creating an instance of the Q class and then finding all the objects based on the predicate. Then calling the repository's void delete(Iterable<? extends T> entities) method.

May be it is because of this workaround they don't provide it, but that is for the Spring Source guys to confirm

Upvotes: 4

Related Questions