0

I'm trying to delete an entity but the delete query is not generated and there's no error shown in the console :

@Override
@Transactional
public void removeClassObject(MyClassObject classObject) {
    MyClassObject ip = entityManager.find(MyClassObject.class, classObject.getId());
    entityManager.remove(ip);
}

Take notice : @Transactional is from springFramework package

EDIT :

All my configuration are ok, because I already have the merge and persist functions doing there job without any problem it's just the remove method which doesn't generate any sql query and does not remove the given entity.

EDIT 2 :

This is how I obtain my entityManager :

@PersistenceContext(type = PersistenceContextType.TRANSACTION)
protected EntityManager entityManager;
Community
  • 1
  • 1
OddDev
  • 1,521
  • 7
  • 23
  • 45

1 Answers1

0

If you are using @Transactional annotation, you should consider using interface for your service, and not only implementation.

@Transactional will need a dynamic proxy to be created on your bean to apply the transactional logic, which can be created if your Service has an interface. Otherwise you would need to manage transaction on your own.

In answer I assume that you create entity manager with @PersistanceContext annotation and your service has no interface.

For mor information : Spring transactions

EDIT:

Also make sure, that you have enabled transactions in your configuration. Look here for similar error but with wrong configuration LINK

Emil Hotkowski
  • 2,233
  • 1
  • 13
  • 19
  • I'm already implementing an interface, I added the @Override annotation in my edit – OddDev Apr 17 '18 at 11:14
  • how do you obtain your EntityManager? @MoatezBouhdid – Emil Hotkowski Apr 17 '18 at 11:18
  • All my configuration are ok, because I already have the merge and persist functions doing there job without any problem it's just the remove method which doesn't generate any sql query and does not remove the given entity – OddDev Apr 17 '18 at 11:26
  • I reask my previous question "how do you obtain your EntityManager? "? With @PersistanceContext or with EntityManagerFactory? – Emil Hotkowski Apr 17 '18 at 11:30
  • with @PersistanceContext – OddDev Apr 17 '18 at 11:31
  • btw no sql is generated, but did it has effect on database? I am asking if it removed but only hadn't shown logs. – Emil Hotkowski Apr 17 '18 at 11:41
  • No, the line is not removed from the database – OddDev Apr 17 '18 at 11:42
  • Is it possible that you have some relation with this entity where you use it as optional = false? Read here the anwser, maybe it will help https://stackoverflow.com/questions/26349213/hibernate-entitymanager-remove-referenced-entity-not-working – Emil Hotkowski Apr 17 '18 at 11:51
  • No, I don't have that, I already have multiple entities with a fully functionnal DAO layer and MyClassObject entity is nearly the same as the others – OddDev Apr 17 '18 at 11:54
  • Try adding entityManager.flush() after removing and see if you get any error or it will work just fine. – Emil Hotkowski Apr 17 '18 at 12:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/169172/discussion-between-moatez-bouhdid-and-rjiuk). – OddDev Apr 17 '18 at 13:08